Image Upload to a directory via a HTML form & Servlet?
Michael Cropper
Ranch Hand
Joined: Sep 30, 2009
Posts: 137
posted
0
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
Marshal
Joined: Mar 22, 2005
Posts: 35249
7
posted
0
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.
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
Marshal
Joined: Mar 22, 2005
Posts: 35249
7
posted
0
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.
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.
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.
Thanks for pointer, I will have a good read over that and post back if I have any questions.
Michael
Michael Cropper
Ranch Hand
Joined: Sep 30, 2009
Posts: 137
posted
0
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
Joined: Sep 30, 2009
Posts: 137
posted
0
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.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.
subject: Image Upload to a directory via a HTML form & Servlet?