Programmatic access to MultiPartConfig in Servlet

2019-03-29 05:27发布

I use Servlet 3 @MultiPartConfig annotation to implement file upload in my application. I need set multipart-config location parameter at the runtime (not hardcode in annotaion parameter). Is there any API for programmatic access to multipart-config of servlet?

Thanks

2条回答
贪生不怕死
2楼-- · 2019-03-29 06:01

I had your same problem too and the solution is simple: standard Servlet 3.0 file upload is not enough: just grab the jars from Apache FileUpload Commons and you're done

Just look at this crystal clear example with Streaming API

   ServletFileUpload upload = new ServletFileUpload();

   // Parse the request
    FileItemIterator iter = upload.getItemIterator(request);
    while (iter.hasNext()) {
      FileItemStream item = iter.next();
      String name = item.getFieldName();
      InputStream stream = item.openStream();
      if ( item.isFormField() == false) 
          System.out.println("File field " + name + " with file name "
            + item.getName() + " detected."); 
          FileOutputStream fos = new FileOutputStream("your_location"); 
          Streams.copy ( stream,  fos, true);

       }
    }
查看更多
Emotional °昔
3楼-- · 2019-03-29 06:23

The @MultiPartConfig is really just a marker interface for the container. When the servlet is initialized the annotation values provided are mapped to it with a proxy object. When the incoming request is a multipart/form-data the parts of the upload are mapped to the request, and the container performs the necessary work based upon the values from the annotation and the parts on the request. There is no way for you to intercept this process since it all happens within the guts of the container. However, there is one alternative. It requires performing a file system operation a second time. Since you have all the parts you can reconstruct the file and "re-upload" it to the location of your choice. It might look something like the method below. Keep in mind although I tested this quickly in a servlet of my own to demonstrate the concept it is obviously not finished code:

@Override
protected void doPost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
        throws ServletException, IOException {

    httpServletResponse.setContentType("text/html");
    PrintWriter printWriter = httpServletResponse.getWriter();

    InputStream inputStream;
    FileOutputStream fileOutputStream;

    for (Part part : httpServletRequest.getParts()) {

        inputStream = httpServletRequest.getPart(part.getName()).getInputStream();
        int i = inputStream.available();
        byte[] b = new byte[i];
        inputStream.read(b);
        String fileName = "";

        for (String temp : part.getHeader("content-disposition").split(";")) {
            if (temp.trim().startsWith("filename")) {
                fileName = temp.substring(temp.indexOf('=') + 1).trim().replace("\"", "");
            }
        }

        String uploadDir = "/temp";
        fileOutputStream = new FileOutputStream(uploadDir + "/" + fileName);
        fileOutputStream.write(b);
        inputStream.close();
        fileOutputStream.close();

        printWriter.write("Uploaded file " + uploadDir + "/" + fileName + ".");
    }
}
查看更多
登录 后发表回答