Configure related content in yml
spring: #404 mvc: throw-exception-if-no-handler-found: true #static resources static-path-pattern: /** resources: #static resources static-locations: file:${img.location}
When we configure 404 exceptions, the configuration added should be turned off (that is to say, if you want to use static resources, you must give up the unified capture of 404 exceptions. The solution is: http://blog.springcloud.cn/sc/wk2/)
spring: #404 mvc: throw-exception-if-no-handler-found: true resources: add-mappings: false
upload files:
front end:
<form enctype="multipart/form-data" id="uploadForm"> <input type="file" name="uploadFile" id="upload_file" style="margin-bottom:10px;"> <input type="button" id="uploadPicButton" value="上传" onclick="uploadImage()"> </form> var pic = $ ( ' #upload_file ' ) [ 0 ] .files [ 0 ]; var fd = new FormData (); var data = { header:{ token:"1212", reqOrg:"1212" }, body :{ spaceClassId:1 } } //fd.append('uploadFile', pic); fd.append('file', pic); fd.append( ' data ' , JSON.stringify(data)); //This maintains a restful form $.ajax({ url:'/resources/insertResources', type:"post", // Form数据 data: fd, cache: false, contentType: false , //Can't use json anymore processData: false, success:function(data){ console.log("ddd") } });
Back-end (using @RequestParam("file") to accept parameters, not using @RequestBoby to accept characters, my plan is to pass @RequestParam and then transfer beans by myself
@PostMapping("/insertResources") @ResponseBody public SuccessResponse<Object> insertResources(@RequestParam("file") MultipartFile file, @RequestParam("data") String dataRequest) { DataRequest<SpaceClassHomework> spacePicDataRequest = JSON.parseObject(dataRequest, new TypeReference<DataRequest<SpaceClassHomework>>() { }); resourcesService.insertResources(file, spacePicDataRequest.getBody()); return new SuccessResponse<Object>(null); }
New method on the server side
file.transferTo (dest);
@Override public void insertResources(MultipartFile file, SpaceClassHomework spaceClassHomework) { // Get the file name String fileName = file.getOriginalFilename(); // Get the suffix name of the file String suffixName = fileName.substring(fileName.lastIndexOf( " . " )) ; String filePath = uploadDir; File dest = new File(filePath + fileName); try { file.transferTo (dest); } catch (IOException e) { e.printStackTrace (); log.error( " File upload failed " ); } spaceClassHomework.setSpaceClassFileName(fileName); spaceClassHomework.setSpaceClassFilePath(filePath); spaceClassHomeworkMapper.insertSelective(spaceClassHomework); }
download
Front end: little impact
rear end:
1. Can't be the same as usual, add @responseBody
2. When the browser outputs the name, the encoding is not utf-8, but java is utf-8 again. All I use
String fileNameTemp = new String(fileName.getBytes("UTF-8"), "ISO8859-1");
set two variables, one for browser output, one for java operation
3 , spring method,
new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED);
@GetMapping("/getResources") public ResponseEntity<byte[]> getSpaceClass(@RequestParam("spaceClassFileId") String spaceClassFileId , HttpServletResponse resp) throws Exception { String filePath = uploadDir; // Get the path and file name String fileName = resourcesService.getResources(Long.parseLong(spaceClassFileId)); String fileNameTemp = new String(fileName.getBytes("UTF-8"), "ISO8859-1"); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); headers.setContentDispositionFormData("attachment", fileNameTemp); File file = new File(filePath + fileName); return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED);