I am not able to get values from both files and text input in a servlet when my form includes multipart/form-data. I am using the apache.commons.fileuploads for help with the uploads. Any suggestions. Also in the code below there are some things that I feel should be more efficient. Is there a better way to store these multiple files in a db.
public void performTask(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
{
boolean promo = false;
Database db = new Database();
Homepage hp = db.getHomePageContents();
String part = ParamUtils.getStringParameter(request, "part", "");
if(part.equals("verbage"))
{
String txtcontent = (String)request.getParameter("txtcontent");
String promoheader = (String)request.getParameter("promoheader");
String promosubheader = (String)request.getParameter("promosubheader");
hp.setBodyText(txtcontent);
hp.setPromoHeader(promoheader);
hp.setPromoSubHeader(promosubheader);
System.err.println(txtcontent);
}
else
{
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);
//System.err.print(items);
} catch (FileUploadException e) {
e.printStackTrace();
}
Iterator itr = items.iterator();
while (itr.hasNext()) {
FileItem item = (FileItem) itr.next();
if(item.getFieldName().equals("mainimg1"))
{
if(item.getName() !="") hp.setMainImg1(item.getName());
}
if(item.getFieldName().equals("mainimg2"))
{
if(item.getName() !="") hp.setMainImg2(item.getName());
}
if(item.getFieldName().equals("mainimg3"))
{
if(item.getName() !="") hp.setMainImg3(item.getName());
}
if(item.getFieldName().equals("promoimg1"))
{
promo = true;
if(item.getName() !="")
{
hp.setPromoImg1(item.getName());
try {
File savedFile = new File("/Library/resin-4.0.1/webapps/ROOT/images/promoImg1.jpg");
item.write(savedFile);
//System.err.print(items);
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}
if(item.getFieldName().equals("promoimg2"))
{
if(item.getName() !="")
{
hp.setPromoImg2(item.getName());
try {
File savedFile = new File("/Library/resin-4.0.1/webapps/ROOT/images/promoImg2.jpg");
item.write(savedFile);
//System.err.print(items);
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}
if(item.getFieldName().equals("promoimg3"))
{
if(item.getName() !="")
{
hp.setPromoImg3(item.getName());
try {
File savedFile = new File("/Library/resin-4.0.1/webapps/ROOT/images/promoImg3.jpg");
item.write(savedFile);
//System.err.print(items);
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}
System.err.println("FNAME =" + item.getFieldName() + " : " + item.getName());
if (item.isFormField()) {
}
else {
try {
if(!promo)
{
String itemName = item.getName();
File savedFile = new File("/Library/resin-4.0.1/webapps/ROOT/images/"+itemName);
item.write(savedFile);
}
//System.err.print(items);
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}
}
}
db.updateHomePageContent(hp);