【问题标题】:write file into spring boot folder将文件写入spring boot文件夹
【发布时间】:2015-04-01 03:31:17
【问题描述】:
我的spring boot项目结构是这样的
源
|-主要
|--|-java
|--|-资源
静态
|-css
|-图片
|-js
现在我想将文件写入静态/图像文件夹
我尝试像新文件一样
BufferedOutputStream stream =new BufferedOutputStream(new FileOutputStream(new File("static/images")));它会抛出"No such file or directory" exception
但在其他 html 文件中,我可以通过 "js/jsFile.js" 获取 js
【问题讨论】:
标签:
spring-boot
relative-path
【解决方案1】:
new File("static/images")是对的
我使用了new File("/static/images"),所以我得到了一个例外
【解决方案2】:
我在使用 Spring Boot 的情况下,我必须将图像保存到一个静态访问的目录中。
以下代码完美运行
byte[] imageByteArray ....
String fileName = "image.png";
String fileLocation = new File("static\\images").getAbsolutePath() + "\\" + fileName;
FileOutputStream fos = new FileOutputStream(fileLocation);
fos.write(imageByteArray);
fos.close();
希望对您有所帮助。
【解决方案3】:
@zhuochen shen 是正确的。
发生的事情是 Eclipse 不显示写入文件。所以我查看了文件探索。文件写入正确。
try {
Path path=Paths.get("static/images/"+productDto.getImage().getOriginalFilename());
Files.write(path,productDto.getImage().getBytes());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
enter image description here
enter image description here