zoukankan      html  css  js  c++  java
  • 转:upload.parseRequest为空

    FileItemFactory factory = new DiskFileItemFactory();  
    ServletFileUpload upload = new ServletFileUpload(factory);  
    upload.setHeaderEncoding("UTF-8");  
    List items = upload.parseRequest(request);   

    上传是items一直是空list。导致原因是struts2把原始的原来S2为简化上传功能,把所有的enctype="multipart/form-data"表单做了wrapper最后把HttpServletResquest封装成 org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper 怪不得我的 ServletFileUpload.parseRequest(request)不行!!! 

    看我怎么改!废话不多说,直接贴代码

     
      1. MultiPartRequestWrapper wrapper = (MultiPartRequestWrapper) request;   
      2.         File file = wrapper.getFiles("imgFile")[0];   
      3.         String fileName = wrapper.getFileNames("imgFile")[0];  
      4.         //检查文件大小  
      5.         if(file.length() > maxSize){  
      6.             String temStr= "上传文件大小超过限制。";  
      7.             this.writeResponse(response, temStr);  
      8.             return;  
      9.         }  
      10.         //检查扩展名  
      11.         String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();  
      12.         if(!Arrays.<String>asList(extMap.get(dirName).split(",")).contains(fileExt)){  
      13.             String temStr= "上传文件扩展名是不允许的扩展名。 只允许" + extMap.get(dirName) + "格式。";  
      14.             this.writeResponse(response, temStr);  
      15.             return;  
      16.         }  
      17.   
      18.         SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");  
      19.         String newFileName = df.format(new Date()) + "_" + new Random().nextInt(1000) + "." + fileExt;  
      20.   
      21.         try {    
      22.             InputStream in = new FileInputStream(file);    
      23.             File uploadFile = new File(savePath, newFileName);    
      24.             OutputStream out = new FileOutputStream(uploadFile);    
      25.             byte[] buffer = new byte[1024 * 1024];    
      26.             int length;    
      27.             while ((length = in.read(buffer)) > 0) {    
      28.                 out.write(buffer, 0, length);    
      29.             }    
      30.     
      31.             in.close();    
      32.             out.close();    
      33.         } catch (FileNotFoundException ex) {    
      34.             ex.printStackTrace();    
      35.         } catch (IOException ex) {    
      36.             ex.printStackTrace();    
      37.         }    
  • 相关阅读:
    git shell自动打tag
    git 获取最新的匹配到的tag
    centOS debian ubuntu 一键安装 docker 教程
    git将当前分支的修改推到其他分支
    MusicPlayer2
    Ventoy 多合一启动盘制作工具神器
    electron-builder 下载 electron过慢或报错的解决办法
    解决npm install 报错“npm WARN pug-loader@2.4.0 requires a peer of pug@^2.0.0 but none is installed. You must install peer dependencies yourself.“
    全能笔记软件 Notion 的“中国版" wolai
    Your branch is behind 'origin/master' by N commits, and can be fast-forwarded 解决方法
  • 原文地址:https://www.cnblogs.com/blueherb/p/3998583.html
Copyright © 2011-2022 走看看