@Autowired
private Environment environment;
public void fileDownload(@RequestBody FileDownloadBo fileDownloadBo){
InputStream inputStream = null;
OutputStream outputStream = null;
try {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletResponse response = attributes.getResponse();
String fileDownloadUrl = environment.getProperty("file.download.url");
URL url = new URL(fileDownloadUrl+fileDownloadBo.getFilePath());
java.net.HttpURLConnection connection = (java.net.HttpURLConnection) url.openConnection();
inputStream = connection.getInputStream();
outputStream = response.getOutputStream();
response.setContentType("application/x-download");
response.addHeader("Content-Disposition", "attachment;filename="+fileDownloadBo.getFileName());
IOUtils.copy(inputStream, outputStream);
} catch (Exception e) {
throw new ServiceException("文件下载异常");
}finally {
try {
if(inputStream!=null) {
inputStream.close();
}
if(outputStream!=null) {
outputStream.close();
}
} catch (IOException e) {
throw new ServiceException("IO关闭异常");
}
}
}