0

我的问题是 setSizeThreshold 和 setRepository 是如何工作的。

如果我错了,请纠正我,我的理解是如果文件大小 > setSizeThreshold(无论数字如何),那么它应该将文件发送到临时存储的 setRepository。但我从来没有看到这个文件存储在 setRepository(filePath) 中。我没有 setFileCleaningTracker,所以从技术上讲,文件应该仍然在 setRepository(filePath) 中?

  1. 如何测试存储库是否正常工作?有办法吗?

这是我的代码,一切都来自文档,没什么特别的。

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);
        }

    }

在文档中

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

Very large upload requests should not be permitted.

  1. 究竟什么是“非常大”?什么尺寸被认为是非常大的?

提前致谢...

4

1 回答 1

-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();
        }
于 2014-08-27T07:04:58.463 回答