Uploading images to a specific place in a Java web server - java

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.

Related

Java web application need to refresh to "see" resource files

I'm working on a web based application with Spring MVC and Thymeleaf. I have form which upload image. I decided to save the images in src/main/resources/static/images/ and that works perfect. But I found an issue with upload. When I save new image and try to display it in the HTML it's now working. When i refresh the images folder from STS and refresh the page it works fine. Can you give me some advice why this is happening and is there a way to fix it? I know i can use java code to get image content and display it as a resource but i would like to use thymeleaf's EL. I display the image with #{/images/imagename.jpg}
Best regards,
Peter
The thing is, if your resources are within your web app, you won't be able to display it and change it at will. When you refresh your images folder on STS, it "redeploys" the resources and you can see your images.
What you need to do is to put your static/images folder outside your web app, then you'll be able to easily upload a new image and have it displayed on your web app

Java Webapp - Where to store Images

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.

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.

JavaWebStart and resources like images and files

I have a doubt. I am using javawebstar in a swing project. In my app i have some images that I use to improve the design and a file .xls to show a report. I am using the method getClass().getResource("") to locate the images and I realized that my app try to find the resource in the web server using something like this: jar:http://www... . I thought that all resource would be sought in the local computer where the app was installed. Am I doing something wrong?
The app. will download the resources when needed and cache them on the local disks.
For security reasons the URL shown in the console will always point to the server, even for a trusted app. for which all the resources are cached.

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