wood burning stoves 2.0*
The moose likes JSP and the fly likes How to get upload an image on server and get it on next jsp? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "How to get upload an image on server and get it on next jsp?" Watch "How to get upload an image on server and get it on next jsp?" New topic
Author

How to get upload an image on server and get it on next jsp?

ronney singh
Ranch Hand

Joined: Feb 10, 2012
Posts: 40

Hello
I am trying to upload an image on server and to display it on next jsp page.
I have written this code:-




I am able to get filepath and filename on next page by request.getAttribute but when I try to put that into src of img tag, image doesn't show up.


Live Life King Size
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61103
    
  66

Please be sure to use code tags when posting code to the forums. Unformatted or unindented code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please click this link ⇒ UseCodeTags ⇐ for more information. Properly indented and formatted code greatly increases the probability that your question will get quicker, better answers.

I've gone ahead and added the code tags for you. See how much easier the code is to read?

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61103
    
  66

The file path isn;t going to be of any use. You need to figure out they the HTTP URL to the file will be.

By the way, uploading files into the web application itself is a really really bad idea. What happens when you re-deploy updates to the application?

It's be a better practice to store them outside of the web app, and to we tie a servlet that can stream the files from that location as the response to an image request.
ronney singh
Ranch Hand

Joined: Feb 10, 2012
Posts: 40

Thanks for telling me about the code tag.
If I store images in the database that would be more appropriate?
Please tell me how to find the http URL of file.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61103
    
  66

ronney singh wrote:If I store images in the database that would be more appropriate?

That could have performance implications. Most people just store the images in a folder location separate from the web app.

Please tell me how to find the http URL of file.

That depends upon whether you intend to continue to store the images inside the web app (not a good idea) or separately.

In the latter case, there is no URL as the image files are not part of the web application. In this case, one way to deal with this is that a servlet is written that knows how to find the image file (which could be identified via a request or path parameter) and to read it from the file system, and then stream it to the response, setting the appropriate headers to identify it as an image.
Piyush Mangal
Ranch Hand

Joined: Jan 22, 2007
Posts: 196
You can store the uploaded file either in the database or on the file system out side of your web application. If you are storing your file on file system, make sure to externalize the path through properties file so that you can change the location as and when required.



In the latter case, there is no URL as the image files are not part of the web application. In this case, one way to deal with this is that a servlet is written that knows how to find the image file (which could be identified via a request or path parameter) and to read it from the file system, and then stream it to the response, setting the appropriate headers to identify it as an image.


You don't need to write any servlet to retrieve the file if you are using tomcat.
In order to get the http url for the uploaded file (incase you use file system on server) in tomcat, you will have to map that path to logical path by modifying your server.xml.

<context crosscontext="true" docbase="c:\\temp\imgService" path="/imgService"></context>

In the above example, physical path c:\\temp\imgService is mapped to /imageService so if you have a image named image1.jpg in c:\\temp\imgService , you can refer the same in
your html as http://localhost:8080/imageService/image1.jpg .

Note : Assuming you are running tomcat on localhost and on port 8080.

ronney singh
Ranch Hand

Joined: Feb 10, 2012
Posts: 40

I am using WebSphere so there would be different way for that other than Tomcat to get the url of image file if I am going to store them in Web Application.
Thanks for reply.
ronney singh
Ranch Hand

Joined: Feb 10, 2012
Posts: 40

Please tell me the code for storing images outside the web application.
I have read some posts about that but in all of them Tomcat is used and change is made in server.xml But
I am using WebSphere 7.0 so please help me with some code and changes to be made for that.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61103
    
  66

ronney singh wrote:I am using WebSphere so there would be different way for that other than Tomcat to get the url of image file if I am going to store them in Web Application.

Why would you store them in the web app? Do you have a plan for how to migrate them for new versions of the app?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61103
    
  66

ronney singh wrote:Please tell me the code for storing images outside the web application.
I have read some posts about that but in all of them Tomcat is used and change is made in server.xml But
I am using WebSphere 7.0 so please help me with some code and changes to be made for that.


You know better than that. No one is going to write your code for you.

To store the file outside the app, just write it there when you save the file.

Now, as to serving the files, that depends on whether you are going to take the servlet approach (which will be the same for any container), or the alternate web app (in which you simply need to set up the other web app).
ronney singh
Ranch Hand

Joined: Feb 10, 2012
Posts: 40

Thanks for reply
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get upload an image on server and get it on next jsp?