Reputation: 1935
I am wokring on spring boot, and i have created a folder web
and images
floders on this path : myApp/src/web/images
And when doing
private String saveFile(MultipartFile file, String fileName) throws IOException {
byte[] bytes = file.getBytes();
String imagePath = new String(this.servletContext.getRealPath(this.imagesPath) + "/" + fileName);
Path path = Paths.get(imagePath);
Files.write(path, bytes);
return imagePath;
}
i got this error :
java.nio.file.NoSuchFileException: /private/var/folders/4g/wd_lgz8970sfh64zm38lwfhw0000gn/T/tomcat-docbase.8255351399752894174.8098/images/IMG_2018-01-06 15:18:48.486.jpg
where i should put the images
folder in order to upload files into it successfully.
Thanks for the help
Upvotes: 0
Views: 9162
Reputation: 630
You could save images into same /myapp/src/web/image/
directory using a fairly straightforward way.
the path in your application start in myapp
directory you just need to chain the folow path /usr/web/images
and using a FileOutputStream
object to save there.
An example below.
private String saveFile(MultipartFile file,String filename) throws IOException{
final String imagePath = "src/web/images/"; //path
FileOutputStream output = new FileOutputStream(imagePath+filename);
output.write(file.getBytes());
return imagePath+filename;
}
Second Edit
if you want to get a image via GET request you can make a method that accept a Request Parameter with the name of the image and produces IMAGE CONTENT TYPE. something like that. (this sample work with a different types of images.)
@RequestMapping(value = "/show/",produces = MediaType.IMAGE_PNG_VALUE)
public @ResponseBody byte[] showImage(@RequestParam("image") String image) throws IOException{
final String imagePath = "src/web/images/";
FileInputStream input = new FileInputStream(imagePath+image);
return IOUtils.toByteArray(input);
}
I'm using apache common dependency to get byte array
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
Upvotes: 3
Reputation: 131346
i am working on spring boot, and i have created a folder web and images folders on this path : myApp/src/web/images
and this exception :
java.nio.file.NoSuchFileException: /private/var/folders/4g/wd_lgz8970sfh64zm38lwfhw0000gn/T/tomcat-docbase.8255351399752894174.8098/images/IMG_2018-01-06 15:18:48.486.jpg
At runtime the images
folder created in the application source code will not be physically located in an images
folder of the folder that hosts the application as this path : myApp/src/web/images
is probably not considered by Spring Boot as the application is started.
In Spring Boot, you can access to static resources located in some specific folders such as static
or public
.
But I am not sure it will help you as you want to not only access to uploded files but also put content in the folder.
So I advise you to use another approach : put the images in a specific folder that is distinct of the application deployment folder.
Besides, generally, you want that the files be available after a shutdown/startup of the application.
So instead of using a relative path to the application to host the images :
myApp/src/web/images
use an absolute path (prefix with /
) that is independently of the runtime folder of the application.
Upvotes: 1