现在我用Extjs 配合 struts2 实现文件上传功能,现在的问题是:struts.multipart.maxSize=10240当上传的文件的大小大于 系统规定的最大文件10240时候,文件能上传成功,但是tomcat 后台会报:
2010-04-01 09:13:58 [org.apache.struts2.dispatcher.multipart.MultiPartRequest]-[ERROR] org.apache.commons.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (8157321) exceeds the configured maximum (10240)
异常。当上传文件的大小小于 系统规定的最大文件10240时候,文件上传不成功。 不知道什么情况。请各位大虾 帮助下 啊

解决方案 »

  1.   

    下面是上传的servlet 代码package com.aoyang.servlet;import java.io.File;
    import java.util.Date;
    import java.util.Iterator;
    import java.util.List;import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;import org.apache.commons.fileupload.FileItem;
    import org.apache.commons.fileupload.disk.DiskFileItemFactory;
    import org.apache.commons.fileupload.servlet.ServletFileUpload;
    import org.springframework.web.context.support.WebApplicationContextUtils;import com.aoyang.common.util.FileUtil;
    import com.aoyang.pubinfo.bean.PubFileAttach;
    import com.aoyang.pubinfo.service.PubFileAttachService;public class FileUploadServlet extends HttpServlet {    private PubFileAttachService pubFileAttachService;
            
        private String uploadPath;
        private String tempPath;
        private String fileCat;
        
        public FileUploadServlet() {
         pubFileAttachService = this.pubFileAttachService;
            uploadPath = "";
            tempPath = "";
            fileCat = "post";
        }    
    public void init() throws ServletException {
    uploadPath = getServletContext().getRealPath("/attachFiles/");
    File uploadPathFile = new File(uploadPath);
            if(!uploadPathFile.exists()) {
                uploadPathFile.mkdirs();    
            }
            tempPath = (new StringBuilder(String.valueOf(uploadPath))).append("/temp").toString();
            File tempPathFile = new File(tempPath);
            if(!tempPathFile.exists()) {
             tempPathFile.mkdirs();
            }
    }
     protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
     resp.setCharacterEncoding("UTF-8");
     HttpSession session = req.getSession();
     pubFileAttachService = (PubFileAttachService)WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext()).getBean("pubFileAttachService");  try {
    DiskFileItemFactory factory = new DiskFileItemFactory();
     factory.setSizeThreshold(4096);
     factory.setRepository(new File(tempPath));
     ServletFileUpload fu = new ServletFileUpload(factory);
     List fileItems = fu.parseRequest(req);
             for(Iterator iterator = fileItems.iterator(); iterator.hasNext();) {
                 FileItem fi = (FileItem)iterator.next();
                 if("file_cat".equals(fi.getFieldName())) {
                      //   System.out.println("11111::"+fi.getFieldName());
                     fileCat = fi.getString();
                     break;
                 }
             }
             for(Iterator i = fileItems.iterator(); i.hasNext();) {
              FileItem fi = (FileItem)i.next();
              if(!"file_cat".equals(fi.getFieldName())) {
                      //   System.out.println("2222::"+fi.getFieldName());
                  String path = fi.getName();
                  System.out.println((new StringBuilder("path:")).append(path).toString());
                  int start = path.lastIndexOf("\\");
                  String fileName = path.substring(start + 1);
                  String relativeFullPath = (new StringBuilder(String.valueOf(fileCat))).append("/").append(FileUtil.generateFilename(fileName)).toString();
                  int index = relativeFullPath.lastIndexOf("/");
                  File dirPath = new File((new StringBuilder(String.valueOf(uploadPath))).append("/").append(relativeFullPath.substring(0, index + 1)).toString());
                     if(!dirPath.exists()) {
                         dirPath.mkdirs();                    
                     }
                     fi.write(new File((new StringBuilder(String.valueOf(uploadPath))).append("/").append(relativeFullPath).toString()));
                     PubFileAttach file = new PubFileAttach();
                     file.setCreatetime(new Date());
                     String empId = (String)session.getAttribute("empId");
                     System.out.println(empId);
                     if(empId != null) {
                     file.setCreator(empId);                  
                     }
                         else {
                          file.setCreator("UNKown");                    
                         }
                     int dotIndex = fileName.indexOf(".");
                     file.setExt(fileName.substring(dotIndex + 1));
                     file.setFilename(fileName);
                     file.setFilepath(relativeFullPath);
                     file.setFiletype(fileCat);
                     file.setNote((new StringBuilder(String.valueOf(fi.getSize()))).append(" bytes").toString());                  
                     pubFileAttachService.save(file);
                     StringBuffer sb = new StringBuffer("{success:true");
                     sb.append(",fileId:").append(file.getFileid()).append(",fileName:'").append(file.getFilename()).append("',filePath:'").append(file.getFilepath()).append((new StringBuilder("',message:'upload file success.(")).append(fi.getSize()).append(" bytes)'").toString());
                     sb.append("}");
                     System.out.println((new StringBuilder("str:")).append(sb.toString()).toString());
                     resp.getWriter().write(sb.toString());
                             
              }
             }  
    } catch (Exception e) {
    e.printStackTrace();
    }
     
     
     
     }
    }
      

  2.   

    struts.multipart.maxSize=10240
    Struts2使用fileUpload拦截器实现文件上传的时候才会用到上面的参数你实现的文件上传是直接调用Commons FileUpload
    设置文件上传大小需要使用
    ServletFileUpload fu = new ServletFileUpload(factory);
    fu.setFileSizeMax(10240);
      

  3.   

    哈哈 我当时也是这个问题@ 
    这个size的单位是字节! 也就是说你设置的只有10k。。
    明白了吧@
      

  4.   

    http://topic.csdn.net/u/20100301/11/47754d01-8272-4aa4-ac19-fdf234c20943.html对了就给分啦@@
      

  5.   

    文件大小和类型的限制是通过Struts2拦截器实现的你只需要在上传的action中做如下配置就可以<param name="allowedTypes">
          application/octet-stream,application/x-zip-compressed,image/bmp,image/png,image/gif,image/jpeg,image/jpg,image/x-png,
          image/pjpeg
    </param>
    <param name="maximumSize">5242880</param>但是有一点要注意Struts2默认文件上传最大为2M即便你设置了<param name="maximumSize">5242880</param>当上传的文件大于2M时候也会出错的这时要设置另外一个常量<constant name="struts.multipart.maxSize" value="1000000000"/>要让他的value设置的比你限定上传最大值要大一点。