微服务框架下,简单的参数传递比较简单。但是要实现文件的上传下载,还是花费了我一天的时间。记录一下。
一、文件上传
1、服务提供者
1.1 提供文件上传接口
/**
* @Description: 知识库文件上传
* @Author: ykbian
* @Date: 2020/4/11 23:00
* @Param:
* @return: 不报错就是成功
*/
@ResponseBody
@RequestMapping(value = "/uploadTemplate", method = RequestMethod.POST)
public String uploadTemplate(@RequestPart(value = "file") MultipartFile file) {
return fileService.upload(file);
}
1.2 具体实现
/**
* 文件地址
*/
@Value("${com.hld.fileAddress}")
public String fileAddress;
@Override
public String upload(MultipartFile file) {
JSONObject jsonObject = new JSONObject();
// 获得原始文件名
String fileName = file.getOriginalFilename();
// 截取文件类型; 这里可以根据文件类型进行判断
String fileType = fileName.substring(fileName.lastIndexOf('.'));
try {
// 截取上传的文件名称
String newFileName = customFileName(fileName);
// 拼接上传文件位置
String newfilePath = fileAddress + File.separatorChar + newFileName + fileType;
// 创建文件存放路径实例
File dest = new File(fileAddress);
// 判断文件夹不存在就创建
if (!dest.exists()) {
dest.mkdirs();
}
// 创建文件实例
File uploadFile = new File(newfilePath);
// 判断文件已经存在,则删除该文件
if (uploadFile.exists()) {
uploadFile.delete();
}
// 利于spring中的FileCopyUtils.copy()将文件复制
FileCopyUtils.copy(file.getBytes(), uploadFile);
jsonObject.put("result","success");
jsonObject.put("fileName",newFileName+fileType);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
jsonObject.put("result","fail");
}
return jsonObject.toJSONString();
}
2、服务消费者
2.1、引入依赖包
<!-- jar包尽量用最新版本的 免得入坑 -->
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form</artifactId>
<version>3.8.0</version>
</dependency>
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form-spring</artifactId>
<version>3.8.0</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
2.2、配置Encoder
@Configuration
class MultipartSupportConfig {
@Bean
public Encoder feignFormEncoder() {
return new SpringFormEncoder();
}
}
这个配置写在哪里都可以,只要最终在Fegin客户端引入即可。
2.3、Fegin实现接口
2.3.1 在@FeginClient 引入前面的配置
@FeignClient(name = "server",configuration = SolutionServer.MultipartSupportConfig.class)
2.3.2 实现上传接口
@PostMapping(value = "/uploadTemplate",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
String uploadTemplate(@RequestPart(value = "file") MultipartFile file);
二、文件下载
1、服务提供者
@RequestMapping(value = "/templateDownload", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
public void templateDownload(HttpServletResponse response) throws FileNotFoundException {
fileService.download(response);
}
2、服务消费者
2.1 、FeginClient 接口实现
@RequestMapping(value = "/templateDownload",method = RequestMethod.POST,consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
Response downloadFile();
2.2 、Contrller调用
@GetMapping("/download")
public ResponseEntity<byte[]> downFile() {
ResponseEntity<byte[]> result = null;
InputStream inputStream = null;
try {
// feign文件下载
Response response = solutionServer.downloadFile();
Response.Body body = response.body();
inputStream = body.asInputStream();
byte[] b = new byte[inputStream.available()];
inputStream.read(b);
HttpHeaders heads = new HttpHeaders();
heads.add(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=SolutionTemplate.xlsx");
heads.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
result = new ResponseEntity<byte[]>(b, heads, HttpStatus.OK);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}