【问题标题】:Apache File upload, FileItemIterator is emptyApache文件上传,FileItemIterator为空
【发布时间】:2016-01-20 00:32:06
【问题描述】:

我想要在移动设备上进行简单的图像捕捉/上传。对于实际的文件上传,我使用 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>

【问题讨论】:

    标签: java servlets apache-commons


    【解决方案1】:

    试试这个:

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

    【讨论】:

    • 我不能。显然,新的公共 API 为 parseRequest(RequestContext) 踢了 parseRequest(request)。从我读过的内容来看,这应该没什么区别
    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-30
      • 2018-02-04
      • 2016-10-15
      • 2016-11-10
      • 2011-05-31
      相关资源
      最近更新 更多