users@jersey.java.net

Re: [Jersey] Problem while deploying my Jersey application

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Tue, 24 Nov 2009 14:41:42 +0100

On Nov 24, 2009, at 2:17 PM, Anil Kumar Veeramalli wrote:

> Thanks Paul.
> I declared my form in my jsp as ,
>
> <form action="/REST-POST-Demo/resources/filepost/form-data"
> method="POST" enctype="multipart/form-data">
> <input type="file" name="file" id="file" />
> <input type="submit" name="submit" value="upload" />
> </form>
>
> when I select a particular file and click on Upload, I just wanted
> to read the content of the file and print it on Console.
>
> How can I do that.
> bp.getContentDisposition().getParameters().get("name") is returning
> me the value as "file"
>

Yes, because that is the name of the control and not the name of the
file that is to be uploaded. If you want the file name you need to
obtain the "filename" parameter or call:

   bp.getContentDisposition().getFileName()

To get the content as a String do:

   String s = bp.getEntityAs(String.class)

or to get it as an InputStream do:

   InputStream is = bp.getEntityAs(InputStream.class)

Its all in the JavaDoc :-)


The sample i sent a link to (which i think you are using) enables
logging on the server side. So you take a look at the server-side log
you should see logs like the following:

2 > POST http://localhost:8080/fileupload/resources/upload/form-data
2 > content-length: 662
2 > accept-encoding: gzip,deflate
2 > referer: http://localhost:8080/fileupload/
2 > connection: keep-alive
2 > accept-language: en-us,en;q=0.5
2 > host: localhost:8080
2 > accept-charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
2 > user-agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US;
rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5
2 > content-type: multipart/form-data;
boundary=---------------------------12441158614839373711807801786
2 > cookie: JSESSIONID=65ed0804f6ac7f1c2ee23a950311
2 > accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/
*;q=0.8
2 > keep-alive: 300
2 >
-----------------------------12441158614839373711807801786
Content-Disposition: form-data; name="file"; filename="junk.txt"
Content-Type: text/plain
JUNK
-----------------------------12441158614839373711807801786
Content-Disposition: form-data; name="file"; filename="junk.txt"
Content-Type: text/plain
JUNK
-----------------------------12441158614839373711807801786
Content-Disposition: form-data; name="file"; filename="junk.txt"
Content-Type: text/plain
JUNK
-----------------------------12441158614839373711807801786
Content-Disposition: form-data; name="submit"
upload
-----------------------------12441158614839373711807801786--

Paul.