To tell you the truth, Marwan, although there are different strokes for different folks, I find the Struts implementation of commons upload in CommonsMultipartRequestHandler not to my "taste". There seem to be continual problems with it as well. Maybe that is my bias. Don't know. Anyway, CommonsMultipartRequestHandler (which should be, shouldn't it (?), "StrutsMultipartRequestHandler") has the following code:


public void handleRequest(HttpServletRequest request) throws ServletException {

           // Get the app config for the current request.
           ModuleConfig ac = (ModuleConfig) request.getAttribute(
                   Globals.MODULE_KEY);

           // Create and configure a DIskFileUpload instance.
           DiskFileUpload upload = new DiskFileUpload();
           // The following line is to support an "EncodingFilter"
           // see http://nagoya.apache.org/bugzilla/show_bug.cgi?id=23255
           upload.setHeaderEncoding(request.getCharacterEncoding());
           // Set the maximum size before a FileUploadException will be
   thrown.
           upload.setSizeMax(getSizeMax(ac));
           // Set the maximum size that will be stored in memory.
           upload.setSizeThreshold((int) getSizeThreshold(ac));
           // Set the the location for saving data on disk.
           upload.setRepositoryPath(getRepositoryPath(ac));

           // Create the hash tables to be populated.
           elementsText = new Hashtable();
           elementsFile = new Hashtable();
           elementsAll = new Hashtable();

// Parse the request into file items.
List items = null;
try {
items = upload.parseRequest(request);
} catch (DiskFileUpload.SizeLimitExceededException e) {
// Special handling for uploads that are too big.
request.setAttribute(
MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED,
Boolean.TRUE);
return;
} catch (FileUploadException e) {
log.error("Failed to parse multipart request", e);
throw new ServletException(e);
}


           // Partition the items into form fields and files.
           Iterator iter = items.iterator();
           while (iter.hasNext()) {
               FileItem item = (FileItem) iter.next();

               if (item.isFormField()) {
                   addTextParameter(request, item);
               } else {
                   addFileParameter(item);
               }
           }
       }

This is not acceptable to me.  I prefer something like:



public void parse(HttpServletRequest req,
Vector uploadListeners,
int fileSizeLimit,
Hashtable parameters,
Hashtable files,
String tmpFolder,
String encoding)
throws IOException {
DiskFileUpload dfu = new DiskFileUpload();
dfu.setSizeMax(fileSizeLimit);
dfu.setSizeThreshold(UploadConstant.MEMORY_BUFFER_SIZE);
dfu.setRepositoryPath(tmpFolder);
if(encoding != null) {
dfu.setHeaderEncoding(encoding);
}
List list = null;
try {
list = dfu.parseRequest(req);
} catch(FileUploadException fue) {
throw new IOException(fue.getMessage());
}
Object obj = null;
for(Iterator iter = list.iterator(); iter.hasNext();) {
FileItem fi = (FileItem)iter.next();
String fieldName = fi.getFieldName();
if(fi.isFormField()) {
String holder = null;
if(encoding != null) {
holder = fi.getString(encoding);
} else {
holder = fi.getString();
}
Vector params = (Vector)parameters.get(fieldName);
if(params == null) {
params = new Vector();
parameters.put(fieldName, params);
}
params.addElement(holder);
} else {
String fin = fi.getName();


           if(fin != null) {
             UploadFileItem ufi = new UploadFileItem(fi);
             fin = fin.replace('\\', '/');
             int j = fin.lastIndexOf("/");
             if(j != -1) {
               fin = fin.substring(j + 1, fin.length());
             }
             ufi.setFileName(fin);
             ufi.setContentType(fi.getContentType());
             ufi.setFileSize(fi.getSize());
             files.put(fieldName, ufi);
           }
         }
       }
     }

This approach just grabs the list of FileItems in commons upload and wraps them inside your own version of FileItem (UploadFileItem in my case). You then store them as you see fit for use as you see fit. This is for me a simpler approach which is more flexible and easier to build applications around. Obviously, others smarter than I am like the Struts approach. You may have a third approach. I don't like to become involved with dependencies where unnecessary and see no need to bind a file upload application to Struts. If someone else likes something different, good for them.

Michael McGrady


Marwan Salam wrote:

My action is pasted below. As you will see I never call setMaxFile
(long) in my acdtion. From the stack trace it looks like Struts internal classes are doing the call: org.apache.struts.upload.CommonsMultipartRequestHandler.handleRequest
(CommonsMultipartRequestHandler.java:219)


These are the 2 lines that do the actual call from CommonsMultipartRequestHandler:
DiskFileUpload upload = new DiskFileUpload();
upload.setSizeMax((int) getSizeMax(ac));


That is not my code. I have tried both solutions mentioned before and they did not help.

I would appreciate any help.

Marwan


MaintainItemForm theForm = (MaintainItemForm) form;


//this line is here for when the input page is upload-utf8.jsp, //it sets the correct character encoding for the response String encoding = request.getCharacterEncoding(); if ((encoding != null) && (encoding.equalsIgnoreCase("utf-8"))) { response.setContentType("text/html; charset=utf-8"); }


//retrieve the text data

//retrieve the file representation
FormFile file = theForm.getMyFile();

//retrieve the file name
String fileName= file.getFileName();

//retrieve the content type
String contentType = file.getContentType();

//boolean writeFile = theForm.getWriteFile();

//retrieve the file size
String size = (file.getFileSize() + " bytes");

String data = null;

try {
//retrieve the file data
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream stream = file.getInputStream();
//write the file to the file specified
OutputStream bos = new FileOutputStream("/testImages");
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);
}
bos.close();
data = "The file has been written to \"" + "/testImages" + "\"";
logger.debug(data);
//close the stream
stream.close();
}
catch (FileNotFoundException fnfe) {
return null;
}
catch (IOException ioe) {
return null;
}


//place the data into the request for retrieval from display.jsp
// request.setAttribute("text", text);
// request.setAttribute("queryValue", queryValue);
request.setAttribute("fileName", fileName);
request.setAttribute("contentType", contentType);
request.setAttribute("size", size);
request.setAttribute("data", data);


//destroy the temporary file created
file.destroy();

//return a forward to display.jsp
return mapping.findForward("edit_success");


--- In [EMAIL PROTECTED], Michael McGrady <[EMAIL PROTECTED]> wrote:


The method setMaxFile(long) exists even in older versions. So, are

you

doing something other than tossing a long value into that method?

Michael McGrady

Travis Reeder wrote:



Looks like you need the latest commons-fileupload.jar.

Travis

-----Original Message-----
From: Marwan Salam [mailto:[EMAIL PROTECTED] Sent: Thursday, October 21, 2004 10:21 AM
To: [EMAIL PROTECTED]
Subject: FileUpload Errors



Hi all,

I am using Struts 1.1 and I am trying to upload an image file to

the


server but I keep getting the below exception. I have used the

example


that comes with Struts under jakarta-struts-1.1\webapps and did

exactly


like they have but still getting the exception. I have put print
statements all over the Action but the problem is that the request


is


not even getting to the action. No compile errors whatsoever. I

also


have commons-logging.jar in my WEB-INF\lib.

My JSP looks like:

<html:form action="maintainItem" enctype="multipart/form-data">

.
.
.
Select Image: <html:file name="maintainItemForm"


property="myFile"/>


<br> <html:submit onclick="dispatch.value='uploadImage'"

value="Upload


Image"/>
          .
          .
          .
</html:form>

I have defined the form in struts-config.xml and the request does

not


even hit the action.


[10/21/04 12:08:24:171 CDT] 6ea2663c RequestProces I org.apache.struts.action.RequestProcessor Processing a 'POST' for path '/maintainItem'
[10/21/04 12:08:24:936 CDT] 6ea2663c WebGroup E SRVE0026E: [Servlet Error]-[org.apache.commons.fileupload.FileUpload: method setSizeMax&#40;I&#41;V not found]: java.lang.NoSuchMethodError: org.apache.commons.fileupload.FileUpload: method setSizeMax(I)V


not

found
at


org.apache.struts.upload.CommonsMultipartRequestHandler.handleRequest


(CommonsMultipartRequestHandler.java:219)
at org.apache.struts.util.RequestUtils.populate
(RequestUtils.java:1055)
at org.apache.struts.action.RequestProcessor.processPopulate
(RequestProcessor.java:798)
at org.apache.struts.action.RequestProcessor.process
(RequestProcessor.java:254)
at org.apache.struts.action.ActionServlet.process
(ActionServlet.java:1422)
at org.apache.struts.action.ActionServlet.doPost
(ActionServlet.java:523)
at javax.servlet.http.HttpServlet.service
(HttpServlet.java:760)
at javax.servlet.http.HttpServlet.service
(HttpServlet.java:853)
at com.ibm.ws.webcontainer.servlet.StrictServletInstance.doService
(StrictServletInstance.java:110)
at com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet._service
(StrictLifecycleServlet.java:174)
at com.ibm.ws.webcontainer.servlet.IdleServletState.service
(StrictLifecycleServlet.java:313)
at com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet.service
(StrictLifecycleServlet.java:116)
at com.ibm.ws.webcontainer.servlet.ServletInstance.service
(ServletInstance.java:283)
at com.ibm.ws.webcontainer.servlet.ValidServletReferenceState.dispatch
(ValidServletReferenceState.java:42)
at com.ibm.ws.webcontainer.servlet.ServletInstanceReference.dispatch
(ServletInstanceReference.java:40)
at


com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.handleWebAppDi


s


patch(WebAppRequestDispatcher.java:948)
at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.dispatch
(WebAppRequestDispatcher.java:530)
at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.forward
(WebAppRequestDispatcher.java:176)
at com.ibm.ws.webcontainer.srt.WebAppInvoker.doForward
(WebAppInvoker.java:79)
at com.ibm.ws.webcontainer.srt.WebAppInvoker.handleInvocationHook
(WebAppInvoker.java:201)
at


com.ibm.ws.webcontainer.cache.invocation.CachedInvocation.handleInvoc


a


tion(CachedInvocation.java:71)
at com.ibm.ws.webcontainer.srp.ServletRequestProcessor.dispatchByURI
(ServletRequestProcessor.java:182)
at com.ibm.ws.webcontainer.oselistener.OSEListenerDispatcher.service
(OSEListener.java:334)
at com.ibm.ws.webcontainer.http.HttpConnection.handleRequest
(HttpConnection.java:56)
at com.ibm.ws.http.HttpConnection.readAndHandleRequest
(HttpConnection.java:610)
at com.ibm.ws.http.HttpConnection.run(HttpConnection.java:435)
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:593)


Any help would be appreciated.

Marwan





-------------------------------------------------------------------


--


To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




-------------------------------------------------------------------


-----------


This e-mail message is for the sole use of the intended recipient


(s) and contains confidential and/or privileged information belonging to Siebel Systems, Inc. or its customers or partners. Any unauthorized review, use, copying, disclosure or distribution of this message is strictly prohibited. If you are not an intended recipient of this message, please contact the sender by reply e-mail and destroy all soft and hard copies of the message and any attachments. Thank you for your cooperation.


====================================================


-------------------------------------------------------------------


--


To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]









--------------------------------------------------------------------


-


To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]






---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]









--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to