服务提供端这里结合的是minio文件服务器:
controller,这里一定是void,不要指定返回内容
@RequestMapping("/file")
@RestController
public class FileSystemController {
@Autowired
private FileSystemService fileSystemService;
@GetMapping("/download")
public void getDownloadInput(@RequestParam(name = "bucketName") String bucketName, @RequestParam(name = "fileName") String fileName, HttpServletResponse response){
try {
fileSystemService.download(bucketName, fileName, response);
}catch (Exception e){
return;
}
}
}
service
@Override
public void download(String bucketName, String fileName, HttpServletResponse response) throws Exception {
response.reset();
if (StringUtils.isEmpty(bucketName)) {
response.sendError(500, "桶名称为空");
return;
}
InputStream inputStream = null;
BufferedInputStream bufferedInputStream = null;
OutputStream outputStream = null;
try {
// 设置ContentType,响应内容为二进制数据流,编码为utf-8,此处设定的编码是文件内容的编码
response.setContentType("application/octet-stream;charset=utf-8");
// 以(Content-Disposition: attachment; filename="filename.jpg")格式设定默认文件名,设定utf编码,此处的编码是文件名的编码,使能正确显示中文文件名
response.setHeader("Content-Disposition", "attachment;fileName=" + fileName + ";filename*=utf-8''" + URLEncoder.encode(fileName, "utf-8"));
inputStream = fileSystemUtil.getObject(bucketName, fileName);
//使用这个buffereInputStream 为了防止大文件内存溢出
bufferedInputStream = new BufferedInputStream(inputStream);
outputStream = response.getOutputStream();
//每次取出5M
byte[] buffer = new byte[1024 * 1024 * 5];
int len;
while ((len = bufferedInputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}
} catch (Exception e) {
log.error("下载文件异常:{}", fileName, e);
} finally {
if (inputStream!=null){
inputStream.close();
}
if (outputStream!=null){
outputStream.close();
}
if (bufferedInputStream!=null){
bufferedInputStream.close();
}
}
}
消费者端:
feginClint端
@FeignClient("filesystem")
public interface FileSystemFeginClient {
@GetMapping("/filesystem/file/download")
Response getDownloadInput(@RequestParam(name = "bucketName") String bucketName, @RequestParam(name = "fileName") String fileName);
}
controller使用
@GetMapping("/download")
public void getDownloadInput(@RequestParam(name = "bucketName") String bucketName, @RequestParam(name = "fileName") String fileName, HttpServletResponse response){
Response responseFe = fileSystemFeginClient.getDownloadInput(bucketName,fileName);
response.reset();
InputStream inputStream = null;
BufferedInputStream bufferedInputStream = null;
OutputStream outputStream = null;
try {
// 设置ContentType,响应内容为二进制数据流,编码为utf-8,此处设定的编码是文件内容的编码
response.setContentType("application/octet-stream;charset=utf-8");
// 以(Content-Disposition: attachment; filename="filename.jpg")格式设定默认文件名,设定utf编码,此处的编码是文件名的编码,使能正确显示中文文件名
response.setHeader("Content-Disposition", "attachment;fileName=" + fileName + ";filename*=utf-8''" + URLEncoder.encode(fileName, "utf-8"));
inputStream = response.body().asInputStream();
//使用这个buffereInputStream 为了防止大文件内存溢出
bufferedInputStream = new BufferedInputStream(inputStream);
outputStream = responseOut .getOutputStream();
//每次取出5M
byte[] buffer = new byte[1024 * 1024 * 5];
int len;
while ((len = bufferedInputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}
} catch (Exception e) {
log.error("下载文件异常:{}", fileName, e);
} finally {
if (inputStream!=null){
inputStream.close();
}
if (outputStream!=null){
outputStream.close();
}
if (bufferedInputStream!=null){
bufferedInputStream.close();
}
} }