How to save uploaded files into a mapped folder - java

I have a desktop application which uploads files to an upload servlet. I want that uploaded files to be saved into a folder outside Tomcat, which is already mapped.
The url for the mapped folder is: "localhost:8080/MyApp/documents".
Physical path for the mapped folder is: /Users/gyo/Documents/MyApp/documents
Probably I'm making a newbie mistake but I would like to know: Is it possible to save the files in the mapped folder? If yes, how can I do that since servletContext.getRealPath("") is returning me the full path where the app is deployed?

Here is a link that explains how to upload files in Java running on tomcat and spring. Look for permissions on that folder "ls -ltr /folder-path/" to see owner and write permissions.
http://www.gingercart.com/Home/java-snippets/upload-images-java-code-example
Also look at java policies set on Tomcat for file/folder permissions at $CATALINA_BASE/conf/catalina.policy

Related

Upload file to Spark Java

I am trying to create a folder which contains uploaded files. I have seen some examples that saving files into the absolute folder such as D:/tmp or Home directory. My question is I am trying to put my uploaded files into a folder which is inside the project folder.
I also tried to get project folder using like this :
public static String getProjectFolder(){
return System.getProperty("user.dir")+"/";
}
Result become something like this, and file not found exception inside the Html file!
http://localhost:1234/Users/USER_NAME/Documents/java/PROJECT_NAME/upload/genres/action.jpg
What is the best way to add folder into my project? Or should I use hardcoded folder for it ?
You need to set the folder that you want to upload files to as the static files folder using staticFiles.externalLocation().
The only folder the client can reach, either for getting content from OR uploading to, is the folder you set using the static files location API staticFiles.externalLocation(). You can see also an example for that here.
When I run my server locally, I define it to be:
staticFiles.externalLocation(System.getProperty("user.dir") + "/src/main/resources");
This is regarding what your question.
You might run into an issue though. Because now you probably have two locations that you want public:
The resources folder (with all the JavaScripts, CSSs, etc. Typically /src/main/resources)
Upload folder (to let the user upload files)
Having both of these the same location is not a good practice. I managed to resolve that by creating a symbolic link called files inside the public folder.
abcmbp:resources abc$ pwd
/Users/abc/dev/wspace/proj1/src/main/resources
abcmbp:resources abc$ ls -l files
lrwxr-xr-x 1 abc staff 27 Mar 3 21:20 files -> /Users/abc/appUploadFolder/
Then, in the post handle of the uploading path, I give this folder as the destination, and this way I have a separation between resources and uploaded files.

Playframework 2.2 : Uploaded Files in Public directory not accessible

I am having problems with accessing file in public directory after deployment.
The stage command give me a folder target\universal\bin where my .exe file is.
In development mode I used to upload my files to public\uploads\pictures and access them from this location. But, after deployment I am unable to upload the pictures. I read this Stack Link that has two options. Is it possible to define a folder directory that is not absolute.
Application Conf
myUploadPath="public/Upload/Pictures/"
Accessing folder
String myUploadPath = Play.application().configuration()
.getString(myUploadPath);
Please tell me a solution to overcome this..
Found a solution
During development we are using public directory to store anything extras(in my case uploaded files) that we have. But, while deploying the application it is important that we change the reference to these extra files. I have changed the path from public directory to the absolute path of where the file is executed using
Play.application().path().getAbsolutePath()
and store the files in this directory.
If you don't want to use this than you can also specify an External Asset

How to refer a file system in Cloudbess?

I'm new to Cloudbees. I just opened an account and trying to upload a .JAR file which basically downloads a file to the location mentioned by user (through java command line arguments). So far I have run the .JAR in my local. So far, I was referring to my local file system to save the file. If I deploy my .JAR file to Cloudbees SDK, where can I save the downloaded file (and then process it).
NOTE: I know this is not a new requirement in java if we deploy the jar in UNIX/WINDOWS OS where we can refer the file system w.r.t to home directory.
Edit#1:
I've seen couple of discussions about the same topic.
link#1
link#2
Everywhere they are talking about the ephemeral (temporary) file system which we can access through System.getProperty("java.io.tempDir"). But I'm getting null when I access java.io.tempDir. Did anyone manage to save files temporarily using this tempDir?
You can upload a jar with the java stack specifying the class and classpath (http://developer.cloudbees.com/bin/view/RUN/Java+Container)
Our filesystem however is not persistent, so if you are talking about saving a file from within your application, you could save it in this path
System.getProperty("java.io.tmpdir")
but it will be gone when your application hibernates, scales-up/down or is redeployed.
If you want a persistent way to store file/images, you can use AmazonS3 from your cloudbees application: uploading your files there will ensure their persistence.
You can find an example of how to do that in this clickstart:
http://developer-blog.cloudbees.com/search?q=amazon+s3
More information here: https://wiki.cloudbees.com/bin/view/RUN/File+system+access

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

how can i open a file from specific folder of server to browser in jsp

I'm doing a web application where I can upload and download pdf files from server.
In this I created a pdf files and stored its path on the database.
What I want is when I search by some criteria (like filename) I should get list of files from db. When I click open link it should be opened in jsp.
Downloading part was done by me. What I need is just to open a pdf in jsp.
is it possible to open server file from jsp?
Place those files outside WEB-INF folder and access. WEB-INF folder is secure in a Java EE environment. By placing your files inside the WebContent folder and outside the WEB-INF folder will help you to access those files for download. You need to mention the full path for that file. E.g for an image your path will be https://localhost:9444/AppName/resources/theme/images/logo.png

Categories