简体   繁体   English

Spring HTTP文件上载:验证文件大小

[英]Spring HTTP File Upload: Validation of File Size

I've a basic question about validation of file size in a Spring based HTTP File Upload Service. 我有一个关于在基于Spring的HTTP文件上传服务中验证文件大小的基本问题。 I am using Spring's org.springframework.web.multipart.MultipartFile to perform the file upload. 我使用Spring的org.springframework.web.multipart.MultipartFile来执行文件上传。

My question is does multipartFile.getSize() method return size only after completion of entire upload? 我的问题是multipartFile.getSize()方法只在完成整个上传后返回大小吗? If yes, what's the right look ahead approach to restrict files based on size? 如果是,那么基于大小限制文件的正确前瞻方法是什么?

Note: The reason I ask this question is because I do not want an end user to upload 1 GB file and get an error message after an hour saying, "Sorry, maximum file size allowed is 2 KB." 注意:我问这个问题的原因是因为我不希望最终用户上传1 GB文件并在一小时后收到错误消息,“抱歉,允许的最大文件大小为2 KB”。

MultipartFile only check the file size in server side. MultipartFile仅检查服务器端的文件大小。 if you want to check the file size at the time of upload you need to use client side javascript or jquery . 如果你想在上传时检查文件大小,你需要使用客户端javascriptjquery javascript or jquery can be use to check the file side at the time of upload. javascriptjquery可用于在上传时检查文件端。

$('#myFile').bind('change', function() {

  //this.files[0].size gets the size of your file.
  alert(this.files[0].size);

});

I think the person do with it should be the front end programmer (javascript/h5) who should check the size before user uploading. 我认为这个人应该是前端程序员(javascript / h5)谁应该在用户上传之前检查大小。 Then the server side operator need to set some properties like "maxRequestLength" or "upload_max_filesize" for http request. 然后服务器端操作员需要为http请求设置一些属性,如“maxRequestLength”或“upload_max_filesize”。

Answer to your first question,is does multipartFile.getSize() method return size only after completion of entire upload? 回答你的第一个问题,multipartFile.getSize()方法是否只在完成整个上传后返回大小?

-No,you get multipartFile.getSize() before completion of entire upload. 不,你在完成整个上传之前得到multipartFile.getSize()。

@RequestMapping(value = "/CSVfileupload")
public @ResponseBody String callCSVFileUpload(
    @RequestParam("CSVfilepath") MultipartFile multipartFile, HttpSession session,HttpServletRequest request) throws IOException {
     //get the file size before doing any operation 
     logger.info("Controller - callCSVFileUpload() multipartFile.getSize()="+ multipartFile.getSize());
     InputStream inputStream =  new BufferedInputStream(multipartFile.getInputStream());
     JsonArray response=null;
     try{
         //you business logic 
        }
     catch(Exception e){
        e.printStackTrace();
        }
        logger.info(" response.toString()="+ response.toString());
        return response.toString();
    }//end of callCSVFileUpload

You can use CommonsMultipartResolver in you application-context.xml file. 您可以在application-context.xml文件中使用CommonsMultipartResolver。

<bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="50000000" />
</bean>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM