https://blog.csdn.net/qq_35867245/article/details/84325385
上傳附件(服務端代碼)
第一步:在application.yml中配置附件要上傳的路徑(此路徑也是下載的路徑)
***:windows路徑和linux的路徑是不同的,定義路徑時要仔細(存放路勁自己定義即可)
第二步:在服務端要調用接口所在的類(一般為控制層controller)定義一個變量
第三步:上傳附件的代碼:
本人遇見的坑:部署時vue+springboot分離部署
linux中的文件上傳路徑是java容器內部的路徑,要進入容器內部才能看到,
進入java容器內部命令時
docker exec -it 容器名 bash
下載附件
ExcelReaderUtil工具public static void download(String path, HttpServletResponse response) {
try {
if(StringUtils.isNotBlank(path)){
File file = new File(path);
// 取得文件名。
String fileName = file.getName();
// 以流的形式下載文件。
InputStream fis = new BufferedInputStream(new FileInputStream(path));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
String uncod=URLDecoder.decode(fileName,"UTF-8");
fileName = new String(uncod.getBytes("UTF-8"), "iso-8859-1");
response.setHeader("Content-Disposition", "attachment;filename=".concat(String.valueOf(fileName)));
// 設置response的Header
response.addHeader("Content-Length", "" + file.length());
OutputStream toClient = new BufferedOutputStream(
response.getOutputStream());
toClient.write(buffer);
toClient.flush();
toClient.close();
}
// path是指欲下載的文件的路徑。
} catch (IOException ex) {
ex.printStackTrace();
}
}
---------------------