Renaming uploaded images

Hi I have uploaded images to my server using common.FileUpload. Which works fine, I m uploadingmultiple images. My code is below.

1) Can anyone post some code snipets or where by I can change the name of images before it gets saved to the server? So that I could add that to my code.

2) in my form where (html), I have some hidden fields, however, request.getPrameter() returns null? Why is that happening? How do I pass other field with multidata forms?

<%

boolean isMultipart = ServletFileUpload.isMultipartContent(request);

if (!isMultipart){

}else{

FileItemFactory factory =new DiskFileItemFactory();

ServletFileUpload upload =new ServletFileUpload(factory);

List items =null;

try{

items = upload.parseRequest(request);

}catch (FileUploadException e){

e.printStackTrace();

}

Iterator itr = items.iterator();

while (itr.hasNext()){

FileItem item = (FileItem) itr.next();

if (item.isFormField()){

}else{

try{

String itemName = item.getName();

File savedFile =new File(config.getServletContext().getRealPath("/")+"uploadedFiles/"+itemName);

item.write(savedFile);

out.println("<tr><td><b>Your file has been saved at the loaction:</b></td></tr><tr><td><b>"+config.getServletContext().getRealPath("/")+"uploadedFiles"+"\\"+itemName+"</td></tr>");

}catch (Exception e){

e.printStackTrace();

}

}

}

}

%>

Thanks in advance.

[2692 byte] By [Me_mea] at [2007-11-27 11:58:14]
# 1

Visit this thread:

http://forum.java.sun.com/thread.jspa?forumID=45&threadID=440511

skp71a at 2007-7-29 19:18:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

cheers for the link, I see they are using import com.oreilly.servlet.MultipartRequest; and im using apache.common.fileupload? Are they similer can they work together.

Please tell me why I can't pass other parameter from the html form that image uploads.

I have a hidden field, but when I call request.getPramter(), its returning null?

Me_mea at 2007-7-29 19:18:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

Did you put the hidden field within <html:form></html:form>?

Try request.getAttribute().

skp71a at 2007-7-29 19:18:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

Hi,

Iam also using the same package. I too had the same problem. Since the form enctype="multipart/form-data" u cannot get the hidden value by using request.getParameter(). The following code is used to get the other input values.

while (iter.hasNext()) {

FileItem item = (FileItem) iter.next();

if (item.isFormField()) //This loop is used to get the values of other inputs fields in the form.

{

String name = item.getFieldName(); //This will get the field names. for eg. if u have a hidden field this line will get the hidden filed name.

String value = item.getString(); // this will give u the hidden filed value.

}

check the following link for further details.

http://jakarta.apache.org/commons/fileupload/using.html

thanua at 2007-7-29 19:18:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

chk this thread for more info

http://forum.java.sun.com/thread.jspa?threadID=329408&messageID=1343161

thanua at 2007-7-29 19:18:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...