store file in spring boot resource folder after deployment - java

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

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 :).

Related

How to write and read file in spring boot

I have a problem with saving files and then downloading them after generating a .war file.
I need to handle the generation of many files after pressing the button by admin in the application. The files are generated using part of the code that was sent using the POST method and second part is from the database.
The files are hundreds / thousands and it is impossible to do it manually. Admin generates files from time to time. The user should be able to download these files from the application.
When I run the application in IntelliJ, app has access to the folders on the disk, so the following code works:
(part of backend class, responfible for saving files in path)
private void saveTextToFile(String text, String fileName) {
String filePathAndName = "/static/myFiles/" + fileName+ ".txt";
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource(".").getFile() + filePathAndName );
FileWriter fileWriter = null;
try {
fileWriter = new FileWriter(file);
PrintWriter printWriter = new PrintWriter(fileWriter);
printWriter.print(text);
printWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
The file was saved in folder:
C:\Users...\myProject\target\classes\static.
(and this is link to generated file in thymeleaf)
<html xmlns:th="http://www.thymeleaf.org">
<a th:href="#{|/myFiles/${thisIsMyFileName}|}">Download file</a>
</html>
Unfortunately, when I generate the .war file and run it, the files are not saved in the application's "resources" folder. As a result, the user cannot download this file via the link generated by thymeleaf.
In general, you do not want to upload anything into your application's files - it opens you to many security problems if someone figures out how to overwrite parts of the application, and in most application servers, it is simply not writable.
A much better approach is to have a designated server folder where you can write things. For example, you could have the following in your configuration:
myapp.base-folder = /any/server/folder/you/want
And then, in the code, you would find that folder as follows:
// env is an #AutoWired private Environment
File baseFolder = new File(env.getProperty("myapp.base-folder"));
I find this better than using a database (as #Stultuske suggested in comments), because databases are great for relations, but mostly overkill for actual files. Files can be accessed externally without firing up the database with minimal hassle, and having them separate keeps your database much easier to backup.
To generate links to the file, simply create a link as you would to any other type of request
<a th:href="#{/file/${fileId}|}">Download file</a>
-- and to handle it in the server, but returning the contents of the file:
#GetMapping(value="/file/{id}")
public StreamingResponseBody getFile(#PathVariable long id) throws IOException {
File f = new File(baseFolder, ""+id); // numerical id prevents filesytem traversal
InputStream in;
if (f.exists()) {
in = new BufferedInputStream(new FileInputStream(f));
} else {
// you could also signal error by returning a 404
in = new BufferedInputStream(getClass().getClassLoader()
.getResourceAsStream("static/img/unknown-id.jpg"));
}
return new StreamingResponseBody() {
#Override
public void writeTo(OutputStream os) throws IOException {
FileCopyUtils.copy(in, os);
}
};
}
I prefer numerical IDs to avoid hassles with path traversal - but you can easily use string filenames instead, and deal with security issues by carefully checking that the canonical path of the requested file starts with the canonical path of your baseFolder

Java - Get file from jar

I need to catch some directory within the application. For that I have a small demonstration:
String pkgName = TestClass.class.getPackage().getName();
String relPath = pkgName.replace(".", "/");
URL resource = ClassLoader.getSystemClassLoader().getResource(relPath);
File file = new File(resource.getPath());
System.out.println("Dir exists:" + file.exists());
While running application from IDE I receive my goal and I can find my directory. But running application as JAR file, does not return a valid "file" (from Javas perspective) and my sout gives me back File exists:false. Is there some way to get this file? In this case, the file is a directory.
Java ClassPath is an abstraction that differs from a filesystem abstraction.
A classpath element may exist in two physical ways:
exploded with classpath pointing to the root directory
packed in a JAR archive
Unfortunatelly, the file.getPath does return a File object if classpath is pointing to file system but it does not if you refer to a JAR file.
In 99% of all cases you should read the contents of a resource using InputStream.
Here is a snippet, that uses IOUtils from apache commons-io to load the whole file content into a String.
public static String readResource(final String classpathResource) {
try {
final InputStream is = TestClass.class.getResourceAsStream(classpathResource);
// TODO verify is != null
final String content = IOUtils.toString(
is, StandardCharsets.UTF_8);
return content;
} catch (final IOException e) {
throw new UncheckedIOException(e);
}
}

Write a file in resources in a java maven project

I have to implement a temporary "persistence" solution for retrieving some definitions ( simple json strings). I have a rest endpoint which creates an instance of my object and then I want to write the json definition inside a file.
I currently have a class loader which loads files from the resources folder inside my module, I use these to grab the query results. Now I want to save a new definition, and write it to a file inside the resources folder.
However, I am unsuccessful in doing so. I grab the path from the existing file with the classloader and when I try to create a new file using FileUtils, it either throws some errors, or creates the file in D:.
Is there a way to use the resources folder to add/edit files at runtime ? Here is some code also with one of the many ways i've tried it.
public String writeDocument(String path, String content) throws IOException {
\\Example call: writeDocument("/definitions/somedefinition.txt",jsonStringHere);
\\where /definitions/somedefinition.txt is placed in /resources
ClassLoader classLoader = getClass().getClassLoader();
final URL url = classLoader.getResource(path);
final File file;
String finalPath = null;
try {
file = Paths.get(url.toURI()).toFile();
FileUtils.writeStringToFile(file, content);
finalPath = file.getAbsolutePath();
} catch (URISyntaxException e) {
e.printStackTrace();
}
return finalPath;
}

Add and use files in JAVA Jar as they would be in the folder

I have JAVA project which contains some *.sql files and on deploy if there is no database cleated yet I can run some classes which applies those *.sql files on database.
But I have to make JAVA Jar file out of it in order to deploy. I do know how to run any class from jar, but how to add and access my *.sql in the Jar if I do not want extract the files.
The *sql files that I need is being used the following way:
mysql -uroot -ppassword < databaseStructure.sql
that creates me database. somehow I need access that file out of jar when necessary.
InputStream stream = YourClass.class.getResourceAsStream("/" + pathToYourFile);
Example:
You have a file insertSomething.sql in your jar.
public static String getResourceContent(
final String path) throws IOException {
InputStream stream = YourClass.class.getResourceAsStream("/" + path);
if (stream != null) {
return IOUtils.toString(stream);
}
}
Now you can get the content:
String content = getResourceContent("insertSomething.sql");
You can get an InputStream from files in your classpath. I'm not sure if that does what you need, exactly.
See this previous SO article on the topic: Different ways of loading a file as an InputStream

Runnable JAR file not found exception

I would like to know how to create a runnable JAR with resources (pictures, pdfs) in a resource folder either inside or outside the source package (ie /src/resources/images/ or /resources/images/) in Eclipse. Currently, i have my resources inside the source folder of my eclipse project, but I've also tried it inside its own folder in the package. The program builds and executes fine in eclipse, but when I go to export as a runnable jar, I keep getting a file not found exception when I run it on the desktop. I'm declaring my files with a string like this
private String file = "src/resources/orderForm.pdf";
I understand that I should user getResourceAsStream(), but due to some constraints, I can't (has to do with how files are saved, I'm reading in whole pdf files, not as streams) so I'm wondering how to get my files into the correct location in the jar. If i unpack it after I've made it they always show up in the top level, outside of the folders. Here is a screen shot of my current project structure. For the sake of saying it, this project works fine in eclipse, also in the java build properties, the source folder is in the build path along with all subfolders, I also tried doing the same with the empty resources folder in an earlier test.
You will need to do the getResourceAsStream, and make sure that the pdfs get built into the jar. Since you have them in the source folder, they should.
Assuming you have the pdf under src/resources/orderForm.pdf, it will end up in the jar file as /resources/orderForm.pdf. You would open a resource stream for /resources/orderForm.pdf.
If you must have a honest to goodness file, then you would need code that reads the PDF as a resource stream, FileOutputStreams it out to a temp file, then uses the file. Or, you simply cannot package the pdfs in the jar.
public class PDFWriter {
public static final String testDir = "C:\\pdftest\\";
public static final String adobePath = "\"C:\\Program Files\\Adobe\\Reader 10.0\\Reader\\AcroRd32.exe\"";
public static void main(String[] args){
try {
new PDFWriter().run();
} catch (Exception e) {
e.printStackTrace();
}
}
public void run() throws Exception {
InputStream in = this.getClass().getResourceAsStream("/resources/test.pdf");
new File(testDir).mkdirs();
String pdfFilePath = testDir + "test.pdf";
FileOutputStream out = new FileOutputStream (pdfFilePath);
byte[] buffer = new byte[1024];
int len = in.read(buffer);
while (len != -1) {
out.write(buffer, 0, len);
len = in.read(buffer);
}
out.close();
Runtime rt = Runtime.getRuntime();
String command = adobePath + " " + pdfFilePath;
rt.exec(command);
}
}
The only reason the file way works at all in eclipse is because the bin folder is a real folder, but when you build the thing, it all gets zipped up into a jar.
Maybe a bit of confusion over source folders. If you have a source folder called 'src', the package structure under it does not contain "src". src/net/whatever/Class.java will turn into net/whatever/Class.class when build.
So, you could create a second source folder called 'rsrc' (resources), and under this put your resources/order.pdf. rsrc/resources/order.pdf will become resources.order.pdf when you build the jar.
For the sake of easy exporting from Eclipse without constantly thinking about it, I recommend putting the resources folder under the src folder. Long story short, if you don't put it in your src folder, every time you create a jar, you will need to check the box next to the resources folder. So just put it in the src folder and save yourself some problems.
As for referrencing the files, I would try
private String file = "resources/finished.pdf";
This allows you to access the file under src/resources/finished.pdf.

Categories

Resources