【发布时间】:2017-08-20 23:45:44
【问题描述】:
我正在使用 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);
}
}
谢谢
【问题讨论】:
-
您需要在路径前加上 file: 在 Unix 上和 file:/// 在 Windows 上。例如
file:/var/lib/或file:///c:/temp/ -
感谢您的回复,我会检查一下
-
谢谢@AndreiDamian-Fekete,问题与我使用的路径有关。
标签: web-services spring-boot file-upload upload image-uploading