JavaWeb 通过smartupload 实现上传

来源:互联网 发布:淘宝首页图片轮播代码 编辑:程序博客网 时间:2024/05/23 23:25

注意这边要加入jar包,

String filePath=getServletContext().getRealPath("/")+"file";

 File file=new File(filePath);
 if(!file.exists()){
 file.mkdir();
 }
 
 SmartUpload upload = new SmartUpload(); 
 Request req = upload.getRequest();  
 try{  
   upload.initialize(getServletConfig(), request, response);  
   // 允许上传的文件类型  
   upload.setAllowedFilesList("doc,xls,txt,docx");  
   // 拒绝上传的文件类型  
   try {
upload.setDeniedFilesList("exe,bat,jsp");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}  
   // 允许上传文件的单个最大大小  
   upload.setMaxFileSize(1024 * 1024 * 20);  
   // 允许上传文件的最大大小总和  
   upload.setTotalMaxFileSize(1024*1024*10);  
   //上传数据  
   upload.upload();  
   int count=upload.save(filePath);
   System.out.println(count);
   }  
   catch (SmartUploadException e){  
       e.printStackTrace();  
       return;  
   }  
 String emaill= req.getParameter("?");                 //这边可以取上传的数据