Setting file size restrictions when uploading files with Jersey

后端 未结 5 1849
孤独总比滥情好
孤独总比滥情好 2021-01-04 08:42

I\'m currently implementing functionality for uploading files using jersey rest. I would like to set a maximum allowed file size which to me seems like a pretty common requi

相关标签:
5条回答
  • 2021-01-04 08:59

    If you're using tomcat you can set the the size threshold at which the file will be written to the disk to a sensible value for your machine.

    e.g. if the servlet is in web.xml

    <servlet>
      <servlet-name>Upload Servlet</servlet-name>
      <servlet-class>YourServletName</servlet-class>
    
      <multipart-config>
       <!-- 10MB of files -->
       <max-file-size>10485760</max-file-size>
       <!-- 10KB of form data -->
       <max-request-size>10240</max-request-size>
       <!-- Buffer to disk over 512KB -->
       <file-size-threshold>524288</file-size-threshold>
     </multipart-config>
    
    </servlet>
    

    or using annotations:

    @MultipartConfig(
        maxFileSize=10485760,     // 10Mb max
        fileSizeThreshold=524288, //512 Kb before buffering to disk
        maxRequestSize=10240      // 10Kb of meta data
        location=/tmp             // with permission to write, default uses tomcat tmp
    )
    

    With reference to HttpRequest maximum allowable size in tomcat?

    0 讨论(0)
  • 2021-01-04 09:02

    If the client does not send the file size, fall back to reading the file from stream. Once you hit the size limit, stop reading and refuse the file. You should do this anyway, since you can't trust the client (anyone can create an app that sends http requests to your service and those requests may not have correct data you expect - so have to take that into account).

    In addition, it may be possible to add some validation to the web form as well to fail fast, but I am not a JavaScript expert, so not sure if/how that can be done.

    0 讨论(0)
  • 2021-01-04 09:02

    You can get the request size by reading a header. In your example:

    @POST
    @Path("uploadFile/")
    @Consumes("multipart/form-data")
    @Produces("text/html")
    public String handleDocumentUpload(
        @HeaderParam("content-length") long contentLength,
        @FormDataParam("file") InputStream uploadedInputStream,
        @FormDataParam("file") FormDataContentDisposition fileDetail) {
    
        if(contentLength > MAX_FILE_SIZE) {
          throw new IllegalArgumentException(
                "File is to big! Max size is " + MAX_FILE_SIZE);
        }
      // ...more file handling logic
    }
    
    0 讨论(0)
  • 2021-01-04 09:17

    You can have your custom class LimitedSizeInputStream extends InputStream with @Override read methods which check for specific size limit as mentioned on https://stackoverflow.com/a/30072143/5962766. Wrap your InputStream with new LimitedSizeInputStream(fileStream, FILE_SIZE_LIMIT) and you'll get exception when read limit is reached.

    0 讨论(0)
  • 2021-01-04 09:18

    You can check the length, in bytes, of the request body and made available by the input stream with the following code:

    public Response uploadFile(@Context final HttpServletRequest request, @FormDataParam("uploadFile") InputStream uploadedInputStream,
          @FormDataParam("uploadFile") FormDataContentDisposition fileDetail, @FormDataParam("uploadFile") FormDataBodyPart body) {
    

    The key part being @Context final HttpServletRequest request. Then in the method body, you can get the length of the inputstream and react to it accordingly with:

    int contentLength = request.getContentLength();
    
    if (contentLength == -1 || contentLength > MAX_REQUEST_SIZE) {
      // deal with it
    }
    
    0 讨论(0)
提交回复
热议问题