Java Webapp - Where to store Images - java

I created a web application using Java Spring, Hibernate, with JSP's. It's basically a blog I made from scratch, so there's an interface where I can add a new blog post with pictures.
I use Heroku to host and deploy the app.
When I want to post images in a blog post, I do:
And that image is currently stored in a resource folder right outside of 'WEB-INF'. Except, as I add more blog posts, I will have a lot of large sized pictures. I don't know where I should store these images and how I should access it. I thought of storing it in the file system, but wouldn't I have to re-deploy the app on heroku each time I add more images? Another option was having a public dropbox folder and linking the images there, but that would open up my entire dropbox directory to the public. Is there a better way to do all of this, especially managing so many large pictures?

Saving files in file system is not a scalable solution. If you add new servers for your web app the images won't be displayed correctly for any request.
You have to save your images in a distributed environment. It can be a LOB column on the database (Postgres for example) or an Amazon S3 storage system.
The access type is very different but both systems are a good option for your images. If they are big in size, S3 is the best option.

Related

Uploading images to a specific place in a Java web server

I'm working on a Java web application. As part of this web app, I have a database containing objects with image file paths associated with them. By simply dragging and dropping, I was able to put these images in an images folder in my web server. Their path in Eclipse appears to be src/main/webapp/images.
When I want to access these images in a JSP page, I simply make an img tag with src="/images/fileName.png" and it works perfectly fine.
Here's the issue. I want users to be able to upload their own images to the web server, storing the filenames in the database. I've been looking around for a way to do this, but I'm having trouble finding a detailed answer. What can I use to write the images to that specific folder, images, in my web server? Thanks in advance.
For reference, here is an example of a fileName: /images/propane.png
And here is an example of me calling it in JSP: <img src="/images/propane.png">
I realize this may be a kind of basic question, but I'm really having trouble with getting the image to be stored in that specific directory.
You need to be aware that anything uploaded to src/main/webapp/images will be deleted when you deploy a new version of your application to OpenShift. You can save and serve the images from the OPENSHIFT_DATA_DIR, which is the only directory you can write to that won't be deleted when you redeploy your application. You also need to be aware that the files in OPENSHIFT_DATA_DIR are not shared across gears in a scaled OpenShift application. So if your application is scaled, you need to store user uploaded images somewhere like Dropbox or Amazon S3.
For details on how to serve images from OPENSHIFT_DATA_DIR via your Java application, look at the answer to this question and this OpenShift tutorial.

getting around the 1000 file per directory limit

I just learned from the console of google app engine that there is a 1000 file per directory limit. This is a real bummer and completely unexpected. I may have to change a lot of what was designed for my application.
The application has some entities on the datastore and these entities may have a link to an image. originally I expected to upload all the images together with the application as static files. But there could be 10,000 images or more.
What solution do you recommend? using another service for serving the images and just providing the link to this other service in the entity? Is there a service inside google apps for this purpose?
You can use Google Cloud Storage for your images. You can also consider a Blobstore service within the App Engine itself.
It is a better idea than uploading them as static files, considering how many images you have. There is also a limit on the total size of an app code that you may run into. Plus, it will be easier to upload new versions.

get uploaded file path in java web application

Since from the 4 days i have been trying to find out the path for the uploaded file. I think it wont possible. Can any one tell me how to get the uploaded file path in java web application. Is there any external API to get the uploaded file path? And my project is google app engine type project. Please some one answer it.
As you can't write to the file system it's likely you can't do whatever it is you are trying to do. So you need to use one of the storage options available instead, likely GCS.
https://developers.google.com/appengine/docs/java/googlecloudstorageclient/
Google Cloud Storage is useful for storing and serving large files.
Additionally, Cloud Storage offers the use of access control lists
(ACLs), and the ability to resume upload operations if they're
interrupted, and many other features. (The GCS client library makes
use of this resume capability automatically for your app, providing
you with a robust way to stream data into GCS.)

Where should i put my uploaded images on server in java application?

Am having project structure in eclipse as shown below (using struts ). I have to upload a lot of item images in WebContent/images . The volume of images can increase with time to somewhere around 10000 to 15000 . My question is that each time I would be deploying a war file on the server the old data would be over written and all of the uploaded images would be gone . So what is the best way to implement this upload thing . What I have think of is making a seprate images folder on the machine and create a sym-link to it in my server directory . But if there is any better option available , or even if someone can add some comments on my exiting solution it would be better .
Store the images on your machine. Let's say, /var/project/images. If you have multiple servers running you can add a mount point using for example nfs or cifs. Writhe a image servlet that returns the data from this location. Just add the servlet to your web.XML and use /resources/images/* as servlet location. You can then usethe path information to locate the image in /var/project/images/.
You can use the Symbolic Links/ alias direcory of linux filesystem to do this [folder short cuts in windows]
keep all the images outside webapps, like
usr/local/tomcat/itemimage/
and create a alias directory(Symbolic Links- like folder shortcut in windows) to webapps/<application>/image direcory to link usr/local/tomcat/itemimage/
One Suggestion, If you are going to have a large amount image data, better to load directly into an Amazon S3 bucket. S3, is a “highly durable and available store” and can be used to reliably store application content such as media files, static assets and user uploads. It allows you to offload your entire storage infrastructure and offers better scalability, reliability, and speed than just storing files on the filesystem.

BlazeDS/ FLEX - Upload an Image and Display

I'm working on a FLEX which enables the user navigate through a list of images at the server (BalzeDS tomcat 4.0.0.14931). The images stored under the "WebContent/Images", and the already available/ saved images are displayed as desired.
From this app user would be able to select a file from their local directory and upload it. I have done the necessary coding related with uploading the image and it successfully creates a copy at the desired folder at the server. Once the image file is uploaded I'm trying to display the it at the FLEX end, but the image is not displayed. When check at the target folder at the server end, the file has been created successfully.
In order to make the uploaded image visible, the BlazeDS Tomcat server has to be restarted. Since this is not desired way to go ahead, I am seeking some help from you experts out there to resolved this.
I tried searching the net get this issue sorted out, but unable to come cross any solution. An identical issue was posted on another forum (Tomcat restart problem), that also still not resolved.
I this is best approach to achieve this JAVA/ BlazeDS/ TomCat/ FLEX environment or is there a better alternative. The other alternative I could achieve this is through persisting the file into the database as byte[], but I did not want accomplish in that manner since size of the database tend to shoot up.
I would really appreciate if you could help me out on this.
Thanks in advance.
If you have to restart Tomcat, that means you are storing user data in your web-apps directory. You should not be storing any user data in the webapps folder. These are considered "system files" and should not be publicly editable under any circumstance.
Store any user data in a publicly accessible folder with the correct permissions and serve them over HTTP (you should be able to view it in a web browser).

Categories