Spring 引导中的绝对路径
Absolute path in Spring Boot
我正在使用 Spring Boot,我创建了一个用于照片上传的 Web 服务。
为了保存文件,我使用了绝对路径“/uploads”,但它表示该位置不存在,所以我使用 java 路径、src/...
在本地服务器上,这工作得很好,但在 heroku 服务器上托管后,图像无法上传。添加元素后图片src不存在
这是网络服务:
//image /////////////////////////////////////////////////////////////////////
private final Logger log = LoggerFactory.getLogger(this.getClass());
@PostMapping("/")
@RequestMapping(value="/transfererImage", method = RequestMethod.POST)
public void transfererImage(@RequestParam("file") MultipartFile file, @RequestParam("nom") String nom) throws Exception{
String nomFichier="";
try {
nomFichier = nom;
byte[] bytes = file.getBytes();
ClassLoader classLoader = getClass().getClassLoader();
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream (new FileOutputStream(new File("src/main/resources/static/images/"+nomFichier)));
bufferedOutputStream.write(bytes);
bufferedOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
log.info("une erreur est produite lors de la lecture de fichier"+ nomFichier);
}
}
谢谢
我用这个方法解决了这个问题:
System.getProperty("user.dir"))
它 returns 项目的根文件夹。
谢谢。
我正在使用 Spring Boot,我创建了一个用于照片上传的 Web 服务。 为了保存文件,我使用了绝对路径“/uploads”,但它表示该位置不存在,所以我使用 java 路径、src/... 在本地服务器上,这工作得很好,但在 heroku 服务器上托管后,图像无法上传。添加元素后图片src不存在
这是网络服务:
//image /////////////////////////////////////////////////////////////////////
private final Logger log = LoggerFactory.getLogger(this.getClass());
@PostMapping("/")
@RequestMapping(value="/transfererImage", method = RequestMethod.POST)
public void transfererImage(@RequestParam("file") MultipartFile file, @RequestParam("nom") String nom) throws Exception{
String nomFichier="";
try {
nomFichier = nom;
byte[] bytes = file.getBytes();
ClassLoader classLoader = getClass().getClassLoader();
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream (new FileOutputStream(new File("src/main/resources/static/images/"+nomFichier)));
bufferedOutputStream.write(bytes);
bufferedOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
log.info("une erreur est produite lors de la lecture de fichier"+ nomFichier);
}
}
谢谢
我用这个方法解决了这个问题:
System.getProperty("user.dir"))
它 returns 项目的根文件夹。
谢谢。