Micro-service file upload and download

https://blog.csdn.net/weixin_34235457/article/details/88231392

Interface definition file uploading and downloading rest
Import cn.hutool.core.io.FileUtil;
Import cn.hutool.core.util.StrUtil;
Import cn.hutool.crypto.digest.DigestUtil;

@Api (value = "document controller", tags = { "file upload and download Interface"})
@RestController
@RequestMapping ( "/ file")
public class FileController {

Private static String save_path = Final the System.getProperty ( "user.home");
/ **
* upload
*
* @ file param
* @return
* /
@ApiOperation (value = "upload")
@PostMapping ( "/ the Upload")
public String the Upload (@NotNull MultipartFile file) {
// file MD5 value
String md5File = DigestUtil.md5Hex (file.getInputStream ());
// file suffix
Extname StrUtil.subAfter = String (file.getOriginalFilename (),, to true ".");
// If the file does not exist, save, or return directly MD5 file name
File localfile = new new File (save_path, md5File)
IF (! FileUtil.exist (localfile)) {
// create a new file
file AttachFile = FileUtil.touch (save_path, md5File);
// write the file stream file
FileUtil.writeFromStream (file.getInputStream (), AttachFile);
return " file uploaded successfully "
}
return" file already exists ";
}



/ **
* Download documents
*
* @return
* /
@ApiOperation (value =" Help file downloads ")
@ApiImplicitParam (name =" fileName ", value =" filename ", dataType =" String ", paramType =" Query ")
@GetMapping (" / downloads ")
ResponseEntity downloads public (@RequestParam String fileName) {
File File = new new File (save_path, fileName);
IF (! File.Exists ()) {
return ResponseEntity.notFound () Build ();.
}
HttpHeaders new new headers HttpHeaders = () ;
headers.add ( "Cache-Control", "NO-Cache, NO-Store, the MUST-revalidate");
// Chrome browser to download files may appear: ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION,
// cause: It may be because the file name contains English commas,
// solution: Make sure the filename argument in double quotes parcel [1]
headers.add ( "Content-Disposition", "Attachment; filename = \" "+ file.getName () +" \ "") ;
headers.add ( "Pragma", "Cache-NO");
headers.add ( "the Expires", "0");
return ResponseEntity
.ok()
.headers(headers)
.contentLength(downloadModel.getDocFile().length())
.contentType(MediaType.parseMediaType("application/octet-stream"))
.body(new FileSystemResource(file));
}
}
restTemplat调用api上传
public void uploadFile(@NotNull MultipartFile file){
File newFile = FileUtil.writeFromStream(file.getInputStream(),"F:/upload/"+file.getOriginalFilename());
FileSystemResource fs =new FileSystemResource(newFile);
MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
//这里不能直接传参File或MultipartFile,需要用FileSystemResource包装一下
param.add("file",fs);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
The HttpEntity <MultiValueMap <String, Object >> HttpEntity the HttpEntity new new = <> (param, headers);
. ResponseEntity <String> = responseEntity new new RestTemplate () Exchange ( "http://cloud.test.hnlat.com/zuul/resources -server / File / Upload ", HttpMethod.POST, HttpEntity, String.class);
String fileName = responseEntity.getBody () toString ();.
}
attached:
[. 1] http://codeverge.com/embarcadero.delphi. IntraWeb / Chrome-err_response_headers_multi / 1,061,174
---------------------
author: weixin_34235457
source: CSDN
original: https: //blog.csdn.net/weixin_34235457/article/ details / 88231392
copyright: This article is a blogger original article, reproduced, please attach Bowen link!

Guess you like

Origin www.cnblogs.com/linus-tan/p/11132487.html