|
|
|
@ -173,4 +173,38 @@ public class LocalFileUtil implements IFileUtil{
|
|
|
|
|
throw new IllegalArgumentException("上传文件失败,IO错误");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void downloadByOtherFile(HttpServletResponse response, String name,String relativePath) {
|
|
|
|
|
Assert.hasText(relativePath, "路径为空");
|
|
|
|
|
System.out.println("------------------------------------" + relativePath);
|
|
|
|
|
File file = new File(relativePath);
|
|
|
|
|
Assert.isTrue(file.exists(), "附件不存在");
|
|
|
|
|
String sp = relativePath.substring(relativePath.lastIndexOf(".") + 1);
|
|
|
|
|
|
|
|
|
|
String substring = name.substring(0, name.lastIndexOf("."));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
InputStream fis = null;
|
|
|
|
|
try {
|
|
|
|
|
response.setContentType("application/octet-stream");
|
|
|
|
|
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode((substring + "." + sp), CharsetUtil.UTF_8));
|
|
|
|
|
fis = new BufferedInputStream(Files.newInputStream(file.toPath()));
|
|
|
|
|
response.addHeader("Content-Length", String.valueOf(fis.available()));
|
|
|
|
|
byte[] buff = new byte[4096];
|
|
|
|
|
int len;
|
|
|
|
|
while ((len = fis.read(buff)) != -1) {
|
|
|
|
|
response.getOutputStream().write(buff, 0, len);
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
throw new IllegalArgumentException("下载文件失败: " + e.getMessage());
|
|
|
|
|
} finally {
|
|
|
|
|
try {
|
|
|
|
|
if (fis != null) fis.close();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|