当图像上传时,Dropzon js发送空POST请求(显然)

时间:2016-02-09 18:32:39

标签: java jsp dropzone.js

我正在尝试使用Dropzone js上传多个图片,并预览用户表单。我已经使用jsp servlet实现了它。但我无法在服务器端获取图像。当在dropzone组件上选择图像但是HttpServletequest请求不包含图像数据(显然)时,将调用服务器端的doPost方法。可能是我正在读取请求数据错误或其他一些问题。请帮忙。我使用的是Netbeans 8.0.2,Wildfly 8.2 final,jdk 1.8。这是我的html页面的代码:



Dropzone.options.imagedropform = {
  paramName: 'file',
  addRemoveLinks: true,
  maxFilesize: 10, // MB
  maxFiles: 5
  };

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Image upload</title>
        <script src="${pageContext.request.contextPath}/imageuploadresources/customdropzone.js" type="text/javascript"></script>
        <script src="${pageContext.request.contextPath}/imageuploadresources/dropzone.js"></script>
        <link rel="stylesheet" href="${pageContext.request.contextPath}/imageuploadresources/dropzone.css">
    </head>
    <body>
        <form id="imagedropform" action="Upload" class="dropzone">
            <div class="dz-message">
                Drop files here or click to upload.
            </div>
        </form>
    </body>
</html>
&#13;
&#13;
&#13;

这是我在this tutorial之后的上传servlet:

@WebServlet("/Upload")
public class Upload extends HttpServlet {

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    PrintWriter out = response.getWriter();

    if (!ServletFileUpload.isMultipartContent(request)) {
        throw new IllegalArgumentException("Request is not multipart, please 'multipart/form-data' enctype for your form.");
    }

    ServletFileUpload uploadHandler = new ServletFileUpload(new DiskFileItemFactory());

    fileWriter.println(new File(request.getServletContext().getRealPath("/") + "/images/"));
    try {
        List<FileItem> items = uploadHandler.parseRequest(request);
        fileWriter.println(items.toString());

        for (FileItem item : items) {
            fileWriter.println(item.toString()+"===="+item.getName());
            if (!item.isFormField()) {
                File file = new File(request.getServletContext().getRealPath("/") + "/images/", item.getName());
                item.write(file);

                System.out.println("uploaded");
            }
        }
    } catch (FileUploadException e) {
        throw new RuntimeException(e);
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        fileWriter.close();
        out.close();
    }
}

}
}

我上传视图的一些截图 initial output image selected for upload

如果可以使用JSF(没有Spring)的某些解决方案,请同时建议我。或者其他一些易于使用的好看的插件,用于使用JSF 2.2(除了primefaces或其他一些面孔)上传多个图像也是受欢迎的。感谢

1 个答案:

答案 0 :(得分:0)

我经过一番搜索后解决了这个问题。实际上我错误地读了POST请求数据。以下是使用MultipartParserthis link

中的示例读取多个文件上传请求的正确方法
import com.oreilly.servlet.multipart.FilePart;
import com.oreilly.servlet.multipart.MultipartParser;
import com.oreilly.servlet.multipart.ParamPart;
import com.oreilly.servlet.multipart.Part;
// and other required classess
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    if (!ServletFileUpload.isMultipartContent(request)) {
        throw new IllegalArgumentException("Request is not multipart, please 'multipart/form-data' enctype for your form.");
    }

    MultipartParser mp = new MultipartParser(request, 10*1024*1024); //10MB
    Part part;
    while ((part = mp.readNextPart()) != null) {
    String name = part.getName();
    if (part.isParam()) {
      // it's a parameter part
      ParamPart paramPart = (ParamPart) part;
      String value = paramPart.getStringValue();
      fileWriter.println("param; name=" + name + ", value=" + value);
    }
    else if (part.isFile()) {
      // it's a file part
      FilePart filePart = (FilePart) part;
      String fileName = filePart.getFileName();
      if (fileName != null) {
        // the part actually contained a file
        long size = filePart.writeTo(new File("/path to save directory"+filePart.getFileName()));
        System.out.println("file; name=" + name + "; filename=" + fileName +
          ", filePath=" + filePart.getFilePath() + 
          ", content type=" + filePart.getContentType() + 
          ", size=" + size);
      }
      else { 
        // the field did not contain a file
        System.out.println("file; name=" + name + "; EMPTY");
      }
    }
  }
}