Spring MultipartFile upload file location - java

I am using Spring framework MultipartFile to upload files using my application to the server, though whenever I do any 'Project > Clean' from Eclipse STS, all files I've uploaded earlier get deleted automatically.
I am saving all files uploaded to the following path:
String rootPath = request.getSession().getServletContext().getRealPath("contentrepository");
Which in real path get translated to:
C:\springsource\apache-tomcat-7.0.40\webapps\myapp\contentrepository\
so can someone please tell me what I am doing wrong here to cause those files to get deleted each time I do a Porject > Clean? Is it that STS clean command empty the webapps folder content? Shall I change the upload files folder location to another locations outside the apache folder totally?
Thanks or your time

Spring Multipart upload has nothing to do with your problem!
Whenever you clean your project, all deployed directories will be cleaned. Similarly, when you deploy your project, complied files will be deployed to server.
When you upload a file, it is save in your deployed location, however on cleaning all the data is lost.
If you want to persist file during your testing i.e want to have files even if you clean your project:
- Store uploaded file to some other location.
OR - Take backup of uploaded files before you clean your project and copy them to deployed folder.
You might like to view this question, for what exactly 'clean' does.

Related

Springboot temp directory

I created an application that my user can upload, so far so good, but after a few days that my application is started his temporary directory disappears (I do not know if the OS goes out or something) and from then on the upload not It works more. I need to either restart my application or recreate the directory (an example directory "/tmp/tomcat.323231231312.8282/work/Tomcat/localhost/myapp"
[EDITED]
I already saves all files in a separated file, but the springboot saves the files in a temp directory until upload finish, then I have access to that file
Please provide below property :
spring.http.multipart.location: /data/upload_tmp
This will enable spring boot to create desired folder for hosting upload files.
The temporary upload location [/tmp/tomcat.4296537502689403143.5000/work/Tomcat/localhost/ROOT] is not valid
Why can`t you create sepearate directory for upload activities using spring boot and even log into seperate logger too? Any restriction from application side to go for temp?
Refer this if it replacates the same.
How to create a directory on statup in spring boot project
Try if this is useful
How does one specify a temp directory for file uploads in Spring Boot?

How to preserve folders on server when deploying war file

I am developing an application using Tomcat. I deploy by copying in a war file and restarting the tomcat serice. However, I have large folders full of images and other ancillary data. My question is: is there an alternative to including all the data in the war file for deployment? Specifically:
1) Can folders in the app folder be "protected" during deployment of a war file so they will not be deleted?
2) Would it be bad practice to upload amended classes and jsp files piecemeal to the app folder rather than re-deploy a whole .war file each time? If it is bad practice, why?
I solved this as follows: store data outside the document root in a folder with the right permissions, then use the "alias" statement in the web server config file.
In my case I am using nginx and my location statement is:
location /MyApp/data{alias /opt/myapp/data;}
Then I can refer to /data within the app.

Tomcat serves old files even after removing and copying new .war file

I have a maven project ,say hello.In my /var/lib/tomcat8/webapps folder,i have placed its .war file,as hello.war.
Now I made some changes to a js file,run mvn package and copied the target/hello.war to tomcat8/webapps folder.
The server still serves old JS file.
Things I have tried:
1.Tried opening in private window.
2. Removing Catalina folder in tomcat8/work folder.
3. added to context.xml
But none of them work.Also deleted old hello.war in webapps and copied again,but still problem persists.
However,when i tried copying it as hello1.war in webapps folder,the servers serves the new file.
Can someone guide me how to solve the problem?
Clean and rebuild your project war file.
Delete the hello folder from server. Then restart the server.
Sometime .js is loaded from cache, try Ctrl + F5
Tomcat isn't responsible for this. Some intermediate Web cache is doing it, or your browsser itself is caching.
Try to undeploy the app from http://localhost:8080 , in tomcat manager, then make shutdown from tomcat/bin and finally copy the war to your webapp folder and startup tomcat again
Unzip the war at some temp location and check if the unzipped JS file is the one which you are expecting. If it is, clean the tomcat webapps directory by removing the previous version war and the unizpped directories and files from that war. After that, copy the new war into your webapps folder and start the server.
One other thing, if you are using eclipse and using external maven build then after re-building your project refresh that project in your workspace and then copy the war file.
The ideal thing to do and which I normally follow is :
Delete temp folder
Delete work folder
Delete webapps/hello.war file.
Delete webapps/hello folder
In my case one file was stale and served old content whatever changes I did, the other one used on the same jsp page was successfully refreshed.
Restart/Publish/Clean Tomcat work directory... in eclipse didn't help.
The only thing which did the trick was cleaning the cache in Firefox.
I still have no clue why first file was saved in cache, and the other - not.

Openshift File Uploading in Java

I am doing a program to file-uplaod in the openshift. In this program I am upload whole files documents in the uploads folder, which is given default folder of the Openshift server.
I am using the file upload path
System.getenv("OPENSHIFT_DATA_DIR")
for upload files.
But I want to upload the file in my own project folder. I am created folder externally and I want to all the file in this folder.
$OPENSHIFT_DATA_DIR is the correct location for persistent data. If you upload to your $OPENSHIFT_REPO_DIR, subsequent git pushes will overwrite contents of with your local repo and the uploads will be lost.
If I'm misunderstanding your use case, please help me understand the reference to 'project folder' and 'created folder externally'. Feel free to share your use case on the OpenShift Forums as well: https://www.openshift.com/forums/openshift

Issues with getting realpath while working with Eclipse Indigo

I am developing a web application.
My project is stored in
F:\Pro1\OHCMS
Now i want to upload a video in
F:\Pro1\OHCMS\Webcontent\Video
folder.
When i write
filePath=getServletContext().getRealPath("/Video")+"\\";
It is returning file path as
F:\Pro1\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\OHCMS
How can i get the real path of my actual Webcontent folder ?
Your application is being run by your IDE's default application server. That is, your WebContent folder is actually copied to somewhere else (check out your build script to know where exactly is it copied). And after that, all your data is published to server.

Categories