SpringBoot从别的服务器上下载单个⽂件以及多个⽂件打成压缩
包的形式
/**
* 单个⽂件下载
*/
@RequiresPermissions("system:appraising:detail")
@GetMapping("/downloadOneFile/{fileId}")
public void fileDownload(@PathVariable("fileId") Long fileId,HttpServletResponse response, HttpServletRequest request) throws Exception {
UserAttachVo userAttachVo;
if(null != fileId) {
userAttachVo = iUserAttachService.findUserAttachByFileId(fileId);
// ⽂件的名称
String downloadFilename = FileName();
BufferedOutputStream os = new OutputStream());
try {
// 转换中⽂否则可能会产⽣乱码
downloadFilename = de(downloadFilename, "UTF-8");
// 指明response的返回对象类型
response.setHeader("Content-type", FileType());
// 设置在下载框默认显⽰的⽂件名
response.setHeader("Content-Disposition","attachment;filename=" + downloadFilename);
// 去服务器上下载⽂件并下载
HttpURLConnection conn = (HttpURLConnection) new FilePath()).openConnection();
conn.setReadTimeout(6000);
conn.setConnectTimeout(6000);
conn.setRequestMethod("GET");
BufferedInputStream fis = null;
if (ResponseCode() == HttpURLConnection.HTTP_OK) {
fis = new InputStream());
}
byte[] buffer = new byte[1024];
int r = 0;
if(null != fis) {
while ((r = ad(buffer)) != -1) {
os.write(buffer, 0, r);
os.flush();
}
fis.close();
}
} catch (Exception e) {
<("下载⽂件失败", e);
} finally {
os.flush();
os.close();
}
}
}
/**
* ⽂件打成压缩包下载
*/
@RequiresPermissions("system:appraising:detail")
@GetMapping("/downloadFile")
public void downloadFile(String appraisingId, String appraisingName, String userName, HttpServletResponse response, HttpServletRequest request) throws Exception {        List<UserAttachVo> userAttachVos;
if(null != appraisingId) {
connect下载
ZipOutputStream zos = new OutputStream());
try {
userAttachVos = iUserAttachService.findUserAttachByAppraisingId(Long.valueOf(appraisingId));
// ⽂件的名称
String downloadFilename = appraisingName + userName + ".zip";
// 转换中⽂否则可能会产⽣乱码
downloadFilename = de(downloadFilename, "UTF-8");
// 指明response的返回对象是⽂件流
response.setContentType("application/octet-stream");
// 设置在下载框默认显⽰的⽂件名
response.setHeader("Content-Disposition","attachment;filename=" + downloadFilename);