Appengine Multipart Servlet

时间:2016-06-20 09:47:40

标签: java google-app-engine servlets

我的appengine存在多个问题(在java 7中)。我想创建一个接受multipart / form-data请求的servlet。我读过这个:How to upload files to server using JSP/Servlet?

AppengineServlet版本是2.5所以我不能使用getParts(),getParte(“”)等... (我已经尝试过这个)。我得到了“500服务器错误”。

protected void doPost(HttpServletRequest req,
        HttpServletResponse res) throws ServletException, IOException {

try {
    List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(req); /This is not working
    for (FileItem item : items) {
        if (item.isFormField()) {
            // Process regular form field (input type="text|radio|checkbox|etc", select, etc).
            String fieldName = item.getFieldName();
            String fieldValue = item.getString();
            // ... (do your job here)
        } else {
            // Process form file field (input type="file").
            String fieldName = item.getFieldName();
            String fileName = FilenameUtils.getName(item.getName());
            InputStream fileContent = item.getInputStream();
            // ... (do your job here)
        }
   }
} catch (Exception e) {
    JSONObject json = new JSONObject();
    json.put("errorMessage", e.getMessage());
    res.setContentType("application/json");
    json.put("error","Cannot parse multipart request.");
    PrintWriter out = res.getWriter();
    out.print(json.toString());
    out.flush();
    out.close();
    //throw new ServletException("Cannot parse multipart request.", e);
}




}

错误:

java.lang.NoClassDefFoundError: Could not initialize class org.apache.commons.fileupload.disk.DiskFileItem
at org.apache.commons.fileupload.disk.DiskFileItemFactory.createItem(DiskFileItemFactory.java:191)
at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:350)
at org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:126)
at com.crumbits.servlet.CreateCrumbServlet.doPost(CreateCrumbServlet.java:39)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
at com.google.apphosting.utils.servlet.ParseBlobUploadFilter.doFilter(ParseBlobUploadFilter.java:125)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.apphosting.runtime.jetty.SaveSessionFilter.doFilter(SaveSessionFilter.java:37)

0 个答案:

没有答案