1

I have deployed a spring-boot application JAR file. Now, I want to upload the image from android and store it in the myfolder of resource directory. But unable to get the path of resource directory.

Error is: java.io.FileNotFoundException: src/main/resources/static/myfolder/myimage.png (No such file or directory)

This is the code for storing the file in the resource folder

private final String RESOURCE_PATH = "src/main/resources";
String filepath = "/myfolder/";

public String saveFile(byte[] bytes, String filepath, String filename) throws MalformedURLException, IOException {

File file = new File(RESOURCE_PATH + filepath + filename);
    
OutputStream out = new FileOutputStream(file);
try {
    out.write(bytes);
} catch (Exception e) {
    e.printStackTrace();
} finally {
    out.close();
}
return file.getName();

}

UPDATED:

This is what I have tried

private final String RESOURCE_PATH = "config/";
controller class: 

String filepath = "myfolder/";
String filename = "newfile.png"

public String saveFile(byte[] bytes, String filepath, String filename) throws MalformedURLException, IOException {
    
    //reading old file      
    System.out.println(Files.readAllBytes(Paths.get("config","myfolder","oldfile.png")));  //gives noSuchFileException
    
    //writing new file
    File file = new File(RESOURCE_PATH + filepath + filename);

    OutputStream out = new FileOutputStream(file);  //FileNotFoundException
    try {
        out.write(bytes);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        out.close();
    }
    return file.getName();
}

Project structure:

+springdemo-0.0.1-application.zip
   +config
     +myfolder
        -oldfile.png
     -application.properties
   +lib
     +springdemo-0.0.1.jar
   +start.sh
-springdemo-0.0.1.jar       //running this jar file

1 Answers1

5

Usually when you deploy an application (or start it using Java), you start a JAR file. You don't have a resource folder. You can have one and access it, too, but it certainly won't be src/main/resources.

When you build your final artifact (your application), it creates a JAR (or EAR or WAR) file and your resources, which you had in your src/main/resources-folder, are copied over to the output directory and included in the final artifact. That folder simply does not exist when the application is run (assuming you are trying to run it standalone).

During the build process target/ is created and contains the classes, resources, test-resources and the likes (assuming you are building with Maven; it is a little different if you build using Gradle or Ant or by hand).

What you can do is create a folder e.g. docs next to your final artifact, give it the appropriate permissions (chmod/chown) and have your application output files into that folder. This folder is then expected to exist on the target machine running your artifact, too, so if it doesn't, it would mean the folder does not exist or the application lacks the proper permissions to read from / write to that folder.

If you need more details, don't hesitate to ask.

Update: To access a resource, which is bundled and hence inside your artifact (e.g. final.jar), you should be able to retrieve it by using e.g. the following:

testText = new String(ControllerClass.class.getResourceAsStream("/test.txt").readAllBytes());

This is assuming your test.txt file is right under src/main/resources and was bundled to be directly in the root of your JAR-file (or target folder where your application is run from). ControllerClass is the controller, which is accessing the file. readAllBytes just does exactly this: read all the bytes from a text file. For accessing images inside your artifact, you might want to use ImageIO.

IF you however want to access an external file, which is not bundled and hence not inside your artifact, you may use File image = new File(...) where ... would be something like "docs/image.png". This would require you to create a folder called docs next to your JAR-artifact and put a file image.png inside of it.

You of course also may work with streams and there are various helpful libraries for working with input- and output streams.

The following was meant for AWT, but it works in case you really want to access the bytes of your image: ImageIO. In a controller you usually wouldn't want to do that, but rather have your users access (and thus download) it from a given available folder.

I hope this helps :).

Igor
  • 1,582
  • 6
  • 19
  • 49
  • Thanks. I tried this but the problem is how to access those resources files in my controller in spring. Do you have any reference from where i can get solution – bhumi vaghasiya Aug 11 '20 at 16:32
  • It is really simple: assuming you create a final artifact called `final.jar`, you copy it to the destination folder (e.g. `C:\Temp` on Windows or `/tmp` on Linux) and then create a folder named e.g. `docs`. Now you put a file into `C:\Temp\docs` (e.g. `image.png`) and then access it using `... = new File('docs/image.png')`. IF you want to include the file right into your `final.jar` (as I would do with e.g. icons), you just put them into `src/main/resources`, they are automatically copied over into your `final.jar` and you can access them using `MyClass.getResourceAsStream('image.png')`. – Igor Aug 11 '20 at 18:55
  • Thank you now i can write file to that location but again i am facing the problem for accessing file i have put in that folder. how can I get the path of this file as uri in the controller class of jar – bhumi vaghasiya Aug 12 '20 at 13:27
  • @bhumivaghasiya why do you want the path of the file to begin with? If the file is inside the JAR, it might not even have a file handler. All you need is usually the resource-stream and a way to handle it. The question is: what do you want to do with the file? Also perhaps https://stackoverflow.com/questions/941754/how-to-get-a-path-to-a-resource-in-a-java-jar-file helps you understand what I mean. – Igor Oct 27 '20 at 19:09
  • Thank you so much @Igor. Your answer helped me to resolved the error.I was stuck for 2 days. Thank you buddy for saving my life :) – Md Noorshid Jul 05 '22 at 11:28
  • @MdNoorshid glad it helped. With Spring Boot in particular there are often new ways of doing that (e.g. also using the classpath or `ClassPathResource` class for reading files). See here: https://www.baeldung.com/spring-classpath-file-access . – Igor Jul 06 '22 at 13:50
  • Thank you @Igor sharing the information. Definitely next time I will try the approach you suggested. Thank you again for making life easy :) – Md Noorshid Jul 12 '22 at 13:33