修复下载找不到路径问题

master
whb 8 months ago
parent 36ce1288ce
commit 431085bb1c

@ -264,12 +264,12 @@ public class JupyterController {
@AnonymousAccess
@ApiOperation("python下载生成文件")
@GetMapping("downloadFileByPython")
public void downloadFileByPython(@RequestParam String caseName, @RequestParam String name, HttpServletResponse response) {
public void downloadFileByPython(@RequestParam String name, HttpServletResponse response) {
//存在就下载
String path = "/usr/local/tianzeProject/financial_bigdata_total/py/data/";
iFileUtil.download(response, caseName, path+name);
iFileUtil.downloadByOtherFile(response, name, path+name);
}

@ -84,4 +84,8 @@ public interface IFileUtil {
*/
String getDiskRelativePath(String fileName, String suffix);
void downloadByOtherFile(HttpServletResponse response,String name,String filePath);
}

@ -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();
}
}
}
}

Loading…
Cancel
Save