read upload file in play framework - java

I'm using play framework 2.1.0, upload files to play-app/upload folder.
then I run play 'start -Dhttp.port=80' to start server.
but when I upload a file to play-app/upload folder, it can not be access immediately.
if I stop the server and start again, then I can access the file.
How can I solve this problem? Thanks a lot.
ps, I route /upload as below:
GET /upload/*file controllers.Assets.at(path="/upload", file)
Could it be that static files are loaded once? How can I solve it?

Preferably create upload folder outside the application's folder and add it's full path like /home/navins/upload-folder/ in application.conf, then you'll be able to access it whole time, also you will be able to upload files there not only with app (ie, by FTP) without need of restarting.

I think what you need is to define a sort of Remote assets controller. Basically, once a file is uploaded, you put it in a folder that is outside your application's folder. Then, use a controller that will let you access it. Here is an example: http://www.jamesward.com/2012/08/08/edge-caching-with-play2-heroku-cloudfront
Here, James Ward creates a controller to access assets that are stored on cloudfront, what you need to do is to write a similar controller and replace the "content url" with the absolute path to your "Uploaded files directory".

finally what I've solve it by adding access method in controller:
public static Result view(String filename) {
File file = new File(Play.application().path().getAbsolutePath() + "/upload/" + filename);
return ok(file);
}
then, change route conf, you can access the files by the method.
BTW, if you are using play framework below 2.0, you may user:
renderBinary(file, ContentType);

Related

class path resource [static/pixel.png] cannot be opened because it does not exist (Heroku)

Im developing a spring boot application using Jhipster, and Im trying to add pixel to email.
The pixel image saved on resources/static folder.
My pixel link:
#GetMapping("/email-status/OPENED/{customer}")
#Timed
public void mailOpened(#PathVariable String customer, HttpServletResponse response) throws IOException {
//code...
InputStream in = new ClassPathResource("/static/pixel.png").getInputStream();
response.setContentType(MediaType.IMAGE_PNG_VALUE);
IOUtils.copy(in, response.getOutputStream());
}
The static folder explorer
When i call the api from the browser with localhost:8080/api/email-status/OPENED/...
the pixel image is displayed.
But when I call from my Domain https://app.mydomain.com/api/email-status/OPENED/123
(I'm using Heroku for hosting)
I get this error
java.io.FileNotFoundException: class path resource [static/pixel.png] cannot be opened because it does not exist
Anyone know why does this happen?
It seems to me that the localhost uses the generated classes where the domain uses the jar. In the latter case the image is inside the jar and that is why cannot be found.
As the document states for ClassPathResource:
Supports resolution as java.io.File if the class path resource resides
in the file system, but not for resources in a JAR
Maybe you should consider using
YourClassName.class.getResourceAsStream("/static/pixel.png")
A very useful link about using resources is the "How to Use Icons"
In case you deployed your app on a webserver in order to access it from your domain, it could only access the image if it is also stored on the same server.
In that case you'd have to copy the image in the appropriate folder on your server.
So I did not find any answer, The image is exists in the files, but yet couldn't use any Local path resource solution ..
I have decided to host the image and use the link instead of static image, This is my working solution
InputStream in = new URL("https://some-host/12345/pixel.png").openStream();
response.setContentType(MediaType.IMAGE_PNG_VALUE);
IOUtils.copy(in, response.getOutputStream());
And one more thing that was must for me is also, the img tag should contain all those params
<img src="image_url(the pixel image)" alt="Logo" title="Logo" style="display:block" width="1" height="1" />

upload file in jsf 2.2

Using my company's chosen cloud storage api and just starting out with JSF 2.2. Trying to build a simple image upload web app and it works when I run locally using Eclipse.
Here's the code:
.xhtml page
<h:form id="form" enctype="multipart/form-data" prependId="false">
Select id Photo: <h:inputFile id="file" value="#{customerEntity.uploadedFile}"/>
<h:commandButton value="Query Cloud"
action="#{customerEntity.addActionController}"/>
</h:form>
customerEntity.java code:
private Part uploadedFile;
private String fileURI;
On local machine, code to extract file name from Part object uploadedFile produces correct file locator (eg. c:\pictures\mypix.jpg) for local access on my machine.
However, when I load into tomcat 7 running in a cloud vm, the application fails with a FileNotFound in the 'try' block:
File source = new File(this.fileURI);
try {
cloud.upload(new FileInputStream(source), source.length());
My debug statements show it's using the file locator from my local pc which clearly doesn't exist on the server. I can't figure out how to get the local file data streamed to the code running on the server.
As a slight jsf newbie, I'm sure it's something obvious, but can't figure it out or resolve via some of the other posts I've seen.
Any guidance would be greatly appreciated!
You're making a rather major conceptual mistake. You should not be interested in the client-specified path/name of the uploaded file. Instead, you should be interested in the content of the uploaded file. And well due to 2 main reasons:
The path to the file will be totally absent when you're using a webbrowser which doesn't expose a security bug that the full client side path is included in the name of the uploaded file (i.e. any browser other than Internet Explorer).
Using new File() on the client-specified path/name would only work if both the webbrowser (which is been used to send the file) and the webserver (which is been used to retrieve the file) runs at physically the same machine, because they have then access to exactly the same local disk file system structure. This doesn't occur in real world.
In order to save the content of the uploaded file in the desired location, do so:
uploadedFile.write("/path/to/uploads/somefilename.ext");
Note: you need to make sure that the somefilename.ext part is unique in its folder. You can if necessary make use of File#createTempFile() to autogenerate an unique filename in the given folder based on filename prefix and suffix.
File file = File.createTempFile("somefilename-", ".ext", new File("/path/to/uploads"));
uploadedFile.write(file.getAbsolutePath());
See also:
Recommended way to save uploaded files in a servlet application

how to create a folder in tomcat webserver using java program?

i want to know how to create a folder in webserver(tomcat 7.0) in java.
iam recently start one project.In that project i need to upload files into server from the client machine.In this each client has his own folder in server and upload the files into them.
And in each user folder we have more than two jsp files.when user request the server to show their content by an url (eg:ipaddress:portnumber/userid/index.jsp) using that files i want to show his uploaded data.
is it possible.?
please,guide me to solve this problem.
thanks.
As to your concrete question, just the same way as in a normal Java application.
File root = new File("/path/to/all/uploads");
File newfolder = new File(root, "/userid");
newfolder.mkdir();
// ...
As to your idea with those copypasted JSP files over all folders, don't do that. Just have a single servlet which is mapped on for example /files/* and reads the folder specific to the currently logged-in user and finally forwards to the JSP to present the results. Or if your intent is really to make the uploads public to everyone so that each user can see each other's uploads, then supply the desired user ID as parameter or pathinfo in the request URL like so http://localhost:8080/context/files/userid.
Please note that you shouldn't store the files in the expanded WAR folder, or they will get lost everytime you redeploy the webapp. Store them on a fixed path outside Tomcat's /webapps folder.
You access files and folders from a web application just like any other Java application: using java.io.File or maybe JDK7's new File I/O mechanism. See also the Java I/O Tutorial and the File-related utilities of Apache Commons IO.
Ok, here we go.
try {
File f = new File("file/path/name/.ext");
if(!f.isDirectory()) {
boolean success = (new File(f)).mkdirs();
}
if(success) {
System.out.println("Success")
}
} catch(Exception e) {}
That's it. I hope that functionally. Ciao

Accessing filesystem from java within Spring and tomcat

I am attempting to do some xml marshalling from with spring/tomcat ... my app is deployed as normal as a war file. The file is indeed copied to the correct location WEB-INF/classes/myData.xml but I am unsure how to access this from with Java and specifically my spring service layer. As normally I access files from with the app context itself.
I want to do this :
final File xml = new File("WEB-INF/classes/myData.xml");
but in my dev build it goes to F:\eclipse\WEB-INF\classes\myData.xml and not the deployment directory inside tomcat
In Spring, a clean way to do this with Java is using ClasspathResource:
Resource myData = new ClasspathResource("myData.xml"):
Alternatively, if this is a Spring bean doing the work, then you can inject it from XML, e.g.
<property name="myResource" value="classpath:myData.xml"/>
... assuming that myResource is a javabean property on your Spring bean of type Resource.
The WEB-INF/classes directory is automatically on the server's classpath, you don't need to (not should you) specify that in the path explicitly.
The Spring Resource interface offers various ways to get hold of the data itself (e.g. getInputStream())
If you are determined to get the path leading to your resource, you can retrieve its URL and then parse it, like this:
URL url = getClass().getResource("/WEB-INF/web.xml");
String path = url.toString();
if (path.startsWith("file:/")) {
path = path.substring("file:/".length());
File file = new File(path);
...do something with the file...
}
There is, however, a caveat: your resource may be read directly from the JAR archive, not from a flat file in your filesystem, and thus not really accessible via a File object. The above snipped worked in JBoss (which includes Tomcat), but JBoss explodes a WAR archive before deploying it - not sure if a pure Tomcat will do this as well.
The main question is: why would you really want to get the resource in the form of a File object? Maybe getting its URL is enough for you? Or maybe you just need read access?
If all you need is read access, the simplest way to get any resource on your classpath (like the above web.xml) is by simply calling:
InputStream in = getClass().getResourceAsStream("/WEB-INF/web.xml")
If you want write access to the file, a much better solution would be to pre-configure a directory (for example, via the web.xml), unpack all your needed files there (for example, via the above getResourceAsStream method) and then edit and access them from your predefined directory which is independent from your application server.

How to store a file on a server(web container) through a Java EE web application?

I have developed a Java EE web application. This application allows a user to upload a file with the help of a browser. Once the user has uploaded his file, this application first stores the uploaded file on the server (on which it is running) and then processes it.
At present, I am storing the file on the server as follows:
try {
// formFile represents the uploaded file
FormFile formFile = programForm.getTheFile();
String path = getServlet().getServletContext().getRealPath("") + "/"
+ formFile.getFileName();
System.out.println(path);
file = new File(path);
outputStream = new FileOutputStream(file);
outputStream.write(formFile.getFileData());
}
where, the formFile represents the uploaded file.
Now, the problem is that it is running fine on some servers but on some servers the getServlet().getServletContext().getRealPath("") is returning null so the final path that I am getting is null/filename and the file doesn't store on the server.
When I checked the API for ServletContext.getRealPath() method, I found the following:
public java.lang.String getRealPath(java.lang.String path)
Returns a String containing the real path for a given virtual path. For example, the path "/index.html" returns the absolute file path on the server's filesystem would be served by a request for "http://host/contextPath/index.html", where contextPath is the context path of this ServletContext.
The real path returned will be in a form appropriate to the computer and operating system on which the servlet container is running, including the proper path separators. This method returns null if the servlet container cannot translate the virtual path to a real path for any reason (such as when the content is being made available from a .war archive).
So, Is there any other way by which I can store files on those servers also which is returning null for getServlet().getServletContext().getRealPath("")
By spec, the only "real" path you are guaranteed to get form a servlet container is a temp directory.
You can get that via the ServletContext.gerAttribute("javax.servlet.context.tempdir"). However, these files are not visible to the web context (i.e. you can not publish a simple URL to deliver those files), and the files are not guaranteed in any way to survive a web app or server restart.
If you simply need a place to store a working file for a short time, then this will work fine for you.
If you really need a directory, you can make it a configuration parameter (either an environment variable, a Java property (i.e. java -Dyour.file.here=/tmp/files ...), a context parameter set in the web.xml, a configuration parameter stored in your database via a web form, etc.). Then it's up to the deployer to set up this directory for you.
However, if you need to actually later serve up that file, you will either need a container specific mechanism to "mount" external directories in to your web app (Glassfish as "alternate doc roots", others have similar concepts), or you will need to write a servlet/filter to serve up file store outside of your web app. This FileServlet is quite complete, and as you can see, creating your own, while not difficult, isn't trivial to do it right.
Edit:
The basic gist is the same, but rather than using "getRealPath", simply use "getInitParameter".
So:
String filePath = getServletContext().getInitParameter("storedFilePath") + "/" + fileName;
And be on your way.
Edit again:
As for the contents of the path, I'd give it an absolute path. Otherwise, you would need to KNOW where the app server sets its default path to during exeuction, and each app server may well use different directories. For example, I believe the working directory for Glassfish is the config directory of the running domain. Not a particularly obvious choice.
So, use an absolute path, most definitely. That way you KNOW where the files will go, and you can control the access permissions at the OS level for that directory, if that's necessary.
Writing to the file system from a Java EE container is not really recommended, especially if you need to process the written data:
it is not transactional
it harms the portability (what if you are in a clustered environment)
it requires to setup external parameters for the target location
If this is an option, I would store the files in database or use a JCR repository (like Jackrabbit).

Categories

Resources