微服务框架下,简单的参数传递比较简单。但是要实现文件的上传下载,还是花费了我一天的时间。记录一下。
/**
* @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);
}
/**
* 文件地址
*/
@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();
}
io.github.openfeign.form
feign-form
3.8.0
io.github.openfeign.form
feign-form-spring
3.8.0
commons-fileupload
commons-fileupload
1.4
@Configuration
class MultipartSupportConfig {
@Bean
public Encoder feignFormEncoder() {
return new SpringFormEncoder();
}
}
这个配置写在哪里都可以,只要最终在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);
@RequestMapping(value = "/templateDownload", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
public void templateDownload(HttpServletResponse response) throws FileNotFoundException {
fileService.download(response);
}
@RequestMapping(value = "/templateDownload",method = RequestMethod.POST,consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
Response downloadFile();
@GetMapping("/download")
public ResponseEntity downFile() {
ResponseEntity 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(b, heads, HttpStatus.OK);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}