spring boot jar部署后 获取相对路径下载
把想要在下载的文件放到jar包运行目录的上级目录
@RequestMapping(value = "/testDownload")
public void testDownload(HttpServletRequest request, HttpServletResponse response) {
ApplicationHome ah = new ApplicationHome(DemoApplication.class);
String docStorePath = ah.getSource().getParentFile().toString();
String fileName = "打印控件.exe";
File file = new File(docStorePath);
File parentFile = file.getParentFile();
String join = StrUtil.join("", parentFile.toString(),"\\", fileName);
try {
ServletOutputStream outputStream = response.getOutputStream();
byte[] bytes = FileUtil.readBytes(join);
response.setContentType("application/octet-stream");
fileName = new String(fileName.getBytes("UTF-8"),"ISO8859-1");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
outputStream.write(bytes);
IoUtil.close(outputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
用的相关依赖,请百度版本号
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.15</version>
</dependency>