wood burning stoves 2.0*
The moose likes Servlets and the fly likes jspsmartupload inside servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "jspsmartupload inside servlet" Watch "jspsmartupload inside servlet" New topic
Author

jspsmartupload inside servlet

Olivia Mayo
Greenhorn

Joined: May 11, 2003
Posts: 29
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?
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
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?


I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Olivia Mayo
Greenhorn

Joined: May 11, 2003
Posts: 29
other functions that access the database like add, edit and delete.
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
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
Well, I've used JSPSmartUpload with no problems at all... don't know how you've implemented it, but this code snippet could help you:

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...
cheers
Olivia Mayo
Greenhorn

Joined: May 11, 2003
Posts: 29
i apologize for not showing any code i tried the code that made but it still didn't work.
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)
{
try
{
this.req = req;
this.resp = resp;

//get session
session = req.getSession();

//get Form details
String action = (String)req.getParameter("action");
out("Action requested: " + action);

if(action == null)
{
resp.sendRedirect("../jsp/index.htm");

}
else if(action.equalsIgnoreCase("addproduct"))
{
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"));

this.uploadImage();

Product prod = new Product();
prod.getDBConnectionMgr(this.dbConnManager);
prod.setName(name);
prod.setDescription(desc);
prod.setImageFile(imageFile);
prod.setManufacturerID(mID);
prod.setProductTypeID(ptID);
prod.insertRecord();

resp.sendRedirect("ProductHandler?action=viewProducts");

}
}
catch (Exception e)
{
out("Error in doPost: ");
e.printStackTrace();
}

}//END 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();

try
{
//start mySmartUpload
mySmartUpload.initialize(this.getServletConfig(), this.req, this.resp);
mySmartUpload.setAllowedFilesList("gif,jpg,jpeg,GIF,JPG,JPEG");
mySmartUpload.setMaxFileSize(1500);
mySmartUpload.upload();
out("file uploaded");

for (int i=0;i<mySmartUpload.getFiles().getCount();i++)
{

com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(i);

if (!myFile.isMissing())
{
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();
while (enum.hasMoreElements())
{
String name = (String)enum.nextElement();
out("parameters in request: " + name);
String value = mySmartUpload.getRequest().getParameter(name);
profileProps.setProperty(name, value);
}
}
}
catch(Exception e)
{
e.printStackTrace();
}

... 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.
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
Hi Olivia...
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.
cheers
Olivia Mayo
Greenhorn

Joined: May 11, 2003
Posts: 29
thanks for your insight i'm reading up on multipart forms, i didn't know they were handled in a different way.
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.
Olivia Mayo
Greenhorn

Joined: May 11, 2003
Posts: 29
Thanks so much! Finally realized what was wrong. Thanks for telling me about the multipart form.
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!
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
You are welcome to javaranch !
 
GeeCON Prague 2014
 
subject: jspsmartupload inside servlet