Unable to find the file saved in WebContent - java

I'm using Glassfish 3 and Eclipse as my IDE.
In the application, I developed a small file upload feature which saves the files in WebContent. I know it is a bad practice because I'm loosing all the files on every redeploy and so on.
The file is saved at:
E:\java\server\Glassfish3\glassfish\domains\domain1\eclipseApps\myApp\upload\story\79.png
Than I try to list render that picture in my .xhtml file using:
<img src = "upload/story/79.png" />
Weirdly enough, on the Servlet side (or ManagedBean in my case) when deciding if the image exists or notit all looks fine and the image is found on the disk. However, in the browser the image is not displayed because it fails to load the given URL.
How can I solve this problem?

Related

Background Image of CSS is not showing

I have written a Java WebApp with Vaadin 14.8.0 and SpringBoot.
When I put the application in production mode and create a war file with the command "mvn clean package -Pproduction" and deploy it on my Wildfly, everything works normally. My CSS files are read and also activated.
However, the path I use in the css file for the background image is not found.
"background-image: url("/META-INF/resources/img/zac-bromell-QwrTnOlWAmI-unsplash.jpg");"
If I enter the path directly as url:
"http://localhost:8080/planyoureplaylist-1.0-SNAPSHOT/META-INF/resources/img/zac-bromell-QwrTnOlWAmI-unsplash.jpg"
I also get a 404 Not Found.
My css files are located in the root directory under ./frontend/styles.
My image files can be found in src/main/resources/META-INF/resources/img/.
I also looked at my WAR file with WINRAR. Since I notice that I find the image files in the directory WEB-INF\classes\META-INF\resources\img, but in the whole folder structure not my css files.
To my surprise the background image was displayed normally when I started the application not yet via an external wildfly.
In reference to the following link https://github.com/vaadin/flow/issues/11015 I understand that it is a bug of vaadin. However, since I am on Vaadin 14.8.0 and the bug should be fixed with 14.6.2 I do not understand the problem.
Gladly point out if something else is needed to solve the problem.
About help I would be very happy.
Thanks in advance
The files in META-INF/resources are published in the server root so your META-INF/resources/img/zac-bromell-QwrTnOlWAmI-unsplash.jpg file is available at img/zac-bromell-QwrTnOlWAmI-unsplash.jpg inside your context root. Your background image CSS should thus be "background-image: url("img/zac-bromell-QwrTnOlWAmI-unsplash.jpg");"

Spring boot doesn't serve some of the images from the src/main/resources/public directory

I'm trying to develop a small webapp, for learning purpose, with spring boot,thymeleaf and mysql.I'm using Eclipse IDE
Users can upload images to the app, My Controller class store the images in the src/main/resources/public/images folder and it's URL in the db.
The problem is, Eventhough the images saved by the controller exists in the src/main/resources/public/images directory, They are not directly visible in the public directory from Eclipse, as a result when referred from the served webpages, those images do not show up. Some of the images I manually copy pasted into the public/images folder within the Eclipse .(To do that actually I had to move images to another directory as it's not possible to copy from and paste into the same folder)
Those images are rendered in the served web pages.. The other images saved by the controller, even though they exists in the same directory ,they are not directly visible from Eclipse IDE and they are't rendered in the served pages.
This is Directory's view within the eclipse and all these images are rendered correctly in the served webpages
This is the same directorie's view from the file system, and it contains many images that are not directly visible from eclipse.
And when I include these images in the webpages , they are'nt rendered even though they reside in the exact same folder as other images..
How to get around this issue?
When you run a spring boot or normal spring project using IDE it will bundle everything inside a war and then it serves that bundle so without restarting the server or re-running the application Eclipse doesn't loads the images.
This has nothing to do with spring boot.It is because of the Eclipse IDE as you know if you paste one image inside the folder directory but images wont load without restarting the server or re-running the application.
My recommendation would be to store images on any directory other than your deployed folder directory and access them without any issues.
I have done this inside tomcat folder without any issue.

Avoiding complete path of folder and Image not been shown in browser

I am making a web application using jsp and servlets .But am facing two problem that i have no ides how to remove :
Problem 1 : I am creating new folders in my WEB-INF folder .But what i want is that instead of giving full paths .I just provide relevant path Like :
File tempfilesstore = new File("C:\\Users\\admin\\Desktop\\SharedCrpto1\\web\\RetrievedFiles\\"+fileid+"-"+personname);
if(!tempfilesstore.exists())
tempfilesstore.mkdirs();
Can this full path be avoided as only path from web folder of the application is required.
Problem 2 : I keep a image in this folder by performing some operation on original image being browsed by the client on browser.
Now when i see the image in folder then it is present their But if i try to see the same image in browser it does not display the image .When i refresh my page for 3-4 times than sometimes it get displayed and sometimes after manually opening it by going to specified location.What can be reason for it ?Please help.
Here is how am trying to get image on browser :
<img src="RetrievedFiles/<%=path%>/<%=sharedfilee%>" alt="Image Preview Not Availablee" width="300" height="300" />
Here ,
String path=presentfileid+"-"+personname;
String sharedfilee=rs.getString("FILE_NAME");
First of all, in a JEE point of view, all files within the WEB-INF folder are not meant to be accessed by anyone but your server. It means that images, CSS files, javascript files, etc. in this directory will not be rendered by your web browser. Your JEE server will prevent that to happen.
So, in order to access files from your web browser, you need to put them outside the WEB-INF folder (at the same level, in a "images" folder, for instance).
For your first problem : Yes, you can use relative paths to instantiate files, using your classLoader.
this.getClass().getClassLoader().getResource("resourcePath")
or
this.getClass().getClassLoader().getResourceAsStream("resourcePath")
depending on your needs. The first one returns a URI (http://docs.oracle.com/javase/7/docs/api/java/lang/ClassLoader.html#getResource(java.lang.String)) whereas the second returns an InputStream (http://docs.oracle.com/javase/7/docs/api/java/lang/ClassLoader.html#getResourceAsStream(java.lang.String)) that you can use with a FileInputStream.
The ressources are located in the classes folder, and the path is relative to the class your get the resource from. For example, you can use "/myImage.png" as a path to get the image at the root level.
Putting all your resources files in the "classes" folder (within subfolder if you want to) is a good architecture design.
For your second problem, you have mainly 2 solutions :
if the image is rendered without any transformation, put it in a folder outside WEB-INF (see the beginning of my comment), and it will be visible from outside. In you JSP, you can access it like that :
request.getContextPath() + "/" + sharedfile
if the image needs a transformation, use a servlet instead
I hope that helps you.
Regards,
Alexandre FILLATRE

jsp file upload location

hi i am uploading my images using the
String filePath = context.getRealPath("/")+"/Library/";
all of the images are get to saved in
.../workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/MyProj/Library/
when i list the files in
.../workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/MyProj/Library/
from terminal i can see the images that were uploaded. but if i list the files in
.../workspace/MyProj/Library/
i cannot see them. also from eclipse (inside project explorer)
MyProj
->Library
i cannot see the images uploaded. then i thought probably that location is kind of temp location, and restarted my computer. but uploaded images are still in
.../workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/MyProj/Library/
but through eclipse they are not visible, doing file->refresh also did not help. my questions are follows will the uploaded images stay in this location
.../workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/MyProj/Library/
and why i cannot see them from eclipse? if it is possible can you show me how to save them directly in
.../workspace/MyProj/Library/
thanks!
You can't see the images because eclipse has deployed your webapp to a server and your images are being uploaded to the server.
You can save them directly to your workspace by using:
System.getEnv("user.home")+"/workspace/MyProj/Library/"
instead of:
context.getRealPath("/")+"/Library/";

Java applet error

I am doing a project on applets. I designed the applet using netbeans. After building the project in netbeans, I took the directory "classes" and a .html file from the "build" directory and moved it to another new directory. This .html file includes the applet. The .html file displays the applet correctly, when it is viewed from my desktop.
I uploaded the "classes" folder and the .html file to my free server (host4ufree.com) using FileZilla. If I try to view the webpage online, I get the following error instead of the applet getting displayed:
java.lang.ClassFormatError: Extra bytes at the end of class file
I am using JDk 1.6.0 update 18, and uploaded the file using FileZilla both ASCII and binary format manner. Yet, I am not able to solve the error problem. Does anybody know the solution to this? Is there something wrong in the manner in which I'm trying to add the applet to my webpage?
The question is quite unclear :S Anyway...
I uploaded the "classes" folder and the .html file to my free server
(host4ufree.com) using FileZilla.
If your applet contains more that one class I do not recommend upload the project classes folder itself but wrap your applet classes to jar file before delpoying it.
Report if that helped

Categories