Upload file to Spark Java - 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.

Related

How to download all folder content in Liferay 6.2

Is there any way to download a folder : all files within the folder and subfolders in Liferay 6.2 without using a loop through all files existing in the folder ?
I need to do it programmatically.
Example :
Folder to download "XFolder"
XFolder
- SubFolder1
- File11
- File12
- SubFolder2
- File21
- File22
- File1
- File2
When choosing to download XFolder, the system searches the folder in document and media and saves all the folder content in a .zip file in disk.
The content should have the same structure above.
Thank you for your help.
You can try to use the "treePath" value of folder and entry to figure out the files but you still will require some looping.
You will probably need dynamic queries for this.
the algorithm should go something like this.
Find out ID of your folder
Look through the treePath property of the Folder table and get a list of all paths that you are interested in.
get all folder ids
loop through all the folders that you are interested in and load their respective files. (Probably you could also do a query that would collect it all in one go)

Writing to text file created manually inside app

Is it possible in Android, to manually add a file inside a project and then, modify it? Example:
"I have a test.txt file in the following path: "app/src/data". I would like to make a method to write a given String in the test.txt file."
Is that possible? I been looking everywhere, but can't seen to do such an easy task.
If you mean modifying files inside the APK itself then it's not possible. Besides, the folder structure you see in the project is not the final structure on the APK (just unzip your APK, it's a .ZIP really): Fpr example, the source directory is all compiled into a classes.dex. The res/ directory is compiled and fully copied ...
Take a look at How to write files to assets folder or raw folder in android?
and https://developer.android.com/training/basics/data-storage/files.html
You can read raw files stored in /res/raw, or assets stored in assets/ , but you cannot modify stuff inside the APK itself.
What you can do is create and modify as many files as you wish from the different places Android gives to any app, such as:
CACHE directory (context.getCacheDir() -> /sdcard/Android/data/your.package/cache
External files (context.getExternalFilesDir() -> /sdcard/Android/data/your.package/files
Arbitrary directories in the SDCARD.

Tomcat JVM - Root folder reference [duplicate]

This question already has answers here:
Recommended way to save uploaded files in a servlet application
(2 answers)
Closed 7 years ago.
The following code returns the path to the war folder (i.e. /ROOT/):
getServletContext().getRealPath("/");
I need to go one folder up from this point, how do I go about doing that?
I have tried the following:
final String path = getServletContext().getRealPath("./");
final String path2 = getServletContext().getRealPath("../");
final String path3 = getServletContext().getRealPath(".../");
I need so save files outside of the ROOT folder, but only one level up, so that everytime I update my website, it doesn't replace the physically uploaded files within the ROOT folder, and rather only touch the web site files.
Thanks
We don't even know if your WAR files are unpacked: Tomcat can well deploy them without unzipping, which leaves you with a non-filesystem-path.
As discussed in the comments, it's bad practice to do this, you should rather figure out where to store the data - for example: database, external storage, somewhere in the file system. Then "just" provide a separate download option for those files - individually or zipped - through your web application.
Another solution you can use is a symbolic link to an external folder.
You can create a folder out side your project let's say for the example at the same level of the webapps folder in your tomcat path.
We will name uploads for the example.
Now in your deploy script (assuming you have one), you can easily create symbolic link from the uploads folder to a folder inside your project.
It will allow you to get the files from inside of your project, and when you re-deploy, the files are not delete.
For example:
ln -s /var/lib/tomcat6/uploads /var/lib/tomcat6/webapps/myWebApp/uploads
The example above is for my distro of tomcat6 but you can change the path to work with your setup.
This answer uses 3 assumptions:
Your WAR file is unpacked.
You have a deployment script and if not you can easily create a bash script
to do so.
The solution you need is for a non distributed setup (each server holds its own files).
Liron
You have to use the File's API over war's location.
File root = new File(getServletContext().getRealPath("./"));
String parent = root.getParent();
String parentParent = new File(parent).getParent();

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 download folder containing files(pdf,png etc) from php server in android

i want to download folder containing files like pdf,jpg,png etc from web services.I have written a application that can download pdf,png file.But i want to download folder containing several files. So my question is- is it possible to download folder and if yes then how can i download folder from server in android. thank you
If you can download one file, why wouldn't you be able to download several? Create the folder on Android, queue the files and start the next download when the current is finished, filling the folder. Also Android supports ZIP, so that might be your best call.
Folder is nothing but an FILE but a different type of file. Once you get the folder you can list down the files in it. You already have URL with the folder, u just need to append the URL of the folder to the files in it and download them one by one.

Categories