【问题标题】:Absolute path in Spring BootSpring Boot 中的绝对路径
【发布时间】: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


【解决方案1】:

我用这个方法解决了这个问题:

System.getProperty("user.dir"))

它返回项目的根文件夹。

谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-12
    • 2016-03-13
    • 2011-06-15
    • 2011-02-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多