Apache文件上传,FileItemIterator为空

时间:2014-05-12 14:56:39

标签: java servlets apache-commons

我希望在移动设备上进行简单的图像捕获/上传。对于实际的文件上传,我使用Apache Commons。现在我遇到了与此处描述的问题相同的问题:Apache commons fileupload FileItemIterator hasNext() returns false,但我无法在post方法之前找出请求可能消耗的位置。

我在我的开发机器上测试过,当我放入任何文件(图像与否)时,迭代器都是空的。

以下是我所拥有的一切,我标记了实际的文件上传代码,因此您不必筛选所有内容。

感谢您的帮助!

@WebServlet("/scanner")
public class MyServiceServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    private static final int maxFileSize = 50 * 1024;
    private static final int maxMemSize = 4 * 1024;
    private static final String saveFolder = "/";

    private final String repoFolder;
    private final DiskFileItemFactory factory;

    public MyServiceServlet() {
        final String os = System.getProperty("os.name");

        if (os.contains("Windows"))
            repoFolder = "C:\\temp";
        else
            repoFolder = "/tmp";

        factory = new DiskFileItemFactory();
        factory.setSizeThreshold(maxMemSize);

        // Location to save data that is larger than maxMemSize.
        factory.setRepository(new File(this.repoFolder));
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("get");
        request.getRequestDispatcher("MyService/start.html").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("Post");
        response.setContentType("text/html");

        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head>");
        out.println("<title>MyService</title>");
        out.println("</head>");
        out.println("<body>");

        if (!ServletFileUpload.isMultipartContent(request)) {
            out.println("<p>Nothing uploaded</p>");

        } else {

            // Create a new file upload handler
            ServletFileUpload upload = new ServletFileUpload(factory);
            // maximum file size to be uploaded.
            upload.setSizeMax(maxFileSize);


                    //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!FILE UPLOAD HERE
                    try {
            FileItemIterator it = upload.getItemIterator(request);
            while (it.hasNext()) {
                FileItem item = (FileItem) it.next();
                if (!item.isFormField()) {
                    String fileName = item.getName();
                    String contentType = item.getContentType();
                    boolean isInMemory = item.isInMemory();
                    long sizeInBytes = item.getSize();
                    file = new File(saveFolder + fileName);
                    item.write(file);
                    System.out.println(file);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

            this.printUserForm(out);
        }

        out.println("</body>");
        out.println("</html>");
    }
}

start.html文件,我可能在这里遗漏了一些东西

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>MyService</title>
</head>
<body>
<form enctype="multipart/form-data" method="post" name="input" action="/MySercice/scanner">
  <input type="file" accept="image/*;capture=camera"></input>
  <input type="submit" value="upload">
 </form>
</body>
</html>

web.xml,如果有帮助的话。但这里什么都没有,真的

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

  <display-name>MyService</display-name>
  <welcome-file-list>

    <welcome-file>start.html</welcome-file>
  </welcome-file-list>

</web-app>

2 个答案:

答案 0 :(得分:0)

试试这个:

if (ServletFileUpload.isMultipartContent(request)) {
            try {
            List<FileItem> multiparts = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
                for (FileItem item : multiparts) {
                    if (!item.isFormField()) {

答案 1 :(得分:0)

对于使用Seam的人: 按照@ CR7建议,我可以通过将以下配置添加到components.xml

来使其工作
<web:multipart-filter create-temp-files="true"  
                      max-request-size="1024000" url-pattern="*"/>

Ps:我不知道为什么,但对我来说,我在url-pattern中放入的任何其他值都会使列表返回空。