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);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论