Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Image Upload to a directory via a HTML form & Servlet?

 
Michael Cropper
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have been reading all about this for the past few hours but I don't seem to be getting anywhere. I am wanting to upload an image from a clients computer to the web app via a HTML form and a Servlet handling the incoming image.

To keep things simple, this is the only thing I am doing on the form and I am using the package from Apache FileUpload.

Here is the HTML form


And here is one of many pieces of code I have tried and haven't worked.



The issue seems to be on the line "List items = upload.parseRequest(request);"

and it is throwing the following error....



Can anyone point me in the right direction here as I have read various tutorials, read the official documentation here, and I am still none the wiser about how I need to go about fixing this.

Any help appreciated.

Thanks
Michael


 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In addition to the Commons FileUpload library you also need the Commons I/O library. That's what the last sentence of the "Using FileUpload" section on the page you linked to is talking about.
 
Michael Cropper
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much for the speedy and useful info, the problem is now fixed!

For future debugging, does the error "java.lang.NoClassDefFoundError:" simply mean - the class/JAR file cannot be found?

Thanks
Michael

 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep, the "Def" is for "definition" - which is found in the class file. And any class like "org/apache/commons/..." can be found somewhere around http://commons.apache.org/. If you haven't checked out all the libraries available there - you should, there's excellent stuff to be found.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The javadoc is your friend:
Thrown if the Java Virtual Machine or a ClassLoader instance tries to load in the definition of a class (as part of a normal method call or as part of creating a new instance using the new expression) and no definition of the class could be found.

The searched-for class definition existed when the currently executing class was compiled, but the definition can no longer be found.


 
Michael Cropper
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great thanks for info

Just one final question related to file uploads, what would you suggest is the best option to ensure filenames don't clash?

I was thinking about just appending a unique ID at the end of the file name so "test.jpg" would turn into "test-123456789.jpg" which would solve the issue (as I want all of the files in one directory, not in separate folders within folders).

Although my only concern there is how I would track unique IDs.

Any experience on that one?

Thanks
Michael
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Cropper wrote:Just one final question related to file uploads, what would you suggest is the best option to ensure filenames don't clash?


java.io.File #createTempFile
 
Michael Cropper
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for pointer, I will have a good read over that and post back if I have any questions.

Michael
 
Michael Cropper
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Had a good play around with the method you mentioned and it was catching the error when there was a duplicate filename, although it was still uploading the image - both when I tried the image upload script within the try/catch block and outside of it - so not sure what to do best there?

I have actually got around this issue by having a naming convention programatically to ensure filenames are always renamed, but it would be good to know where I was going wrong with the what I tried using #createTempFile

Thanks
Michael
 
Michael Cropper
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There doesn't seem to be any easy way to upgrade this code to handle a parameter as well as a file.

Ideally I would like to pass across a parameter eg "imagetype=123" and the file, so that when in the Servlet I can make the filename based on what type of image it is (based on the parameter).

Looking at the various tutorials online, there doesn't seem to be much that covers how to do this - has anyone had any experience of this before?

Worst case scenario is that I will have to break the golden rule of OO programming and just have different Servlets to upload different types of images.

Thanks
Michael

Note, when I say 'image type' I mean the content of the image, not the file extension.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic