Apache常见上传

时间:2011-08-09 11:21:06

标签: java servlets apache-commons

我尝试使用apache常见上传将文件上传到servlet。但是此代码List items = uploadHandler.parseRequest(request);未执行,并且此代码下面没有任何代码。可能是什么问题?

这是我的代码:

System.out.println("Start File upload");
    DiskFileItemFactory fileItemFactory = new DiskFileItemFactory();
    /*
     * Set the size threshold, above which content will be stored on disk.
     */
    fileItemFactory.setSizeThreshold(1 * 1024 * 1024); // 1 MB
    /*
     * Set the temporary directory to store the uploaded files of size above
     * threshold.
     */
    // fileItemFactory.setRepository(tmpDir);

    if(ServletFileUpload.isMultipartContent(request)) {
        System.out.println("Multipart");
    } else {
        System.out.println("Not multipart");
    }

    ServletFileUpload uploadHandler = new ServletFileUpload(fileItemFactory);
    System.out.println("end file upload");
    try {
        /*
         * Parse the request
         */
        System.out.println("start file parse");
        List items = uploadHandler.parseRequest(request);
        System.out.println("start file parse2");
        Iterator itr = items.iterator();
        while (itr.hasNext()) {
            System.out.println("Iterating");
            FileItem item = (FileItem) itr.next();
            /*
             * Handle Form Fields.
             */System.out.println("end fileiterating");
            if (item.isFormField()) {
                System.out.println("form field");
                System.out.println("File Name = " + item.getFieldName()
                        + ", Value = " + item.getString());
            } else {
                // Handle Uploaded files.
                System.out.println("file");
                System.out.println("Field Name = " + item.getFieldName()
                        + ", File Name = " + item.getName()
                        + ", Content type = " + item.getContentType()
                        + ", File Size = " + item.getSize());
                /*
                 * Write file to the ultimate location.
                 */
                // File file = new File(destinationDir,item.getName());
                // item.write(file);
            }
        }
    } catch (FileUploadException ex) {
        //System.out.println("Error encountered while parsing the request "
            //  + ex);
        ex.printStackTrace();
    } catch (Exception ex) {
        //System.out.println("Error encountered while uploading file " + ex);
        ex.printStackTrace();
    }

0 个答案:

没有答案
相关问题