using apache fileupload on GAE

后端 未结 2 829
無奈伤痛
無奈伤痛 2020-12-06 19:39

I use Apache Commons FileUpload in a java server-side app that has a html form with fields :

  1. A destination fied that will be filled with email address of the d
2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-06 20:01

    You should use the FileItemIterator of the Apache Commons FileUpload.

    import org.apache.commons.fileupload.FileItemStream;
    import org.apache.commons.fileupload.FileItemIterator;
    import org.apache.commons.fileupload.servlet.ServletFileUpload;
    import java.io.InputStream;
    ..
    public void doPost(HttpServletRequest req, HttpServletResponse res)
          throws ServletException, IOException {
        try {
          ServletFileUpload upload = new ServletFileUpload();
          res.setContentType("text/plain");
    
          FileItemIterator iterator = upload.getItemIterator(req);
          while (iterator.hasNext()) {
            FileItemStream item = iterator.next();
            String name = item.getFieldName();
            InputStream stream = item.openStream();
    
            if (item.isFormField()) {
              //regular form field
              resp.getWriter().println(("Form:" + name + " : " + Streams.asString(stream));
            } else {
              //fileform field 
              resp.getWriter().println(("File:" +name + " : " + item.getName());
            }
    
          }
        } catch (Exception ex) {
          throw new ServletException(ex);
        }
    }
    

提交回复
热议问题