Springboot file write, packaged as a jar package - spring-boot

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.

Related

Deploy a embedded Jetty&Jersey war to jetty9,only can see static file?

I cteate a jetty&jersey embedded project with IDEA and Maven,I put it in Github https://github.com/Mengqi777/JettyProject. Run JettyServerStart.java start the server, in the browser address bar enter localhost:8080/dynamic, show OK, enter localhost:8080/static, show static resource file
Now I package it to a war file,and put it in jetty webapps directory. But only can see static file in brower with enter localhost:8080/static.404 Not Found Error when in brower with enter localhost:8080/dynamic
What happend?
What should I do to package this project into a war file or jar file and run it in jetty successfully?
You are doing things in your embedded-jetty usage in JettyServerStart.java that you do not declare/configure in your webapp or war file.
It's a maven project, but not declared as a webapp or war project (in the pom.xml)
Its doesn't have its WEB-INF in the right place (maven directory structure wise), which means your built war file is invalid.
The dependencies are not declared correctly for a webapp or war project (you cannot include jetty-server in a war file)
Your badly located WEB-INF/web.xml does not perform the same configuration steps as your JettyServerStart.java
You don't specify how you created your ${jetty.base} directory to support this war file?
You didn't specify what version of jetty-distribution or jetty-home you downloaded, or are attempting to work with.
The statement "and put it in jetty webapps directory" is unclear, which one? (using jetty-distribution/webapps is invalid and will cause many errors and warnings if you attempt to use it for your own webapps, there's no jetty-home/webapps, and you didn't identify your jetty-base configuration)
The way your project is declared right now, even if manually assembled, skipping maven entirely, you have no servlets, no filters, no listeners, no intializers, only a servlet spec mandated DefaultServlet on url-pattern / giving you static content. That's why accessing http://localhost:8080/static/ works, but nothing else.

Download files to Openshift directory

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!

serving files from a spring boot war file

I've followed the guide here for turning a "hello, world" level Spring Boot app to a war file. I can run this war like a jar and it will return the simple template.
What I don't understand is why I can't access a main.css file I've created. I've placed it in the resources directory under "static/css/main.css" and according to the docs here Spring Boot will automatically server files under "resources", "static", "public", and "META-INF/resources". However, when I build my war file and run it I can't query those files in the browser (like http://localhost:8080/static/css/main.css). Have a missed a step? If I peek into the created war file I see the "static" directory in "WEBINF/classes" right beside the "templates" directory and the directory holding my application.
Files in src/main/resources/static are served from / so you don't need static in the path. You CSS file should be available from http://localhost:8080/css/main.css

How to use executable resides in jar which is launched from java web start?

I have created a Java Web Start Application. So my application launch from the web browser. I need to run an executable file on MAC OS. So I have packaged my executable file inside my jar file. But I am not able to load it from the classpath as it gives me File or Directory does not exists exception. Then I have tried by extracting the jar file, but as it is launched from the web browser, I am not able to get the location of the jar file. I have seen the path of java cache from the Java Control Panel, and I have searched on that path but not getting anything.
Is there any option to use the executable which resides in jar file without extracting jar file ?
How do I get my downloaded jar file in the system ? I have checked the java control panel path but I am not getting the jar file.
I want to copy the executable to temp directory. How do I ?
Please help me on this.

Where to deploy zip file on Apache tomcat 7 to be available for download?

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

Resources