You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Zoran Avtarovski <zo...@conads.com> on 2004/09/23 15:11:36 UTC

File Upload Limits

I'm trying to find some detailed information on how to use the file size
limits using the struts MultipartRequestHandler. In the past I have used a
custom servlet, but I thought it was time to bring this into the struts
world.

Before anybody says anything, I tried to search the list archive but it says
it can't be searched.

So anyway I looked at the fileupload example, and have done the following:

Added the controller element
<controller maxFileSize="2M" inputForward="true" />

To my struts-config and in my UploadForm validate action I check the boolean
request.getAttribute(MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED);

I take it from this that the requesthandler sets a boolean value in the
request if the file is greater in size than specified in the controller
element.

The problem is that this isn't working.

Any help would be appreciated.

Zoran



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: File Upload Limits

Posted by Zoran Avtarovski <zo...@conads.com>.
Thanks Michael,

But I was under the impression that the Struts Uploader was just an
implementation of the commons uploader. I remember reading somewhere that
the two are the one and the same.

Either way, any help would be appreciated.

Zoran

> Zoran Avtarovski wrote:
> 
>> I'm trying to find some detailed information on how to use the file size
>> limits using the struts MultipartRequestHandler. In the past I have used a
>> custom servlet, but I thought it was time to bring this into the struts
>> world.
>> 
>> Before anybody says anything, I tried to search the list archive but it says
>> it can't be searched.
>> 
>> So anyway I looked at the fileupload example, and have done the following:
>> 
>> Added the controller element
>> <controller maxFileSize="2M" inputForward="true" />
>> 
>> To my struts-config and in my UploadForm validate action I check the boolean
>> request.getAttribute(MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED);
>> 
>> I take it from this that the requesthandler sets a boolean value in the
>> request if the file is greater in size than specified in the controller
>> element.
>> 
>> The problem is that this isn't working.
>> 
>> Any help would be appreciated.
>> 
>> Zoran
>> 
> I found the Struts uploader stuff to be generally not acceptable but am
> happy with the commons uploader .  Does that appeal to you?  If so, I
> can address the same question in that milieux.
> 
> Michael McGrady
> 



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: File Upload Limits

Posted by Michael McGrady <mi...@michaelmcgrady.com>.
Zoran Avtarovski wrote:

>I'm trying to find some detailed information on how to use the file size
>limits using the struts MultipartRequestHandler. In the past I have used a
>custom servlet, but I thought it was time to bring this into the struts
>world.
>
>Before anybody says anything, I tried to search the list archive but it says
>it can't be searched.
>
>So anyway I looked at the fileupload example, and have done the following:
>
>Added the controller element
><controller maxFileSize="2M" inputForward="true" />
>
>To my struts-config and in my UploadForm validate action I check the boolean
>request.getAttribute(MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED);
>
>I take it from this that the requesthandler sets a boolean value in the
>request if the file is greater in size than specified in the controller
>element.
>
>The problem is that this isn't working.
>
>Any help would be appreciated.
>
>Zoran
>
I found the Struts uploader stuff to be generally not acceptable but am 
happy with the commons uploader .  Does that appeal to you?  If so, I 
can address the same question in that milieux. 

Michael McGrady


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: File Upload Limits: CODE BLOAT

Posted by Michael McGrady <mi...@michaelmcgrady.com>.
Zoran Avtarovski wrote:

>I'm trying to find some detailed information on how to use the file size
>limits using the struts MultipartRequestHandler. In the past I have used a
>custom servlet, but I thought it was time to bring this into the struts
>world.
>
>Before anybody says anything, I tried to search the list archive but it says
>it can't be searched.
>
>So anyway I looked at the fileupload example, and have done the following:
>
>Added the controller element
><controller maxFileSize="2M" inputForward="true" />
>
>To my struts-config and in my UploadForm validate action I check the boolean
>request.getAttribute(MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED);
>
>I take it from this that the requesthandler sets a boolean value in the
>request if the file is greater in size than specified in the controller
>element.
>
>The problem is that this isn't working.
>
>Any help would be appreciated.
>
>Zoran
>

The CommonsMultipartRequestHandler in the struts package uses the 
commons upload classes FileItem, DiskFileUpload and 
FileUploadException.  None of the other classes in the struts package 
reference either commons upload classes or 
CommonsMultipartRequestHandler.  The CommonsMultipartRequestHandler is a 
default implementation of the struts MultipartRequestHandler used in the 
examples UploadForm extension of ActionForm.  I personally think an 
implementation of a subclass of MultipartIterator in strust is more 
useful.  But, whatever, I don't use the Struts solution.  I think, 
further, that this is a prime example of code bloat in struts.  This 
sort of thing should be readily available, of course, but it does not 
belong in the framework itself, in my opinion.  This is, I think, a poor 
application and will have to be maintained despite many better soutions 
bound to come.  I think my application itself is better and it will, no 
doubt, be eclipsed too.

Michael McGrady

Anyway, you can set your file size maximum, if that is what you want, 
using whatever implementation of Struts you conjure.  If you use the 
commons implementation, it is not the other way around, then you can use 
the commons stuff something like as follows:

import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;

import com.crackwillow.app.load.upload.UploadFileItem;
import com.crackwillow.app.load.upload.UploadConstant;
import com.crackwillow.log.StdOut;

public class UploadParser {

 public UploadParser() {
 }

 public void request(HttpServletRequest req,
                            Vector uploadListeners,
                            int fileSizeLimit,
                            Hashtable parameters,
                            Hashtable files,
                            String tmpFolder,
                            String encoding)
     throws IOException {

   DiskFileUpload diskFileUpload = new DiskFileUpload();
   diskFileUpload.setSizeMax(fileSizeLimit);
   diskFileUpload.setSizeThreshold(UploadConstant.MEMORY_BUFFER_SIZE);
   diskFileUpload.setRepositoryPath(tmpFolder);

   if(encoding != null) {
     diskFileUpload.setHeaderEncoding(encoding);
   }

   List list = null;

   try {
     list = diskFileUpload.parseRequest(req);
   } catch(FileUploadException fileuploadexception) {
     throw new IOException(fileuploadexception.getMessage());
   }

   Object obj = null;

   for(Iterator iterator = list.iterator(); iterator.hasNext();) {

'Hope this is helpful.

Michael McGrady



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: File Upload Limits [SOLVED]

Posted by Zoran Avtarovski <zo...@conads.com>.
I was using the FileUpload example as a starting point and I left the
inputForward="true" in my struts-config controller element. When I removed
it all worked file, but for now I'm happy it's working.

I'll have to have a look later at what exactly the inputForward="true"
attribute does.

But thanks to all for their replies.

Z.

> I'm trying to find some detailed information on how to use the file size
> limits using the struts MultipartRequestHandler. In the past I have used a
> custom servlet, but I thought it was time to bring this into the struts
> world.
> 
> Before anybody says anything, I tried to search the list archive but it says
> it can't be searched.
> 
> So anyway I looked at the fileupload example, and have done the following:
> 
> Added the controller element
> <controller maxFileSize="2M" inputForward="true" />
> 
> To my struts-config and in my UploadForm validate action I check the boolean
> request.getAttribute(MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED);
> 
> I take it from this that the requesthandler sets a boolean value in the
> request if the file is greater in size than specified in the controller
> element.
> 
> The problem is that this isn't working.
> 
> Any help would be appreciated.
> 
> Zoran
> 
> 



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: File Upload Limits

Posted by struts Dude <sc...@slingshot.co.nz>.
Hi David,

Let me get this straight. By using the way you described, if the file
to upload exceeds maximum allowable size, then no upload
will be proceeded right?

I only know how to upload the stupid way, i.e. upload the
whole file and abort if file size exceeds maximum.

Thanks


----- Original Message ----- 
From: "David G. Friedman" <hu...@ix.netcom.com>
To: "Struts Users Mailing List" <us...@struts.apache.org>
Sent: Friday, September 24, 2004 7:45 PM
Subject: RE: File Upload Limits


> Zoran,
>
> I'm on Struts v1.2.4 and it works as specified. Here is an outline of the
> key points I followed:
>
> 1) Made sure my <html:form ... /> includes enctype="multipart/form-data"
>
> 2) Make sure my ActionForm subClass contains a private property of the 
> type
> org.apache.struts.upload.FormFile with the appropriate get/set methods. 
> That
> isn't a class but an Interface used by the MultiPartRequestHandler to keep
> thing simple for you.
>
> 3) Use the html file tag: <html:file property="zip" />
>
> Where "zip" was my property (I'm uploading a .zip file) and getZip() and
> setZip() are my getter and setter, respectively.
>
> 4) Check in my Action's appropriate execute method for the existence of 
> the
> attribute you mentioned below.  I do that like so:
>
> // MultiPartRequestHandler is of class:
> // org.apache.struts.upload.MultipartRequestHandler
>
> Boolean b = (Boolean)
> request.getAttribute(MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED);
> if ( b != null ) {
> // You exceeded your Struts controller's
> // "maxFileSize" for uploads so let the
> // user know the upload was too large!
> }
>
> How did I test this?  By setting my struts-config.xml file's controller
> section and using a 10k .zip file:
>
> a) <controller maxFileSize="2K" />
>
> The boolean existed because my zip file was larger then that specified 2K
> limit.
>
> b) <controller maxFileSize="2M" />
>
> The boolean didn't exist because my zip file was under 2M in size.  I 
> could
> then read the file's name using my get method to obtain the FormFile 
> object
> and using it's getFileName() method.  Like so: getZip().getFileName();
> However, in practice you probably want to make sure a file was loaded 
> before
> you try to call that method or BOOM - exception, probably a
> NullPointerException.
>
> Regards,
> David
>
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: File Upload Limits

Posted by "David G. Friedman" <hu...@ix.netcom.com>.
Zoran,

I'm on Struts v1.2.4 and it works as specified. Here is an outline of the
key points I followed:

1) Made sure my <html:form ... /> includes enctype="multipart/form-data"

2) Make sure my ActionForm subClass contains a private property of the  type
org.apache.struts.upload.FormFile with the appropriate get/set methods. That
isn't a class but an Interface used by the MultiPartRequestHandler to keep
thing simple for you.

3) Use the html file tag: <html:file property="zip" />

Where "zip" was my property (I'm uploading a .zip file) and getZip() and
setZip() are my getter and setter, respectively.

4) Check in my Action's appropriate execute method for the existence of the
attribute you mentioned below.  I do that like so:

// MultiPartRequestHandler is of class:
// org.apache.struts.upload.MultipartRequestHandler

Boolean b = (Boolean)
request.getAttribute(MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED);
if ( b != null ) {
	// You exceeded your Struts controller's
	// "maxFileSize" for uploads so let the
	// user know the upload was too large!
}

How did I test this?  By setting my struts-config.xml file's controller
section and using a 10k .zip file:

a) <controller maxFileSize="2K" />

The boolean existed because my zip file was larger then that specified 2K
limit.

b) <controller maxFileSize="2M" />

The boolean didn't exist because my zip file was under 2M in size.  I could
then read the file's name using my get method to obtain the FormFile object
and using it's getFileName() method.  Like so: getZip().getFileName();
However, in practice you probably want to make sure a file was loaded before
you try to call that method or BOOM - exception, probably a
NullPointerException.

Regards,
David

-----Original Message-----
From: Zoran Avtarovski [mailto:zoran@conads.com]
Sent: Thursday, September 23, 2004 9:12 AM
To: Struts Users Mailing List
Subject: File Upload Limits


I'm trying to find some detailed information on how to use the file size
limits using the struts MultipartRequestHandler. In the past I have used a
custom servlet, but I thought it was time to bring this into the struts
world.

Before anybody says anything, I tried to search the list archive but it says
it can't be searched.

So anyway I looked at the fileupload example, and have done the following:

Added the controller element
<controller maxFileSize="2M" inputForward="true" />

To my struts-config and in my UploadForm validate action I check the boolean
request.getAttribute(MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED);

I take it from this that the requesthandler sets a boolean value in the
request if the file is greater in size than specified in the controller
element.

The problem is that this isn't working.

Any help would be appreciated.

Zoran



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org