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

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

Related

Where to put image in Java project for it to be rendered in index.html

I have got a spring boot project with a simple html page where i render my data.
I want to add an image to the html, but when I run application the image is not rendered, no matter where in project I put it to.
Is there a special place, you should put it to?
Currently my index.html is in src/resources/templates.

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.

How to get the assets controller in Play to detect new images?

It seems that if you have play running in production mode, and any new images are added to the public/images directory, the Assets.at() method does not detect these new images until Play has been stopped and restarted from the console.
Is there any way to force the Assets controller to refresh/recompile the list of assets while in production mode? Or do I have to write my own controller for serving up images? (If so, is there one already written to do this that I can reuse?)
You will have to write your own action for your uploaded pictures.
The Assets controller is a controller that serves static resources which are bundled within your application.
You can find a very simple (and low tech) example of an action for retrieving a picture here (I think you'll get the idea).

Generate image and display it inside JSP along with other content

I'm using JFreeChart to generate a dynamic chart depending on the user input. I have a JSP with some textbox and combobox, the user makes the input and submits it, and the Action process it, generating an image of a chart. I need to display this image on the same JSP as before, below the textbox/combobox.
If I use response.setContentType("image/jpeg"); etc... then I get a page with the image alone. I thought of saving the image to a file and then access it with <img >, but I'm not sure that will work (need to save it to WebContent and I may not be able to access it always?).
Is there a way to somehow cache the image and then access it inside the JSP through an <img> or something? Maybe JFreeChart has an easy way to do what I want?
If it matters, I'm also using struts and spring on my webapp.
Thanks in advance.
I've not tried it, but you might look into org.jfree.chart.imagemap and a suitable URL generator from org.jfree.chart.urls. An outline of implementing a PieURLGenerator is illustrated here.
Well, if you generate the image on the server side, you could always just store it in a temp directory using something like a UUID to generate a unique name for it, and concatenating the image file extension on the end of it.
Make sure that the directory the image is generated is accessible on the webserver, and then send the URL path to the image file on the server back to the JSP using ajax (Direct Web Remoting), for display using Javascript.
Just make sure you also have a chron job or service to clear the older files out of the directory now and again.
You should have a servlet that can create the image you want solely from the URL. The URL can then contain an id, which maps back to an object in your program containing raw data in memory. The servlet then generates the image and returns it.
You can then simply set the url of the image in your current web page in Javascript, and it should be loaded.
This is because JSP's are character oriented which do not lend well to binary data so you need to have a servlet do it.

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