aspose file tools*
The moose likes Servlets and the fly likes To know about File Upload Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "To know about File Upload" Watch "To know about File Upload" New topic
Author

To know about File Upload

vijay kumar dahiya
Ranch Hand

Joined: Oct 19, 2008
Posts: 33
Hello To Every One,
when I upload the File like a image from html page Its not show on servlet page using appropriate logic.
I read the FileUpload Home page.But I am still confused.
I want to know Is true that file uploaded image can not show directly on servlet? firstly Its store to database or memory then Its reterive to servlet or JSP.?
Please assist me.
Thanks.


Vijay
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

Whether you are creating pages with JSP, Servlets, php, or Fred's Fabulous Framework, it's all HTML by the time it gets to the browser. And in HTML, images are referenced with the <img> tag.

You can;t just blurt image data out into a page and expect the browser to know what to do with it -- it needs the <img> tag -- whose src can point to a servlet that serves up the image data (and just the image data).


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
vijay kumar dahiya
Ranch Hand

Joined: Oct 19, 2008
Posts: 33
Can you help me please,how Image data display on Servlet page.
Here my servlet file:
Upload file.java:

Servlet page not display the image properly.Although Servlet page show a small icon.and server side show Length but not show the index value,Its show -1.like that:

please help me.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

So the purpose of this example is just to send the uploaded image file back to the browser?

Looking at your code, you start out fine by getting the uploaded data into a file. But then instead of reading from that file and copying it back to the response, you try to read from the request!
vijay kumar dahiya
Ranch Hand

Joined: Oct 19, 2008
Posts: 33
So,How I do that.?
Please help me.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

Okay. So you have a FileItem object. The Apache software will have read the uploaded file from the request and put it into that object. So what you want to do is to read the data out of that object and copy it to the output stream.

Now your task is to see if FileItem has a method which returns an InputStream, or something similar which you can read from. So check the API documentation and see if there is one.

Let us know how you get on with that.
vijay kumar dahiya
Ranch Hand

Joined: Oct 19, 2008
Posts: 33
Dear Sir,
I read the org.apache.commons.fileupload
Interface FileItem have a method:
java.io.InputStream getInputStream()
Returns an InputStream that can be used to retrieve the contents of the file.

I write that in code:
UploadFile.java:

It give a error:
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42927
    
  68
Does the code have an import statement for the InputStream class?
vijay kumar dahiya
Ranch Hand

Joined: Oct 19, 2008
Posts: 33
Oh Sorry Sir,
firstly I used import java.io.File;
now I am using import java.io.*;
that error have remove.
Now I am try to effort to find desired result.I came back later if problem occurs.
Thanks.
vijay kumar dahiya
Ranch Hand

Joined: Oct 19, 2008
Posts: 33
Dear sir,
Servlet are not display proper image.Its show only a small icon.
Here my Home.html:


UploadFile.java: using that code snippet-
although InputStream are properly work.But Servlet not display a proper image.

Please help me..
[ December 12, 2008: Message edited by: vijay kumar dahiya ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42927
    
  68

This looks wrong. You need to read from the FileItem (using its getInputStream method), and write its bytes to the servlet output stream (res.getOutputStream).
vijay kumar dahiya
Ranch Hand

Joined: Oct 19, 2008
Posts: 33
Dear Sir,
I try this:
But use above code It not display on servlet,Its appear a downloaded form and downloaded on disk when click on save button.Other thing is that It not a .jpg form.that mean It does not open by photo gallery.that message show file format not supported by Photo Gallary.
when I add that line:
then servlet show a small icon not show a proper image.
and server side show:

please help me I try this program from a long time.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42927
    
  68
The call to res.reset() shouldn't be necessary. Is the code doing anything else with the response before the call to setContentType?

In general, for debugging problems with HTTP request and responses the Firefox extension LiveHTTPheaders is very helpful. It logs all requests sent by the browser, and also the responses it gets back.
vijay kumar dahiya
Ranch Hand

Joined: Oct 19, 2008
Posts: 33
Dear Sir,
Before call the setContentType("image/jpg");
Server side show:


and at run time It not display on servlet,Its appear a downloaded form and downloaded on disk when click on save button.Other thing is that It not a .jpg form.that mean It does not open by photo gallery.that message show file format not supported by Photo Gallary.
Please help..
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42927
    
  68
Oh wait:
<form method="post" action="upload.jsp" enctype="multipart/form-data">

Are you using a JSP for this? A JSP can't stream binary content (like an image).

But you said it was a servlet.
vijay kumar dahiya
Ranch Hand

Joined: Oct 19, 2008
Posts: 33
Oh,Sorry for that:
Actually Its My home.html file:
web.xml:
UploadFile.java

Problem are still persist.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42927
    
  68
Is the loop possibly being executed more than once? You can only send a single image per request.

To give it its proper filename upon saving, you need to add something like this:

If you want to display it instead, use "inline" instead of "attachment".
vijay kumar dahiya
Ranch Hand

Joined: Oct 19, 2008
Posts: 33
Dear Sir,
Yes, loop are executed twice when sending one request.
I used that:

But same problem exist.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42927
    
  68
Yes, loop are executed twice when sending one request.

Then you need to find out why it's executed twice, and either stop that from happening, or make sure that the code writing the image is only executed once.
[ December 13, 2008: Message edited by: Ulf Dittmer ]
vijay kumar dahiya
Ranch Hand

Joined: Oct 19, 2008
Posts: 33
Dear Sir,
I simply changing in Home.html.Now I send only one thing that is Image.
Now loop is executed only once.
Home.html:
UploadFile.java is same
But same problem exist.It not display on servlet,Its appear a downloaded form and downloaded on disk when click on save button.Other thing is that It not a .jpg form.that mean It does not open by photo gallery.that message show file format not supported by Photo Gallary.
please help me..
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42927
    
  68
If that's happening even though you're setting the Content-Disposition header, I'd use the LiveHTTPheaders extension I mentioned earlier to examine what, exactly, is being sent as the response.
vijay kumar dahiya
Ranch Hand

Joined: Oct 19, 2008
Posts: 33
Dear Sir,
Can you give me any example How I use LiveHTTPheaders extension or any reference of that.I try did at better at my level.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42927
    
  68
Its web site is the first link that comes up in Google for "livehttpheader". If you install it in Firefox it'll add a new menu item under "Tools". I don't think there's much documentation, but by playing around with it it should become clear what it does, and how it does it, pretty soon.
vijay kumar dahiya
Ranch Hand

Joined: Oct 19, 2008
Posts: 33
Dear Sir,
I install the Live HTTP headers 0.14 from Google in Firefox.
Also appearing a new menu of Live HTTP headers in Tools options.
I run the weblogic on Firefox and run that progarm but same issue persist.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42927
    
  68
What have you found out? LiveHTTPHeader is a debugging tool that can tell you what's happening over the HTTP wire, not a tool that fixes anything. Have you inspected the HTTP response headers?
vijay kumar dahiya
Ranch Hand

Joined: Oct 19, 2008
Posts: 33
Dear Sir,
Its not show a new thing.
Same previous.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42927
    
  68
What do you mean by "not a new thing"? You haven't told us what it showed before.

Post the response headers here; you can copy them straight out of the LiveHTTPHeader window. That might give us a clue as to what is going wrong.
vijay kumar dahiya
Ranch Hand

Joined: Oct 19, 2008
Posts: 33
Dear Sir,
I am using Live HTTP headers.So I am not know more about it.
I thing your mean

from Tools option have a Live HTTP headers menu.
when I click on Live HTTP headers new window of Live HTTP headers appears.
Its Text area show some matters.That matter I will paste here ?

Actually Sir,
I am not guess exactly what are you talking for.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42927
    
  68
While the LiveHTTPHeader window is open -make sure its "Capture" checkbox is checked- you upload the file in a different window in the same browser. The LiveHTTPHeader window should now be showing the HTTP request and the HTTP response; that is the main function of this extension. Copy the response headers and post them here.

Play around with it a bit so you get a feeling for what it does and how it shows the HTTP information. It's a very useful tool for anyone developing web applications.
[ December 13, 2008: Message edited by: Ulf Dittmer ]
vijay kumar dahiya
Ranch Hand

Joined: Oct 19, 2008
Posts: 33
Dear Sir,
At run time different window are open in the same browser.
"Capture" checkbox is also checked of LiveHTTPHeader window.
Of LiveHTTPHeader window have four tabs-Headers,Generator,Config,About.
Here I paste the matter of Header Tab.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42927
    
  68
That's just the page with the upload form. But the interesting part is actually doing an upload, and then observing the headers that get sent back along with the image.
vijay kumar dahiya
Ranch Hand

Joined: Oct 19, 2008
Posts: 33
Dear Sir,
That matter of Live HTTP headers after click on Submit button then open a Live HTTp headers window It show blank then downloading the image and open it but it show a image by Photo Gallery then I close the photo Gallery.After that that matter appears in Live HTTP headers window.
vijay kumar dahiya
Ranch Hand

Joined: Oct 19, 2008
Posts: 33
Dear Sir,
After click on Submit button,Headers Tab of Live HTTP headers window display blank.
But Generator Tab show that matter:


And some time Generator tab show that matter:


please help me..

[ UD: removed irrelevant stuff that was screwing up the layout ]
[ December 14, 2008: Message edited by: Ulf Dittmer ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42927
    
  68
That's the HTTP upload request. Right after the Upload should be a section starting with "HTTP/1.x 200 OK" - those would the response headers that are of interest here.

You may need to scroll through the LiveHTTPHeaders window; it fills up fast because it's logging a lot of information for each request/response.
[ December 14, 2008: Message edited by: Ulf Dittmer ]
vijay kumar dahiya
Ranch Hand

Joined: Oct 19, 2008
Posts: 33
Dear Sir,
Some time Generator tab of Live HTTP headers show that:



and some time show that:

Under Generator tab:
On Right side:
Under Show have four check boxes- redirects,invalid,images,CSS.images and CSS are checked.
Under Request have one check box request which is By default unchecked.

But Headers tab show empty.
[ December 14, 2008: Message edited by: Ulf Dittmer ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42927
    
  68
I'm not sure why the Headers tab would be empty. The contents of the Generator tab don't matter.
[ December 14, 2008: Message edited by: Ulf Dittmer ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: To know about File Upload