Spring上传文件

来源:互联网 发布:php 统计文章浏览次数 编辑:程序博客网 时间:2024/06/08 03:02

在使用Spring上传文件时,当文件过多时,会产生文件名重复的问题,所以就提出了用时间来划分文件夹来存放文件的方式,这种方式能减少产生重复文件的概率,但也有不可避免的在一个小时内上传同一个文件两次的概率,这样也会产生覆盖

[java] view plain copy
  1. public class UpLoadFile extends HttpServlet  
  2. {  
  3.   private static final long serialVersionUID = 1L;  
  4.   private static final Logger LOG = Logger.getLogger(UpLoadFile.class.getName());  
  5.   
  6.   private int maxPostSize = 1048576000;  
  7.   
  8.   public void doGet(HttpServletRequest request, HttpServletResponse resopnse)  
  9.     throws IOException  
  10.   {  
  11.     doPost(request, resopnse);  
  12.   }  
  13.   
  14.   public void doPost(HttpServletRequest request, HttpServletResponse resopnse)  
  15.     throws IOException  
  16.   {  
  17.     try  
  18.     {  
  19.       upLoad(request, resopnse);  
  20.     } catch (Exception e) {  
  21.       e.printStackTrace();  
  22.     }  
  23.   }  
  24.   
  25.   private void upLoad(HttpServletRequest request, HttpServletResponse resopnse)  
  26.     throws Exception  
  27.   {  
  28.     String uploadPath = "上传文件存放的位置";  
  29.     LOG.debug("uploadPath:" + uploadPath);  
  30.   
  31.     if (!new File(uploadPath).exists()) {  
  32.       new File(uploadPath).mkdirs();  
  33.     }  
  34.   
  35.     resopnse.setContentType("text/html;charset=UTF-8");  
  36.     request.setCharacterEncoding("UTF-8");  
  37.     DiskFileItemFactory factory = new DiskFileItemFactory();  
  38.     factory.setSizeThreshold(20480);  
  39.       
  40.   
  41.     ServletFileUpload upload = new ServletFileUpload(factory);  
  42.     upload.setSizeMax(this.maxPostSize);  
  43.     try {  
  44.       List fileItems = upload.parseRequest(request);  
  45.       Iterator iter = fileItems.iterator();  
  46.       String fileNameSave = request.getParameter("filename");  
  47.       Date date=new Date();  
  48.       DateFormat format=new SimpleDateFormat("yyyyMMddHH");  
  49.       String time=format.format(date);  
  50.       String fileSavePath = uploadPath+time+"/";  
  51.       File filePath =new File(fileSavePath);      
  52.         //如果文件夹不存在则创建      
  53.         if  (!filePath .exists()  && !filePath .isDirectory())        
  54.         {         
  55.             System.out.println("//不存在");    
  56.             filePath .mkdir();      
  57.         } else     
  58.         {    
  59.             System.out.println("//目录存在");    
  60.         }    
  61.       String exFileName = "";  
  62.       while (iter.hasNext()) {  
  63.         FileItem item = (FileItem)iter.next();  
  64.         String itemName = item.getName();  
  65.         if (!item.isFormField()) {  
  66.           if (itemName.indexOf(".") > 0) {  
  67.             exFileName = itemName.substring(itemName  
  68.               .indexOf("."));  
  69.           }  
  70.           fileSavePath = fileSavePath + itemName.substring(0, itemName.indexOf("."));  
  71.           try {  
  72.             File skFile = new File(fileSavePath + exFileName);  
  73.             if (skFile.exists()) {  
  74.               skFile.delete();  
  75.               item.write(new File(fileSavePath + exFileName));  
  76.             } else {  
  77.               item.write(new File(fileSavePath + exFileName));  
  78.             }  
  79.           } catch (Exception e) {  
  80.             e.printStackTrace();  
  81.           }  
  82.         }  
  83.       }  
  84.       resopnse.getWriter().print("newFileName:" + fileSavePath + exFileName);  
  85.       LOG.debug("upload newFileName:" + fileSavePath + exFileName);  
  86.     } catch (FileUploadException e) {  
  87.       e.printStackTrace();  
  88.     }  
  89.   }  
原创粉丝点击