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
Related
I'm currently facing a problem any time i try to access to WEB-INF/classes folder.
Basically if I run the whole project on a local server it works and find the given file, but when i try to access to the same file after all the jar files have been created and deployed it gives my an error - the file doesn't exists
this is the current path on the server :
file:/product/WebSphere85/AppServer/profiles/opntship_node_1/installedApps/opntship/xxxService.ear/lib/xxxServiceService.jar!/xxx.pdf (A file or directory in the path name does not exist.)
this is the path on my local server:
/C:/Users/foo42/IBM/rationalsdp/workspace/TpdPrintServiceService/target/classes/xxx.pdf
this is how I get the file :
getClass().getResource("/"+fullFileName);
fileToBytes(new File(filePath.getFile()));
I tried in many ways to get access to the folder but still, it works locally but not on server :(
any idea how to fix this problem and access to the web-inf folder ?
Thanks in advance!
We cannot read the entries within an archive (xxxService.ear and xxxServiceService.jar) like it was a plain old File. You may want to try Thread.currentThread().getContextClassLoader().getResourceAsStream(path) instead.
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.
I have a .properties file that is under a source folder I made called res (using Eclipse Mars 2). I have two other folders in there called messages and schemas.
I need help in giving a filepath so it works locally and on a server (e.g. JBoss) after making the project into a .war file. This is my .properties file:
# Credentials
user=flow
password=flow
# Path to schema for validation
schemaPath=schemas/Schema1.xsd
# Path to where you want to keep incoming and outgoing messages
messagePath=messages/
The above properties file will only work if I provide the full path to the two different *Path properties (above is not full path). However, I can't do that because it needs to work on the application server and on different operating systems.
In my code, I save the filepaths to Strings and use those Strings to specify where to write or read. How can I make it so it works after deploying to the server using a .war file?
I am using a Dynamic Web Project in Eclipse Mars 2.
EDIT: Since the properties is user configurable, they might give a full path. It should work whether the path is short as shown or the full path.
You have to make sure that the properties file is part of the classpath, that is usually including it in classes/ directory.
Mark the folder with properties file as Source Folder in your eclipse. If it is in a package, then use that package name in your path while loading the file.
For example, if the file is in config/data.properties, then load the file by .getResource("config/data.properties");
I'm having a trouble accessing some files in universal way across app run modes.
I have folder "resources" in app root folder, which contains some crucial files.
When I run in dev mode, I access them in simple manner, like:
Play.application().path().getAbsolutePath()+"/resources/file.file";
But after I package my app via dist commmand (I've modified build.sbt so that "resources" folder is copied near conf and lib folders), the code above stops working, due to this line
Play.application().path().getAbsolutePath()
now returns a path to bin folder, in which app.bat is ran from. So if in dev mode, the code above returns correct path like X:/app/resources/file.file, in prod mode it's like X:/app/bin/resources/file.file which is incorrect.
P.S. I absolutely can't put my files in conf folder and access them as a resource from a classloader because of numerous reasons which are actually not important.
So the question is simple as that: how to access these file resources in a universal manner across modes, without any hardcoding.
TY in advance.
There is a method on Application which lets you access files in the application root.
https://playframework.com/documentation/2.4.x/api/java/play/Application.html#getFile-java.lang.String-
default java.io.File getFile(java.lang.String relativePath)
Get a file relative to the application root path.
Parameters: relativePath - relative path of the file to fetch
Returns: a file instance - it is not guaranteed that the file exists
Since you already have the application, you should be able to use this method to directly access the file.
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