Java file upload and MultipartFile ServletFileUpload

JAVA file upload ServletFileUpload

Reference this: https://www.cnblogs.com/liuyangv/p/8298997.html 

Java file upload MultipartFile

1. Configure MultipartResolver

 

defaultEncoding = "UTF-8" encoding format is requested, default-8859-1 ISO
maxUploadSize = "1048576" is the upload file size in bytes
uploadTempDir = "fileUpload / temp" temporary file upload path

2. Reception Form

1 <form enctype="multipart/form-data" id="J_uploadLocalImgForm" method="post">
2     <input type="hidden" name="authVenderId">
3     <input type="file" class="hide" id="J_uploadLocalImgFile" name="multipartFile" multiple="">
4 </form>

Written on the label on multiple input this time you can select multiple files to upload, remove not support multi-select the

3. js file upload control verification format 

In general, then it would no longer be directly js to the end to determine what format, but this is not accurate, if the file extension is get rid of it? If you are demanding file format, then it is best done in the background of a more rigorous verification.

var urlType = urlStr.substring(urlStr.lastIndexOf(".") + 1, urlStr.length);
urlType = urlType.toLowerCase();
if (urlType == "jpg" || urlType == "png" || urlType == "jpeg" || urlType == "gif" || urlType == "bmp") {}

4.java Code

Controller layer
   /**
     * 本地图片上传
     * @param multipartFile multipartFiles
     * @return CallbackResult<String>
     */
    @RequestMapping("/multipleUploadWatermarkImg")
    public @ResponseBody CallbackResult<?> multipleUploadWatermarkImg(@RequestBody MultipartFile[] multipartFile){
       return service.multipleUploadWatermarkImg(multipartFile);
    }

Here is a place, behind MultipartFile added this [] array representing Well, supports multiple file upload, if that is a single file, can be removed, and the front desk is in front of the corresponding

Service layer do not write here, it is to multipartFile file for processing, if it is more than one file on the first cycle in the process, here to write a few ways MultipartFile

  • Path getOriginalFilename () file
  • getName () Returns the name of the form parameter.
  • getInputStream () returns a content InputStream to read from a file. This method can be used to obtain flow
  • getContentType () Returns the content type of the file
  • getSize () in bytes, the file size returned
  • Whether isEmpty () Returns the uploaded file is empty, i.e., do not select any file in the multi-part form, or selected file is empty.
  • transferTo will receive the file transfer to a given object file

transferTo here is very easy to use, you can save the file directly to the development path multipartFile.transferTo (new File (path))  

ServletFileUpload problems and conflicts MultipartFile

The problem is that there is both a while, which will not obtain a use when uploading files, the Internet was given a solution, but I do not have to verify,

http://www.itkeyword.com/doc/8187524065876327482/ServletFileUpload-MultipartResolver-javaSpring

 

There is also a small problem I encountered when uploading a file is too large how to do here?

https://bbs.csdn.net/topics/392015065?locationNum=9&fps=1

Here someone gave a method to capture the exception, but I think a little too much trouble, if allowed better to be judged in the foreground

uploadLocalImgFile : function(e) {	
     var file = e.target.files[0];
     if(file.type == "image/png" || file.type == "image/jpeg" || file.type == "image/jpg" ||  file.type == "image/gif"){
        if(file.size > 1024 * 1024){
            swal("上传图片的大小不能超过1M","","warning");
            return false;
     }
   } }

  

 

Guess you like

Origin www.cnblogs.com/tmxk-qfzz/p/11368228.html