So I have a file structure like below
http://dl.dropbox.com/u/322696/FolderPath.JPG
When I start the Tomcat Server, I'm trying to figure out where it's root folder begins with if I were just to start the server in the server window and access a jsp page within the jsp folder. I need to navigate to images/banner/name.jpg (trying to make an image file). I've set the servers context to '/projectname' (the black rectangle next to [repository]). I've tried like:
File image = new File("../images/banner/name.jpg"); //If its root was the jsp I accessed
No dice. Is there a method I could run to determine what the relative path should be?
Are you asking how to get the on disk path from within tomcat? If so you use the ServletContext.
getServletContext().getRealPath("/")
Related
We have a web application hosted on WebSphere that upload images to a directory. The directory path is determined by a parameter value that is stored on database, let's say the parameter name is "dir" [dir = ../images/]
Where "images" folder is under the same application on WebSphere, so the system uploads the image on the folder under WebSphere
On another page, the system retrieves the image and shows it to the user, we wanted the system to read images from database instead of reading it from physical directory. When the system tries to access the image [id: img123.jpg], the system automatically sets the image path to be [dir + imgid -> ../images/img123.jpg]
So we changed the dir parameter value to be [dir = ../images/img.jsp?]
which automatically made the system to open the image from path [../images/img.jsp?img123.jpg] and it worked correctly in reading the image on the webpage as the jsp responds with image bytes stored on database
But now we are not able to write the file as the system tries to write the file to the path [../images/img.jsp?] which will not work.
Do you have any solution to this, or any other suggestions on how to achieve this without making changes to the system itself?
Maybe something like this - take the parameter and cutoff what you don't need:
String dirToUpload = dir.substring(0,dir.indexOf("img.jsp?"));
Problem solved as the main web application has stored the image under the physical path "/images/img.jsp?", as it created a directory with the name "img.jsp?"
Now, I will develop a listener which stores the physical images to database
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
I'm using eclipse and run my jsp/servlet using tomcat6 from eclipse. My servlet creates for me an image that i store in to a directory in my webapps. But when i try to access to this image from my JSP it returns me that the resource is not available.
What's the problem?
Servlet store image in "myapp/images/saved.png"
You need to move the images directory into the WebContent directory. That will make it work.
I am working on web application.I invoke on my jsp request.getContextPath(), but strangely I got address /streetshop.
Then I am appending some path as request.getContextPath() + "abc" and create folder.
Then its creating folder in D:// instead of my webapplication folder.
Please, tell me, I want to upload an image in put it in my web-application root/images/images.gif.
You mix things up here. HttpServletRequest.getContextPath() returns your web application root path. In your example this is /streetshop, so your URL may look similar to www.myapp.com/streetshop. If you want to access the internal file system path, you must obtain it from the ServletContext using request.getServletContext().getRealPath("/"). This should return the location of your WAR files' WebContent folder.
Keep in mind that if you modify contents of this path during runtime, you're going to loose everything when redeploying your application.
Can anybody tell me how do i give absolute path of the img tag's src attribute?
The following doesn't work
<img alt="NO IMAGE" src="/home/administrator/tiger-info0[1].gif"/>
I am working On Ubuntu and i am very sure that image exists on this path.
This is probably happening because the image is located outside the web server's document root.
Your web server will not be able to serve anything from outside the document root. One possible workaround is to use a scripting language that has access to the file system, and route the images through the script. For example, you may want to check out the following implementation in php:
Serving Images Outside Document Root Via PHP
You can also create a symbolic link of /home/administrator/ into the document root:
ln -s /www/yoursite /home/administrator
hmm why don't you copy the image to your web directory and give it the relative path? you server (apache?) may not be able to access the file to serve the browser.
if you are making a local html page you can use that path but if you are creating a website you have to use the absolute path to the document root. And make sure the image path is correct (use firebug)
Give your path correctly with domain or use ../ or ./ is for to represent correct relative path.
You cannot access files that are not in your document root. Get Java application server to not delete your folder. You can probably do this by having one folder into which your users can upload files, and add that folder to your project. You can let users create subfolders inside that main folder, and since the main folder is a part of your project, cleaning the build will not automatically delete it or its subfolders.