What are the steps required for enabling a Java web application (Developed using Spring + Wicket) to accept client file uploads to a specific directory on the server.
While Googling for answers to this question I see suggestions that /WEB-INF/some-directory should be used as the upload location?
Is the reason for this best practice?
Is this location writable by the web application?
What happens to this location when a new version of the web application is deployed?
Can/Should any other directory be used?
Thanks
Two possible reasons for the use of a subdirectory of WEB-INF:
it is not visible through HTTP
your application typically has write access to it
I would rather recommend using a configurable, external directory (e.g configured in a context parameter or en application config file); this way you have fine grained control over the permissions of the directory.
AFAIK, file upload in Wicket is provided by the FileUploadField component, which gives you a FileUpload object that you can writeTo a file on your server.
You might also want to configure/check the maximum size for the files you want to accept. In Tomcat, this is done through the maxPostSize attribute of a Connector (see The HTTP Connector)
Related
Well,I'm working on an Eclipse Dynamic Web Project under Tomcat.
I'm trying to make a web application/site.In a jsp/html page,there is a form where a user can upload a photo.
I handle then this action from a servlet that has to store this image/file somewhere so as to make it possible the image appears whenever I want on the site.
Here is the problem.I started by storing it on my file system,(path in a database) but when I wanted to retrieve it the page didn't appear.
I guess the reason is here:
Why can't I do <img src="C:/localfile.jpg">?
Then,I tried to store the file in the eclipse project folder(WebContent/folder) where I've stored manually some images that do appear.
File folder=new File("/TED/res/img");
File file=new File(folder,fileName);
System.out.println(file.toPath());
Files.copy(fileContent, file.toPath());
But this exception happens:
java.nio.file.NoSuchFileException: /TED/res/img/2017-08-13-123524.jpg
It's one the line of files.copy command which means that
new File(folder,fileName) that I tried failed
What should I do? From what I've read,I understood that also saving file in the IDE's project folder is also wrong but what other choice do I have?
Ultimately, the project will be deployed to a server. As such, there are three distinct issues:
Uploaded user content location: content like images should be uploaded to a folder outside your web app (project). Images inside the web app (project) should be those that are necessary for the application and provided by the developer, not user-generated.
In Eclipse, during development and testing, you will want to serve these images through Tomcat. There are many ways to do this. Tomcat configuration is probably not the best for this - please read the answer and discussion here: Simplest way to serve static data from outside the application server in a Java web application
Once the application is deployed to the server, Tomcat will most likely run behind a Web server like Apache or Nginx. In this case, the external image folder and its contained files can be served directly by the Web server. Even if you implemented a servlet in (2) for local testing with Eclipse and this servlet is part of the code that is deployed, it will not be invoked as the URL will be intercepted by the Web server before it reaches Tomcat. For example, if your uploaded image folder is C:\images on your development environment, it can be served by the servlet using the technique in (2) as /images/*. When deployed to a server, the Web server can be configured to servet /images/* from /srv/content/images and this request will never reach Tomcat.
Oracle recommends using the JnlpDownloadServlet to launch JNLP from Java web applications - https://docs.oracle.com/javase/8/docs/technotes/guides/javaws/developersguide/downloadservletguide.html
However, I need to launch a JNLP application with a dynamically generated JNLP file from a dotnet application.
What would I need to do to enable this?
set content type as application/x-java-jnlp-file
output the JNLP file as a download.
Is there anything else?
The JNLP file is just an XML file that happens to be an executable file as long as you have Java Web Start installed on your machine. This is usually installed on a machine.
From a .NET Web Application point of view, you'd need to ensure the following:
Correct content-type (application/x-java-jnlp). You might need to add configuration to your .NET server to enable this mime type.
The file needs to be accessible via a URL (kind of obvious, but worth mentioning for completeness)
The file needs to be downloadable. Conceptually it is the same as returning any other file.
For seamlessness, the browser needs to be setup to automatically execute jnlp files. Sometimes company/default browser settings do not do this and you'd end up needing an extra click. Not a big deal, but just something to be aware of
In terms of "configuring"/implementing the actual serving of the JNLP file from a .NET Application:
Your end goal is that whatever JNLP file you return is executable with Java Web Start. This gives you a nice repeatable test while you're working on this.
You need to ensure that your .NET Application is able to serve the jar files needed by the JNLP application. Typically the codebase attribute and the resources element needs to reference your .NET application. The JNLP File Structure Page should help with this.
If you look at the JNLP servlet source code you will see that all it does is use the .jnlp file as a template and substitute place holders for specific values. There is nothing special about it that couldn't be duplicated in a .Net MVC or API controller.
I have a Java EE application, running under WebLogic 10.3.5 and Java 6.
I used to have a pdf help file, embedded in my war file, but I need to extract it from there and put it in an external directory (it can be in my same WebLogic domain directory).
I tried to put it in my WebLogic domain and then to < a href > it, but it seems that browsers have limitation and for security reason will not allow to download local file with a href or javascript.
This used to work only on a static HTML file saved on my computer but one the HTML page is deployed on the server, it seems not be possible.
Any idea how I can externalize my help.pdf file from my war file?
#limc is right
you should put this static file outside of Weblogic altogether as a file on an Apache web server
However, in Weblogic there is a feature of virtual directory mapping which allows you to declare a folder outside of the weblogic domain as a content store for any static stuff.
http://docs.oracle.com/cd/E11035_01/wls100/webapp/weblogic_xml.html#wp1039396
This entry goes in WEB-INF/weblogic.xml
<virtual-directory-mapping>
<local-path>c:/usr/mypdfs</local-path>
<url-pattern>/pdf/*</url-pattern>
</virtual-directory-mapping>
Although some application servers allow a Java EE app to reference a file outside the web container, in reality, your web app shouldn't have any knowledge about anything outside the web container, and as you have mentioned, it is indeed a huge security concern.
Depending on what you are trying to accomplish with this PDF file, if you merely want to expose this file on the web, do what #duffmo said and it will work fine. If you want the flexibility to modify this PDF file frequently without recreating the war file again and again, you may want to consider hosting this PDF file in some HTTP web server (Apache2, IIS, etc) and now you reference that link from your web app.
You need to put it at the root of your web context, in exactly the same place as HTML pages. Your web server will be able to find it there.
I have some deployment model question for a Java EE web application. Currently we are deploying our web application as a WAR file in Tomcat 6. All the content is packaged with the WAR file including the static content like images, static html pages and so on. But i want to deploy these static content in a HTTP server and use the Application server only for retrieving the dynamic content. How do i split these things? Does any one has done any thing of this sort and have a good deployment model for my scenario. Help will be appreciated.
Is it a good idea to make 2 WAR files one with only static content and deploy that WAR in HTTP server and the rest as a different WAR file and deploy it in the Application server? But this approach will have impact on all the pages where the static content is currently referred and requires code changes which is very cumbersome since our project is Huge and the code based is very very big.
Any strategy and ideas are welcome.
This can be something interesting to do for performance reasons.
You should have separate deployment scripts / deployment files to do this.
Having multiple file/WAR/folder/scripts to deploy for one project is not an issue. We have the same thing when you have to deploy your WAR and to update your database.
I would have a WAR file and a folder with your static content to deploy.
Edit
Deploying the static content in a HTTP server depends on the server.
If you want to use Apache on a Linux server, you have to set up a Virtual Host.
<VirtualHost *:80>
# This first-listed virtual host is also the default for *:80
ServerName www.example.com
DocumentRoot /www/domain
</VirtualHost>
In this example, you have the a virtual host that listens on 80 port, for any IP address and for the server name www.example.com. Then this is redirected to the /www/domain path.
You will find much more examples and configuration options in the documentation.
You can not deploy WAR file into HTTP server. A WAR is used for Java web applications it must be deployed into application server or servlet container (like Tomcat). I don't think that its a good idea to separate static content in a separate web application. If this is one project it should be one web application, besides:
A WAR file has a special folder structure and contains special files
in addition to JSP pages, Java servlets, Java classes, HTML pages etc.
which combined forms a Web Application.
You can hold your static contents in your one application and there is really nothing bad about it.
If your project is very huge and has a lot of files it is no problem, you just need to use the project structure like that, that it should be easily understandable and readable and the application server or servlet container will take care of deploying as many contents as there is.
Up to version 4, Tomcat has been quite slow in serving static content. This is why it was frequently recommended to split dynamic from static content and serve the latter using a regular web server (the book you mentioned was issued in 2002...). Recent Tomcat versions do not face this problem, thus you can IMHO refrain from splitting, which can be a nightmare for both organization and security.
For static resources, you might rather focus on configuring proper caching, so they will not be transferred more often than necessary.
The Web Start Developers Guide states
All application resources must be retrieved from the JAR files specified
in the resources section of the JNLP file, or retrieved explicitly
using an HTTP request to the Web server.
Storing resources in JAR files is recommended, since they will be cached
on the local machine by Java Web Start.
Now, I have some resources I want to dynamically load after my application has been started (for example OSGi bundles). I can do this using plain HTTP, but I would like to make use of Web Start's support for local caching and versioned/architecture-specific resources.
Is there a way to download additional jar files from the original code base (as specified in the application's jnlp file) using the Web Start infrastructure?
Or alternatively: is there already an established way to integrate OSGi and Web Start that would relieve me of the task to install bundles from the Web Start server?
If you make your application in itself an Equinox-based OSGI application, you can web-start it with all the addition bundles you need.
This article "WebStarting Equinox OSGi Apps" can give you a good introduction on the required settings.
All bundles have to be deployed as signed JAR files
You need a feature that contains all the necessary bundles
When exporting the feature, make sure that PDE creates a JNLP (Java Network Lauching Protocol) file (a checkbox in the export wizard) for this feature
Provide a root JNLP file for you application
Deploy your application to a web server and make sure that the web server is aware of the application/x-java-jnlp-file mime type
He also has an OSGI demo.
I haven't tried it but look at the javax.jnlp.DownloadService API