My Spring Boot Application structure is this
Spring boot app structure
Within csvService.java class, I am trying to read the test.csv file. I am able to read it on localhost but when I deploy it to Google Cloud Platform, it fails to read the file giving FileNotFoundException.
This is the line of code responsible for reading the csv file:
CSVReader reader = new CSVReader(new InputStreamReader(newFileInputStream("test.csv"), "UTF8"));
The file structure is not the same where you deployed it and the test.csv file likely did not get packaged in your binary. Thus you're getting a FileNotFoundException - the file simply isn't where it was on your local machine.
Things to specify/clarify for your project:
Where and how do you deploy? Is it GKE, GCE or AppEngine? Look up for each of those how they behave when you deploy a jar file.
Why do you want to deploy a test.csv file in production? Surely you want to fetch that csv file on fly and not statically (unless it has some specific meaning) - if you do, put it in the resources folder and treat it as such. You will need to slightly alter your file reading code.
Actually the issue was in the Docker file. The image was getting created and wsa only using the jar. I needed to add the below line in the Docker file.
COPY --from=builder /app/test.csv /test.csv
Related
I want to write a file on the server, and then the browser can access it. I wrote it like this before using the war package.
request.getServletContext().getRealPath("cost.xlsx");
I use this path to successfully write files to the webapp folder.
Now I want to use the jar package, I would like to ask how can I write to the static folder of the resources resource folder, so that the browser can access the file I wrote on the server.
I have a springboot program that reads from a text file present inside the project. I use eclipse IDE. I want to share the project. While packaging the project in war format the text file was missing. Can someone tell me how do I package it so as to have the text file inside the package ?
I want to know the steps for both war and jar packaging which will include the text file in them.
This will let the user who unpacks it finds the text file and it can be used by the springboot service directly.
Thanks
Put any resources (files) used at runtime in src/main/resources. They should be available in all packages.
I am using Openshift to allocate a Java application. This app contains a method that needs to download a xml file inside the project to explore it later.
So, the thing is that I need the path for the directory where I want to download the file.
And that is the trouble, I don't know how to call the path I want.
For example:
String filePath = "webapps/downloads/file.xml";
But Openshift tells me the error:
/var/lib/openshift/56e18fa***********0029/jbossews/webapps/downloads/file.xml
(No such file or directory)
The structure of my project is:
Project
src > main
java
resources
webapp
webapps > downloads
I Hope I have explained well. Thanks in advance!
I was having an issue of reading CMYK JPEG images , and have used below url as reference for solving the issue.
http://www.randelshofer.ch/blog/2011/08/reading-cmyk-jpeg-images-with-java-imageio/
I have given the configuration org.monte.media.jpeg.CMYKJPEGImageReaderSpi in the file javax.imageio.spi.ImageReaderSpi under path /META-INF/services/javax.imageio.spi.ImageReaderSpi.
This works perfectly inside eclipse and the image reader is loaded successfully.
This file is not loading when deployed , i can find the folder and the file in the generated war file in my desired jar file inside lib folder, i guess i need to add it to java classpath.
Please help me to add to classpath or if there is any other issue with it.
You need to add this file as a static resource to your build lifecycle.
For Ant or Gradle you just need to write a simple copy task (Ant task, Gradle task), for Maven you can use Maven Resources Plugin.
After that your file should appears in your app package.
I am creating a zip file through my app deployed on apache tomcat 7. I want to make that zip available for download to the users. Where shall I place my zip on tomcat server to be available for download or Can I store my zips on some drive and map them through tomcat to be available for download?
There are 2 ways to it.
Option 1: Put your zip or any file under webapps folder e.g. /webapps/test/hello.zip. Putting zip there can be accessed by http://<host:port>/test/hello.zip
Option 2: You can map any directory on your file system accessible via tomcat. To do this you need to configure server.xml at $Tomcat_home/conf/server.xml
Add this new tag under tag.
<Context path="/test" docBase="complete_path_of_dir_contaning_zip_file">< / Context >
Now zip can be accessed in same way http://<host:port>/test/hello.zip
If you have a database, I'd store the file as a blob in a documents table, and build a servlet that is able to handle the file download. It's easy to implement. The database table should record the blob, the mime type and the name of the file.
In this way, you always have your file available (if you store the file in your webapp folder, and redeploy the war, your file will be dropped).