Originally posted by Olivia Mayo:
i'm using the jspsmartupload to upload files. when i make it into a servlet which handles uploading only, it works fine, however, if i use the SmartUpload/do the uploading inside a servlet with other functions, it doesn't work. I checked the request, response and servlet configuration being passed and they not empty but when i call the upload function, it doesn't seem to be uploading the files. what's happening inside?
what other functions?
the call will be coming from a form which takes in other parameters plus the file I'm uploading.
so when i click submit, it's suppose to send details of a product plus the image i uploaded to the servlet. the add function is suppose to take care of uploading the file and adding the details (including the name of the image) to the database
and whenever I need to get a value I do
Again, I'm just playing with my imagination, since you did not post any code snippet, but maybe this one can help you a bit...
here's my code ...
inside the doPost i get the values of the request and response and assign it to their corresponding Http objects.
public void doPost(HttpServletRequest req, HttpServletResponse resp)
this.req = req;
this.resp = resp;
session = req.getSession();
//get Form details
String action = (String)req.getParameter("action");
out("Action requested: " + action);
if(action == null)
String name = (String)req.getParameter("productName");
String desc = (String) req.getParameter("description");
String imageFile = (String)req.getParameter("file");
int mID = Integer.parseInt(req.getParameter("manufacturerID"));
int ptID = Integer.parseInt(req.getParameter("productTypeID"));
Product prod = new Product();
catch (Exception e)
out("Error in doPost: ");
... this is outside the doPost and is called when i add a product
public void uploadImage() throws ServletException
SmartUpload mySmartUpload = new SmartUpload();
Properties profileProps = new Properties();
mySmartUpload.initialize(this.getServletConfig(), this.req, this.resp);
for (int i=0;i<mySmartUpload.getFiles().getCount();i++)
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(i);
myFile.saveAs(articlePath + myFile.getFileName(), mySmartUpload.SAVE_PHYSICAL);
out("saved file: " + myFile.getFileName());
//get the form fields as an enumeration. Then it associates with name/value pair
Enumeration enum = mySmartUpload.getRequest().getParameterNames();
String name = (String)enum.nextElement();
out("parameters in request: " + name);
String value = mySmartUpload.getRequest().getParameter(name);
... when i run the program, it's able to print out "file uploaded" (which is inside the uploadImage function) however it does not go through the enumeration, and does not print the parameter names. when i check the physical path to where the images are saved, there's nothing saved.
I had a similar problem that might be the same as yours. When you use multipart in a form you work with the request completely different that a *normal* form (withouth the multipart). At the beginning I was trying to access my form elements just as you are doing it, but they were all null.. then I realize that packages like the Oreilly ones or in our case JSPSmartupload entirely handle that. that's why I posted this code:
as you can see I am not doing :
Also, I don't know why you do this:
I don't think you need to do that.
So, for instance in your doPost you have a line like this one:
I don't think you get any value because, again, your form is a multipart one.
actually, i do get values when i use the request.getParameter("parameterName") function. i checked it against the database and i was able to save the parameters i entered to the form. it's the upload part that's not working.
and i did the this.req = req/ this.resp = resp so it would be easy for me to access them when i initialize the SmartUpload object.
As I already had an existing form, I just added the file field and did not bother to add the encryption type in the form tag. that's why i was getting values from the request, it was reading the form as a "normal" form. changed it to multipart and used your method of getting parameters and it worked fine!
Thanks a million!