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()) { String name = item.getFieldName(); FileItemStream item = iterator.next(); 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); } }
Read full article from java - using apache fileupload on GAE - Stack Overflow
No comments:
Post a Comment