Accessing files in Jhipster (Angular + Springboot) - java

I have managed to develop a controller and service in the backend(spring) which successfully uploads a file on the server.
As far as upload location is concerned, I have used System.getProperty("user.dir") to get the path of the current project after which I have appended my custom folder structure specified in application.properties file. Moreover, I am saving the file path in database.
So, while trying to access the file from localmachine(localhost) on anchor tag, the file is successfully getting opened(downloaded to be precised).
However, when I push my application to the server, the project is added as a WAR java file. In the server, the file is getting uploaded to a folder which is outside my war file.
Hence I am not able to access the file from URL.
Please let me know if there is any way to fix this. Thanks in advance.

You could configure a path in your application.properties. In production, it would be absolute path and in dev a relative path. Alternatively, you could configure a url in yml using file:prefix for prod and classpath: prefix for dev to use ResourceLoader.

Related

Passing service account(json file) location to application.properties

I have one maven project which wanted to have service account(json file) for authentication and maven should product the .war not .jar.
Considering that, I wanted to specify the json file location into project application.properties, like this:
spring.cloud.gcp.credentials.location=file:src/main/resources/service-account/service-account-key.json
I am new to the .war world, now the problem is when i build the project locally I am able to get the json file as it is in my resource folder and I am using tomcat locally which is working fine.
But in case of when i deploy into google app engine and its war nature service-account-key.json file is not found on the mentioned location.
Can anyone help on the same, where i need to put in for that as a part of build only i can refer and use the service account json file at both the time locally and after deploy as well.
thanks for you help in advance.
According to Google Cloud documentation you have two choices for providing the credentials to your application:
1. Set the GOOGLE_APPLICATION_CREDENTIALS environment variable explicitly.
2. Pass the path to the service account in code.
You can check this link for more information: https://cloud.google.com/docs/authentication/production#auth-cloud-implicit-java
You can reference the file in resources by prepending classpath: instead of file:
See my answer here.

How to hot deploy newly created files to Apache Tomcat in Eclipse IDE?

I have a peculiar problem. I am extracting the data from a database in the form of .csv file and then passing this file to d3.js for visualization.
The problem is, d3.js doesn't allow file:// based protocol (so I cannot give path to .csv file directly). The .csv file needs to be located on the server. But this file is generated on the run time.
I tried dumping the file in the same project folder, but as expected, the Eclipse doesn't take the updated file until next clean and build action.
Any idea how to work around this problem?
You need to place the file in the place where your application is deployed on Tomcat (e.g.: Tomact7\webapps\myProject...), not in Eclipse Workspace location, this way your js can access the File either by its URL or relative path.

How to access folder in tomcat server using relative path of it?

I have one folder which has some PDF files which is used by my application, but when I include this folder in my war file , the size of war file increase to GBzz , so I thought of keeping this folder in some server location , build and deploy the war without the folder which will have the relative path of this folder , can any one help me on this? I am not sure how to do configuration for this to read the folder ?I am using tomcat server.
The easiest way starting from Tomcat 7 is to configure alias paths i.e. paths on the disk that will be aliased in Tomcat. Check this section in the guide for aliases attribute.
You want something like this in your server.xml:
<Context docBase="AppName" path="/appname" aliases="/pathPdf=c:\pdfs"/>
In case you're on an older Tomcat, your best bet is to setup a servlet that will serve files from the desired external location.

How can I create a file in my project folder through Servlet?

My objective is to create file in one of the folders of my Web-App such that when user clicks a download button, it's gonna download the file from that particular folder. For example, my file resides in "MyProjectName/WEB-INF/NewFolder/myfile.xls". Now, how do I create "myfile.xls" in that folder?
Here's what I have already tried:
I have set the file path to "/WEB-INF/NewFolder/" using this code in my Servlet:
String filePath = getServletContext().getRealPath("/WEB-INF/NewFolder/myfile.xls");
But the problem is, the value of "filePath" in the above code is:
C:\Users\MyUserName\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\MyProjetName\WEB-INF\NewFolder\myfile.xls
Hence "myfile.xls" is not getting created in "/MyProject/WEB-INF/NewFolder" and getting created in the metadata folder instead. Because of this, it's working only in my machine ie., localhost. When I am trying to access my application through another machine I am not able to download the file. I guess I am passing the absolute path to the JSP page which makes it impossible to download it from a different machine. So could anyone please let me know where I have gone wrong in doing this?
Your webapp is deployed to a web server on runtime and filePath points somewhere inside deployment folder not to a project one.
Anyway, why do you want to write into WEB-INF? If you for example redeploy application, then all files inside usually will be removed. Isn't better to use some folder inside server (for example in tomcat we can use ${catalina.home}/myfiles folder or some other outside web server?

where should i put the configuration files in a webservice

Could some one help me on this problem. i have webservice , which reads data from configuration files. When i run this webservice from eclipse , i give absolute the path for these webservices of these configuration files , but when i shift the webservice in to server and run, it can not read the config file. so how can i solve this problem. is there a relative path that webservice can understand during run time.
You can put your configuration files in the root of the AAR archive or in the classes folder. Then use getResourceAsStream to read them.
ClassLoader loader = getClass().getClassLoader();
InputStream inputstream = loader.getResourceAsStream(sFilePath);
How are you deploying these configurations. If they're packed inside the .aar file I would expect them to reside in the classpath, and you could access them via Class.getResourceAsStream()
If they're deployed in the .aar file, however, they're going to be difficult to edit post deployment. In that case you may want to deploy them separately as files and put them in a location well-known to the application, and just read them as files.

Categories

Resources