<%@ page contentType="text/html;charset=GBK" %>
<%@ page language="java" import="com.jspsmart.upload.*"%>
<%@ page import="java.text.SimpleDateFormat"%>
<%@ page import="java.io.File"%>
<%@ page import="java.util.*"%>
<%@ page import="javazoom.upload.*"%>
<%@ page import="uploadutilities.FileMover"%>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<%  
      request.setCharacterEncoding("GBK");//设置编码格式，就不用一个个转码了。
      FileMover fileMover = new FileMover();//你也可以使用自带的实例中jsp:useBean的形式。
   UploadBean upBean = new UploadBean();
      MultipartFormDataRequest mrequest = null;
   Hashtable files = null;
   
   if (MultipartFormDataRequest.isMultipartFormData(request))
      {

   mrequest = new MultipartFormDataRequest(request,null,100*1024*1024,MultipartFormDataRequest.COSPARSER,"GBK");//注意这里也要设置编码参数
     String sTt0 = mrequest.getParameter("title");
     out.println("<br>Title0是："+sTt0+"<br>");
     String sTt1 = new String(sTt0.getBytes("ISO-8859-1"),"GBK");
     out.println("<br>Title1是："+sTt1+"<br>");
     //这里用来测试title参数是否正确。调试的时候，加一句if (true)return;即可。
         files = mrequest.getFiles();
   }


 //获取修改前的文件名
 String sOldFileName =mrequest.getParameter("oldfilename"); 
 out.println("sOldFileName:"+sOldFileName);
 String sWebRootPath = request.getRealPath("/");//得到你的web应用的根。
 String sPath=sWebRootPath+"attach";
 int iFileCount = 0;
 String sServerFileName="";
 String sLocalFileName = "";
 //文件获取
 if ( (files != null) || (!files.isEmpty()) ) {
 
  iFileCount = files.size();
  UploadFile file = (UploadFile) files.get("attach");
  sLocalFileName=file.getFileName();
  out.println("sLocalFileName:"+sLocalFileName);
  int ii= sLocalFileName.indexOf("."); //取文件名的后缀
  String sExt = sLocalFileName.substring(ii,sLocalFileName.length());
  //得到不重复的文件名
  java.util.Date dt = new java.util.Date(System.currentTimeMillis());
  SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmssSSS");
  sServerFileName= fmt.format(dt);
  sServerFileName =sServerFileName + sExt;
  //如果不存在该目录，则新建一个
  File dir =new File(sPath);
  if (!dir.exists()){
   dir.mkdirs();
  }
  upBean.setFolderstore(sPath);//设置要上传的目录
  upBean.addUploadListener(fileMover);//增加filMover监听
  fileMover.setNewfilename(sServerFileName);//设置服务器上的文件名
  upBean.store(mrequest, "attach");//上传
  out.println("file path is "+sPath+"/"+sServerFileName);
 }

%>

