aspose file tools*
The moose likes Servlets and the fly likes upload an avatar or other file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "upload an avatar or other file" Watch "upload an avatar or other file" New topic
Author

upload an avatar or other file

Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

I want a webpage/jsp to let the user specify a local file to upload to my web application/servlet/bean
I can't grok what I need to do. Code snippet:



This lets the client browser select the file. But all I get is the file name, not the contents.
Clearly I'm doing something wrong.

I don't know if my error is in the JSP/html or my servlet or bean or ...

Thanks
Pat
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
you need to add enctype="multipart/form-data" to your form tag.


JDBCSupport - An easy to use, light-weight JDBC framework -
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

See the JSP FAQ for the entry on file uploading.

[Edit:]
Here, in fact.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

Bear Bibeault wrote:See the JSP FAQ for the entry on file uploading.

I did that, and added the type as suggested upthread, so the code in the JSP looks like:



Loaded the Jakarta Commons FileUpload

However, when the POST is received, there are no arguments in the query string. and


is always false.

So I'm still missing something, probably fundamental.
thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

Pat Farrell wrote:However, when the POST is received, there are no arguments in the query string.

There won't be. All data is in the request body.



arg.getRequest()?

I'd use a tool like HttpFox at this point to see what's being sent in the request.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

The ServletFileUpload.isMultipartContent(); function wants a HttpRequest.

When one is using FrontMan, you have a CommandContext, and to get the request you call
arg.getRequest()

I'm not seeing anything in the body either. I'll look again.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

Ah, it was the "arg" that threw me -- I call it "commandContext".

I'm not seeing anything in the body either. I'll look again.

An empty request body would indicate a problem on the browser side of things.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

Bear Bibeault wrote:An empty request body would indicate a problem on the browser side of things.

Agree, I'm just using Firefox 3.5
Perhaps I should try Chrome or something else, altho in Ubuntu, Firefox and Chrome are usually enough.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

Pat Farrell wrote:
Bear Bibeault wrote:An empty request body would indicate a problem on the browser side of things.

Agree, I'm just using Firefox 3.5
Perhaps I should try Chrome or something else, altho in Ubuntu, Firefox and Chrome are usually enough.

I think you'll find that Firefox (any version you care to name) will upload files perfectly well. Blaming the browser isn't going to be a productive strategy for fixing your problem.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

Found the problem. Its not that FireFox is bad.

I did not understand than when you use the flag enctype="multipart/form-data" in the form, you no longer see any of the field values. They are all hidden in the special "multipart/form-data". So my testing for query parameters or other arguments failed.

Once I realized this, I could use the Apache test code:


and then branch to completely separate code to pull apart the "multi-part arguments.

I'm not happy with the cleanliness of the code, as I can't rely upon any ordering of the multi-part arguments, so I don't know if I have the name I want before I get the file name. But, the basics are working, and I can clean it up later. :-)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: upload an avatar or other file