2013-10-09 77 views
0

我的問題是setSizeThreshold和setRepository是如何工作的。Java Commons File Upload,size,repository

糾正我,如果我錯了,我的理解是如果文件大小> setSizeThreshold(什麼數目)然後它應該發送文件到setRepository臨時存儲。但我從來沒有看到這個文件存儲在setRepository(filePath)中。我沒有設置FileCleaningTracker,因此在技術上該文件應該仍然在setRepository(filePath)中?

  1. 如何測試Repository是否正在工作?有沒有辦法?

這是我的代碼,一切都是從doc沒有什麼特別的。

boolean isMultipart = ServletFileUpload.isMultipartContent(request); 

    if(isMultipart){ 

     // Create a factory for disk-based file items 
     DiskFileItemFactory factory = new DiskFileItemFactory(); 

     // Set factory constraints 
     factory.setSizeThreshold(20480); // purposely make it small to test 
     factory.setRepository(new File(root + "tempFile")); 

     // Create a new file upload handler 
     ServletFileUpload upload = new ServletFileUpload(factory); 

     //upload.setSizeMax(20480); // This makes the upload stop 

     try 
     { 
      // Parse the request 
      List<FileItem> items = upload.parseRequest(request); 

      // Process the uploaded items 
      Iterator<FileItem> i = items.iterator(); 

      while(i.hasNext()) 
      { 

       FileItem item = i.next(); 

       //request.setAttribute("item", item); 

       if(item.isFormField()){ 
        String fieldName = item.getFieldName(); 
        String fieldValue = item.getString(); 

        out.println(fieldName + ": " + fieldValue); 
       } 
       else 
       { 
        String fieldName = item.getFieldName(); 
        String fileName = item.getName(); 
        String contentType = item.getContentType(); 
        boolean isInMemory = item.isInMemory(); 
        long sizeInBytes = item.getSize(); 

        File name = new File(item.getName()); 

        // File.separator: "/" 
        item.write(new File(root + "uploads" + File.separator + name)); 


       } 


      } 

     } catch (FileUploadException ex) 
     { 
      ex.printStackTrace(); 
     } catch (Exception ex) 
     { 
      Logger.getLogger(uploadFileServlet.class.getName()).log(Level.SEVERE, null, ex); 
     } 

    } 

在DOC

Larger items should be written to a temporary file on disk.

Very large upload requests should not be permitted.

  1. 究竟是什麼 「非常大」?什麼大小被認爲非常大?

在此先感謝...

+0

**題外話:主要觀點基於:**你基本上是問的東西,不能比你自己以外的任何人來回答。你認爲什麼大。我定期與PETABYTES的數據合作,我的觀點與其他大多數人相比將會大不相同。與其他人一樣。 –

回答

-1
public class NServletController extends HttpServlet { 
    private static final long serialVersionUID = 1L; 

    //private boolean isMultipart; 
    private String FilePath; 
    private File file; 
    private String Address=""; 
    private String Telephone=""; 
    private String Email=""; 
    private String MobileNumber=""; 
    private String Name =""; 
    private String Workdetails=""; 
    private String AccountName=""; 
    private String BankName=""; 
    private String Accountnumber=""; 
    private String BranchName=""; 
    private String Ifsdetails=""; 
    private String Pannumber=""; 
    //private String submit=""; 
    private String filename=""; 
    FileInputStream fis=null; 
    // private String name=""; 
    private int rs1 = 0; 
    private String r=""; 
    private int rs3=0; 
    String filePath = "E:\\Myuploads\\"; 






    /** 
    * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 
    */ 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     // TODO Auto-generated method stub 

     PrintWriter out = response.getWriter(); 

     boolean isMultipartContent = ServletFileUpload.isMultipartContent(request); 

     FileItemFactory factory = new DiskFileItemFactory(); 
     ServletFileUpload upload = new ServletFileUpload(factory); 
     try { 
      List<FileItem> fields = upload.parseRequest(request); 
      Iterator<FileItem> it = fields.iterator(); 
      if (!it.hasNext()) { 

       return; 
      } 
      while (it.hasNext()) { 
       FileItem fileItem = it.next(); 
       boolean isFormField = fileItem.isFormField(); 

       if (isFormField && fileItem.getFieldName().equals("name")) { 
        Name=fileItem.getString(); 
       } 
       else if(isFormField && fileItem.getFieldName().equals("address")) 
       { 
        Address = fileItem.getString(); 
       } 
       else if(isFormField && fileItem.getFieldName().equals("email")) 
       { 
        Email = fileItem.getString(); 
       } 
       else if(isFormField && fileItem.getFieldName().equals("mobileno")) 
       { 
        MobileNumber = fileItem.getString(); 
       } 
       else if(isFormField && fileItem.getFieldName().equals("telephone")) 
       { 
        Telephone = fileItem.getString(); 
       } 
       else if(isFormField && fileItem.getFieldName().equals("work")) 
       { 
        Workdetails= fileItem.getString(); 
       } 
       else if(isFormField && fileItem.getFieldName().equals("accountholdername")) 
       { 
        AccountName = fileItem.getString(); 
       } 
       else if(isFormField && fileItem.getFieldName().equals("bankname")) 
       { 
        BankName = fileItem.getString(); 
       } 
       else if(isFormField && fileItem.getFieldName().equals("accountno")) 
       { 
        Accountnumber = fileItem.getString(); 
       } 
       else if(isFormField && fileItem.getFieldName().equals("branchname")) 
       { 
        BranchName = fileItem.getString(); 
       } 
       else if(isFormField && fileItem.getFieldName().equals("ifsccode")) 
       { 
        Ifsdetails = fileItem.getString(); 
       } 
       else if(isFormField && fileItem.getFieldName().equals("pannumber")) 
       { 
        Pannumber = fileItem.getString(); 
       } 
       else if(isFormField && fileItem.getFieldName().equals("submit")) 
       { 
        String submit = fileItem.getString(); 
       } 
       else { 
        filename = FilenameUtils.getName(fileItem.getName()); 


        OutputStream outputStream = new FileOutputStream(filename); 
        InputStream inputStream = fileItem.getInputStream(); 
        int readBytes = 0; 
        byte[] buffer = new byte[10000]; 
        while ((readBytes = inputStream.read(buffer, 0, 10000)) != -1) { 
         outputStream.write(buffer, 0, readBytes); 
        } 
        if(fileItem.getName()!= null){ 
         // out.println("<td><img width='100' heigth='100' src="+ request.getContextPath() + "/images/"+ fileItem.getName() + "></td>"); 

         if (filename.lastIndexOf("\\") >= 0) { 
          file = new File(filePath 
            + filename.substring(filename 
              .lastIndexOf("\\"))); 
         } else { 
          file = new File(filePath 
            + filename.substring(filename 
              .lastIndexOf("\\") + 1)); 
         } 
         fileItem.write(file); 
        }          
       } 
      } 
     }catch(Exception e) 
     { 
      e.printStackTrace(); 
     } 
+2

你能解釋一下你的解決方案嗎? – Jens

+0

此代碼塊似乎不回答問題(「setSizeThreshold和setRepository如何工作」)。如果你認爲它確實如此,請爲此提供一些解釋。 – DavidPostill