Java无法读取docker中具有-v装载路径的文件
我试图在本地linux系统中读取文件,路径为“/home/data/storefile/12052021/0002ea4b-0742-4a05-9784-6ad6354d978c.wav”。但当我使用CLI运行docker image时: docker run-d-p 8089:9999-v/home/data/storefile:/home/data/storefile语音\u cms。 我的代码在这里:Java无法读取docker中具有-v装载路径的文件,java,linux,spring,docker,deployment,Java,Linux,Spring,Docker,Deployment,我试图在本地linux系统中读取文件,路径为“/home/data/storefile/12052021/0002ea4b-0742-4a05-9784-6ad6354d978c.wav”。但当我使用CLI运行docker image时: docker run-d-p 8089:9999-v/home/data/storefile:/home/data/storefile语音\u cms。 我的代码在这里: @GetMapping(value = "/file/{audioId}&q
@GetMapping(value = "/file/{audioId}")
public ResponseEntity<FileSystemResource> getFile(@PathVariable("audioId") String audioId) throws IOException {
System.out.println("get file");
RequestInfo requestInfo = requestInfoRepository.findFirstByAudioId(audioId);
String path = "";
if (requestInfo != null) {
path = "/" + requestInfo.getFilePath();
} else return null;
// System.out.println(path.substring(20,path.length()));
System.out.println("Path: " + path);
File file = new File(Paths.get(path).toString());
if (file.exists()) {
System.out.println("File exist");
HttpHeaders headers = new HttpHeaders();
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
headers.add("Content-Disposition", "attachment; filename=" + file.getName());
headers.add("Pragma", "no-cache");
headers.add("Expires", "0");
headers.add("Last-Modified", new Date().toString());
headers.add("ETag", String.valueOf(System.currentTimeMillis()));
return ResponseEntity.ok().headers(headers).contentLength(file.length()).contentType(MediaType.parseMediaType("application/octet-stream")).body(new FileSystemResource(file));
} else {
System.out.println("File not exist");
return null;
}
}
@GetMapping(value=“/file/{audioId}”)
公共响应属性getFile(@PathVariable(“audioId”)字符串audioId)引发IOException{
System.out.println(“获取文件”);
RequestInfo RequestInfo=requestInfoRepository.findFirstByAudioId(audioId);
字符串路径=”;
if(requestInfo!=null){
path=“/”+requestInfo.getFilePath();
}否则返回null;
//System.out.println(path.substring(20,path.length());
System.out.println(“路径:+Path”);
File File=新文件(path.get(path.toString());
if(file.exists()){
System.out.println(“文件存在”);
HttpHeaders=新的HttpHeaders();
添加(“缓存控制”,“无缓存,无存储,必须重新验证”);
headers.add(“内容处置”、“附件;文件名=“+file.getName());
添加(“Pragma”、“无缓存”);
标题。添加(“到期”、“0”);
headers.add(“上次修改”,new Date().toString());
headers.add(“ETag”,String.valueOf(System.currentTimeMillis());
返回ResponseEntity.ok().headers(headers).contentLength(file.length()).contentType(MediaType.parseMediaType(“应用程序/八位字节流”)).body(新文件系统资源(文件));
}否则{
System.out.println(“文件不存在”);
返回null;
}
}
当我运行时,文件路径为/home/data/storefile/12052021/0002ea4b-0742-4a05-9784-6ad6354d978c.wav
Java Spring show noti文件不存在