On my website; users can upload their pictures. I am using tomcat with apache , hibernate, jpa.
I would prefer to keep these images at some location like /var/ImagesUploaded on my ubuntu box. Using Java I can refer these files in directory /var/ImagesUploaded using java.io but how will I show these images on HTML pages to user? On HTML files we need tags like <img src=''> and this src is relative to the webapp. So, is it that I have to keep the user uploaded images inside my webapp ONLY! Or is there a better solution?
How about reading the images on your application, and then serve them to your user as a base64 encoded stream? That way, you don't have to expose your image directory to the web, and can effectively prevent it from being crawled by bots.
Read the file as an InputStream inside your application using java.io
Convert it to Base64 using Apache Commons Codec. encodeBase64URLSafeString(IOUtils.toByteArray(yourImageAsInputStream);
In your response HTML, embed the image as <img src="data:image/jpeg;base64,encodedString">
This post on SO might be useful as well.
Either configure your webserver to server files from /var/imagesUploaded (for example, as /imguploads), or use a script that reads the image and outputs it to the user, with the correct headers.
You have several options:
If you are on Tomcat 7, you can use the "alias" feature in your <Context> element (http://tomcat.apache.org/tomcat-7.0-doc/config/context.html#Standard_Implementation)
You can attempt to map the DefaultServlet on some special path like "/uploads/*", but be aware that some versions of Tomcat have a DefaultServlet that basically does not work properly when it is not mapped to "/"
You can write your own servlet to serve the bits yourself, though you will end up duplicating a lot of the capabilities of the DefaultServlet and may fall short in the robustness category (e.g. implementing Range queries, etc.)
Note that if you write your own servlet, you have the option of performing user-based checks for authorized access to certain resources. Maybe you don't want your whole uploads directory to be accessible by, say, Google.
Related
I am working on a Java Web-Application project using servlets, eclipse, and tomcat.
I would like to be able to dynamically store/create persistent files from servlets and allow the user to access the files using a link, without storing the files in the database.
I have read that getServletContext().getRealPath("/") is volatile and gets reset every time the server is restarted.
I have also read that creating a directory like "$HOME/.ourapp" would solve this. Although, I cannot seem to find how to set-up tomcat to allow the user to access the files using a link, using the eclipse-tomcat.
Question : How to set-up eclipse-tomcat so that the link to the website "http://localhost/" and the file "http://localhost/temp-xx.txt" is the same, while also allowing to dynamically create persistent data "temp-xx.txt" is generated by a servlet and allow the user to access it and does not get deleted when the server is restarted.
This gets complicated, because Tomcat can server files using DefaultServlet (it just sends files back to the client, exactly as you'd expect from a web server), but it caches files internally, so modifying the file system underneath it can have some surprising behavior.
You can disable caching for the DefaultServlet but I've seen reports that it still behaves in surprising ways. The only fool-proof solution I've seen is to write your own servlet that streams the files from wherever they are stored.
But writing your own streaming servlet isn't as simple as you might think. If you want it to be high-performance, you'll want to enable all the nice HTTP features like range-requests, eTags, If-Modified-Since and all that stuff that the DefaultServlet already provides. Perhaps you should start with using the DefaultServlet and see how far it will get you.
The configuration is actually really easy: just add a <Resources> element to your META-INF/context.xml file and use a postResources attribute. You can find the documentation in the Tomcat users' guide for resources.
The problem with files is if i have bulk of users i should download images for every user in a certain folder and pass them to jsp.which is unfriendly.Is there any way.
Well, you can create a separate controller which serves images directly from the database, and then use the path of this controller in all of your JSPs. This way you will not need to "preload" the images into some local directory.
In case this happens to be slow, configure some kind of caching layer in front of the application in nginx or apache httpd.
I'd like to implement in my web application a file/directory upload similar to Google Drive style (I think it's the best example to explain what I want).
So I would like to upload:
a single file
multiple selected files
a selected folder (all files contained in it)
On client side I suppose I have to use HTML5, am I wrong? But How to handle this on server side controller. I'm using Spring MVC 3.2.9
Can you suggest me the best approach?
The hard part is the client side upload of folders. According to this other answer on SO about Does HTML5 allow drag-drop upload of folders or a folder tree?, The HTML5 spec does NOT say that when selecting a folder for upload, the browser should upload all contained files recursively.
Of course it is possible, but HTML5 is not enough and you will have to use Javascript to (recursively) find all files in the folder.
As said by conFusl, you can find a nice example on viralpatel.net Spring MVC Multiple File Upload tutorial. Spring Multiple File upload example. The princips are :
on client side generate (via javascript) a form with one <input> tag per file to upload, and give them names like files[i]
on server side, you then get a form containing a List<MultipartFile> that you can process as usual.
Our new start-up company is trying to build a mobile app with an accompanied website. We are trying to setup our application on Amazon Web Services.
We have Java code running in an EC2 instance, which will store data in S3. We want clients (iOS and Web for now) to communicate with the Java Backend via a REST API. Ideally the website would be hosted under the same AWS account.
The Java Code and REST API are already set up in a very basic form, but the setup of the Website is unclear, since this is new to us all. We also would like to evaluate beforehand if such a setup is even feasible.
Since I am in charge of the Website i have already spend hours researching this specific setup, but i simply lack experience in cloud/backend development to come to a conclusion.
Here are some questions we would like to answer:
Where would the HTML files and accompanied JavaScript etc. files be stored?
How can data (images etc.) that is stored within S3 by the JAVA code be accessed from the Website directly?
How could something like bootstrapping of data within HTML files be achieved (in JSON format preferably)?
How could the server be set up to compress certain files like CSS or JavaScript?
Please point me into the right direction, any comment is appreciated.
Where would the HTML files and accompanied JavaScript etc. files be
stored?
Either on the same AWS EC2 box or a different one, just give it a static IP and link that IP to the domain you want, done. Just remember to have port 80 open as a firewall rule.
How can data (images etc.) that is stored within S3 by the JAVA code
be accessed from the Website directly?
The files will have some url that you can link to directly in your html so it's essentially just a url.
How could something like bootstrapping of data within HTML files be
achieved (in JSON format preferably)?
You have a number of choices here. You could potentially create some JSP files to generate the HTML and load the JSP files on access and cache them so they load up super fast. You could argue however, this is overkill and in some ways, the REST endpoint should be robust enough to handle the requests.
Part of me thinks you should endeavor to use the REST API for this information so you just have to manage one endpoint, why make an extra endpoint or over engineered solution for the HTML that you then have to maintain? Build once and reuse.
How could the server be set up to compress certain files like CSS or
JavaScript?
During the build process, run the files through a minify process. This is built into maven so you could do it automatically or by hand using something like jscompress. This Minify plugin shows how to automatically minify your resources. Consider you'll need to be using Maven though as your build tool.
I am a bit of a GlassFish beginner, so please forgive my ingnorance on the subject.
Basically we are serving a game website, and to make the client downloadable by our web app we copy it into a directory within domain1. The problem with this is that when redeploying the web app the client downloadable is lost and we have to copy it across again.
I'd like to be able to store the client downloadable in some external location and have GlassFish provide access to it.
I could just hardcode the link into the web app, but then we would lose portability so that's the reason for having GlassFish handle it.
I could also put the client downloadable into our database but that seems like poor use of a database and could also result in poor database performance.
The third option I have found is to add a custom resource mapping from some name to the file location, and then provide a method in one of my beans to retrieve the file location. This seems like a lot of work just to have an external resource, I feel like there must be an easier way.
So what should I do?
With GlassFish you can define an alternate document root to serve files from outside the war. From the documentation:
Alternate Document Roots
An alternate document root (docroot)
allows a web application to serve
requests for certain resources from
outside its own docroot, based on
whether those requests match one (or
more) of the URI patterns of the web
application's alternate docroots.
To specify an alternate docroot for a
web application or a virtual server,
use the alternatedocroot_n property,
where n is a positive integer that
allows specification of more than one.
This property can be a subelement of a
sun-web-app element in the
sun-web.xml file or a virtual server
property. For more information about
these elements, see sun-web-app in
Oracle GlassFish Server 3.0.1
Application Deployment Guide.
So you could configure something like this:
<property name="alternatedocroot_1" value="from=/ext/* dir=/path/to/ext"/>
Refer to the documentation for full details.
The link to your downloadables needn't be in the same application as the game servlets, right?
One solution would be to create a new "pseudo" application containing only a web.xml and your static file content. You would of course not deploy it in war form (well, only if you really want to) but just copy the files into the unpacked directory when you want to change content. I use a setup like this to serve a bunch of files from a Web app server I run.
At work, in an "enterprise" kind of environment, we do things differently. We have an Apache HTTPD server working as the front end. It forwards to the app server for stuff that needs to be done in Java, but any static content, as well as cookie management, SSL, load balancing and other "web server-y" stuff is done by HTTPD. This yields a bit of a performance advantage with heavily loaded sites and lots of big but static files. It also lets us split the work among different physical boxes, which again can help with performance.