• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

How to Actually UpLoad ?

 
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Everyone!

I hope you're all having a Blast.
Well, I'm attempting to do something new today, I'm trying to upload a File to the Server, but so far have Not been successful. Basically, I wrote a JSP with a Single Form, which contains a File Loader, and a Submit button...


And I have a Servlet to process whatever File that comes in the request...(I called it that way, because I'm planning to load pictures)


The Problem is that the above code creates an Empty File(length = 0). I noticed that the parameter obtained for "file1" Is the physical location where the File is to be located. (/home/{GLASSFISH-HOME}/domains/domain1/config for GlassFish)

What I want to Know is... What are the Steps To Actually UpLoad The File to said Location ? (Get The Picture From Client's Physical Location to Server's Physical location, right?)

I'm truly clueless on this one,

I hope You guys can Help me Out.
Sincerely,

Jose
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You seem to be under the impression that an object of type File is transferred, but it's not that simple. You need a library like http://commons.apache.org/fileupload/ on the server to handle this.
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ulf !

Thanks for the Quick Response.

Are you saying that there's no way I can do it with JSE, JEE Standard APIs only, and I Must use some third party Libraries?

That would be really disappointing, And would make me real sad, because I always try to keep my web-apps Third-party Free, BUT specially because I wanted to learn How to Code it myself.

Please don't tell me there's no way. I really want to learn how to do this.

Hope to read your next comments soon,

Sincerely,

Jose
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course there's a way to do it without any 3rd party libraries - after all, the FileUpload library was written without the help of any other library, so that shows that's possible.

It's cool that you want to learn, but this is a tricky subject which will require you to understand the relevant HTTP specifications in much detail. That's not fun in my book.

I always try to keep my web-apps Third-party Free


I'd strongly urge you to reconsider that if you value your time at all. There are so many excellent debugged libraries out there, that it seems a shame to reinvent them all.
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!

I was thinking and.... To me, it's like this... If I learn how to do it myself, then the time I invested will never be wasted.
So, Where Do you say I could find free resources with Information on how to do this?

I appreciate all your responses.

Keep up the great work,

Jose
 
Sheriff
Posts: 67754
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just adding my voice to Ulf's. I'm not one to just grab a 3rd-party library willy-nilly. I carefully evaluate what's best to do myself and what's a waste of time. This, in particular, is one areas where it's just not worth trying to wade through the multi-part form parsing on your own.
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the intervention Bear.

I just wanted to say, that, you can trust me, Even If it sounds illogical to do it on my own, I still want to. I need to. For the Academic Knowledge I will acquire.

Hopefully there's some place out there that contains the info. to get me started.

Once again... Thank you very much for the comprehension.

Jose
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For starters, you'll want to get a handle on the relevant specs: http://faq.javaranch.com/java/SpecificationUrls#httpRfc

HTTP 1.0 and -especially- Form-based File Upload should get you started. They may point you to further reading.
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In general, the libraries offered by Apache Commons are very useful. It is not to be compared with an average "3rd party" library. First there is Sun Java and then there is Apache Commons and then there are the 3rd party stuff ;)

Commons FileUpload is an excellent choice. It is thoroughly developed and tested and still actively supported.

If you want to handle the upload only for learning purposes (I repeat: only for learning purposes), then you need to read the HTML specification about the input type="file" element and the HTTP specification about the "multipart/form-data" request encoding. Then you should understand how it works under the hood and you should understand what to do with the InputStream of the HttpServletRequest.

http://www.w3.org/TR/html401/interact/forms.html (how to do it in the client side).
http://www.ietf.org/rfc/rfc2388.txt (how to do it in the server side).
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello There !

I will have a look at the specs you've given me. and I will probably have to read a lot more I suppose. I know I will come up with more questions later, But for now, I must say I'm glad to know it's a 'complex' subject the one I'm currently trying to solve, so at the end of the day my abilities will be improved nonetheless.

Thank you Ulf.
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, Thank you Bauke, your information is useful as well.
I think I will eventually end up using Commons FileUpload, but for now I will center my efforts on trying to do this on my own; Like you have adequately put, It's only for learning purposes, so, I believe it's right.

Have a Nice Day you all.

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am too uploading a file, using nothing more than plain old Java (no 3rd party libraries)
and must tell you its fun...
For testing purposes, try uploading a text file, ready from the input stream, and print everything on the console,
i think you will get hold of things on the first attempt itself, anyways, to give you an insight is very important, while reading myltipart encoded forms.

println(), is my best friend, enjoy the
cheers !!!
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bageshwar,

HI !

Man, You gotta show some me code. PLEASE PLEASE ! Could you?
I really want to see how you do it.... sounds really amazing If you can actually pull it off in that way.

Could you post the code of your Servlet? (Because I don't actually understand where do the small pieces of code you just posted come into play)

Sincerely,

Jose
 
Bageshwar Pratap Narain
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



This code has the following pre requisite..
The form only has the "file upload" field, no other form elements are present in the page.

Now what this code is doing is this....
Acquiring the input stream ,
reading useless http header data ( you'd love to have a look at this data in the printlns)

Create a file at the server,
pass the ServletInputStream to the FileOutputStream, and do the usual read and write.
Note : if you have more than 1 form elements in your client code, the number of "read and flush" code lines will increase, ( and this implementation is the worst coding example) as far as the conventions of Java are concerned.
But its very informative, and since you are in control of the client code, this would pose no problems to you
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdi Partner,
Bageshwar, Thank you, Thank you, Thank you...

It's amazing to see that it can actually be done, you don't know how much it means to me. Wow....

I think it's going to take a while before I can get this code to work, I'll study it, apply it, and then eventually and probably will have some questions. I hope I can trouble you in the future with them....

For now...
I can't find the correct words to express my gratitude.

Best Regards,

Jose
 
Bageshwar Pratap Narain
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jose Campana wrote:
I think it's going to take a while before I can get this code to work, I'll study it, apply it, and then eventually and probably will have some questions. I hope I can trouble you in the future with them....

For now...
I can't find the correct words to express my gratitude.

Best Regards,

Jose



Mention not "Partner" !!!
hope I solve your problems
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alright,

As I said before, eventually I'd come up with some questions...

Here's one:

How do you know the length of the headers ? I mean, You read bytes 4 times for a length of 1024. (For the 4 elements in the header info. I'm assuming).
(Where did you learn this was the way to go?)

After that you proceed with reading the Actual message.; You start From position 0. And the code it's pretty straight-forward from then on. BUT.....

IF I DON'T PREVIOUSLY READ THE HEADER INFO, THE FILE CREATION FAILS.
Why is this?

I mean If I comment this lines:



The File creation that comes next Fails, Could you explain to me a little more about your code.
Thanks in advance,

Sincerely,

Jose

PD. Sorry for all the trouble.

 
Bageshwar Pratap Narain
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jose Campana wrote:Alright,

How do you know the length of the headers ? I mean, You read bytes 4 times for a length of 1024. (For the 4 elements in the header info. I'm assuming).
(Where did you learn this was the way to go?)

After that you proceed with reading the Actual message.; You start From position 0. And the code it's pretty straight-forward from then on. BUT.....



1024 is the buffer size, it wont always read 1024 bytes.
The method employed for reading is readLine(), which reads till the next "\r\n" (thats why its important, i mentioned earlier) .
The initial code, just reads the first 4 lines of the POST ( these contain useless info, which i came to know by some reading , and observing the printlns. The actual contents of the file follow after this forth line.


IF I DON'T PREVIOUSLY READ THE HEADER INFO, THE FILE CREATION FAILS.
Why is this?

I mean If I comment this lines:



The File creation that comes next Fails, Could you explain to me a little more about your code.



Since you are writing to an image ( the data follows after the 4 lines of HELLO-HI),
if you add this text to the image file, the file becomes currupt.
Thats why i was saying to use a text file for testing, their you would know exactly at which line does you file data start, and thus you can skip the non-required lines.

Its just about knowing, at which line of the message, your file content will start

 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Correct, I get it now.
It'll always be 4 lines of useless info. Interesting.
That's all I needed to know for now.

Well, actually, What did you mean when you said:

Since you are writing to an image ( the data follows after the 4 lines of HELLO-HI),



What are the HELLO-HIs, you're talking about here? Excuse my ignorance If it's an obvious-response Question.

Thanks for everything man, You saved my life.

Jose
 
Bageshwar Pratap Narain
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

What are the HELLO-HIs, you're talking about here? Excuse my ignorance If it's an obvious-response Question.



These are the 4 lines of HTTP information that we are stripping !!!

If you would comment the code, where that data is read four times and neglected,
that that will lead the code to write everything it has in the ServletInputStream to the FileOutputStream.

and that will include this plain text, which will currupt the image file

Got That ???

 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Sir, I got it.....

Actually I did Before, But..... I just didn't understand why you called them 'HELLO-HI'

There's one last Question I must do:

I'm running this web application locally, so it is VERY FAST. Should I expect it to be considerably Slower once it's used over the Internet, and Upload Large Files?

Good job ! I owe you so much.
Thank you very, very much Sir, and

Good Luck,

Jose

 
Bageshwar Pratap Narain
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I'm running this web application locally, so it is VERY FAST. Should I expect it to be considerably Slower once it's used over the Internet, and Upload Large Files?




yes definately


Important when uploading large files, better use something like AJAX, to show the progress of upload, or else it becomes very frustrating for the end user to know whats happening, and in a frenzy h may push the refresh button uselessly...
spoiling the session.
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good !

Excellent !

It's been an Incredible learning Session Master. Thanks for all the invaluable advice. I hope we can stay in touch. For me what is left to be said is that, 'Thanks' can't say much for the actual gratitude I have for you.

Sincerely, Thanks for taking the time. God Bless you.

Until Next time,

Jose.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic