• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

MULTIPART/FORM-DATA

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All:

I'm trying to parse a multipart request in an action class using org.apache.commons.fileupload. The following code works fine inside a standard servlet, but in an action class upload.parseRequest(request) returns no items (items.size() is zero). Thanks in advance. Here is what the code looks like:

the jsp:

<%@ page language="java" %>

<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<html:html>
<head>
<title>File Upload</title>
<link rel="stylesheet" type="text/css" href="./css/style.css">
<script type='text/javascript' src='./js/script.js'></script>
</head>
<body>
<html:form action="/FileUpload" name="fileUpload" method="post" scope="request" type="gov.don.ex300.controller.form.FileUploadFormBean" enctype="MULTIPART/FORM-DATA">
<html:hidden property="Action" value="upload"/>
Select file to upload <html:file property="formFile"/>
<html:text property="test"/>
<input type="submit" value="Upload">
</html:form>
</body>
</html:html>

the action class method:

private ActionForward doUpload(FileUploadFormBean formBean, ActionMapping mapping, HttpServletRequest request, ActionErrors errors) {
boolean isMultipart = FileUpload.isMultipartContent(request);
if (isMultipart) {
DiskFileUpload upload = new DiskFileUpload();
List items = upload.parseRequest(request);

// Process the uploaded items
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (item.isFormField()) {

String name = item.getFieldName();
String value = item.getString();

} else {
String fieldName = item.getFieldName();
String fileName = item.getName();
}
}
}
return mapping.findForward("show");
}
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


In your struts-config.xml, is FileUploadFormBean named as "fileUpload"?
When using struts, you don't really need the name, scope and type in the <html:form> tag. All these should already be defined in the <action> tag in struts-config.xml.
 
Frank Griffith
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K,

The name of my form bean is fileUploadFormBean, although it's not being used at this point. I removed name and type from the form tag, but I'm still getting zero items out of the parsed request. Thanks for the help.

Frank
[ July 29, 2004: Message edited by: Frank Griffith ]
 
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please see example in %STRUTS DIR%/webapps/struts-upload.war

I think this example will help you to solve problem.
 
Frank Griffith
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've seen the struts example. I can upload a single file without a problem. The code I provided is essentially the sample code provided for the commons file upload. It does not work in a struts action class.
 
I'm all tasted up for a BLT! This tiny ad wants a monte cristo!
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!