struts2文件上传大小

--struts2中文件上传的二个限制,一个是struts.multipart.maxSize,如果不设置,struts2 的核心包下的default.properties文件里有默认的大小设置struts.multipart.maxSize=2097152,即2M. 这是struts2文件上传的第一道关.

第二道关是inteceptor中的maximumSize. 当真实的文件大小能通过第一道关时.针对不同的action中配置的inteceptor,maximumSize才能发挥相应的拦截作用.

比如struts.multipart.maxSize=50M. actionA中inteceptorA的maximumSize=30M. actionB中inteceptorB的maximumSize=10M.

struts.multipart.maxSize=50M对于inteceptorA,B都会起到第一关的作用.而inteceptorA和inteceptorB可以在通过第一关之后,根据自己的业务定制各自针对拦截器起作用的maximumSize

如果真实的文件>50M. 抛出会抛出the request was rejected because its size (XXXX) exceeds the configured maximum (XXXX)异常,他是不能被国际化的,因为这个信息是commons-fileupload组件抛出的,是不支持国际化这信息.

struts2.2 org.apache.commons.fileupload.FileUploadBase.java中

 
  
/**
* Creates a new instance.
*
@param ctx The request context.
*
@throws FileUploadException An error occurred while
* parsing the request.
*
@throws IOException An I/O error occurred.
*/
FileItemIteratorImpl(RequestContext ctx)
throws FileUploadException, IOException {
if (ctx == null ) {
throw new NullPointerException( " ctx parameter " );
}

String contentType
= ctx.getContentType();
if (( null == contentType)
|| ( ! contentType.toLowerCase().startsWith(MULTIPART))) {
throw new InvalidContentTypeException(
" the request doesn't contain a "
+ MULTIPART_FORM_DATA
+ " or "
+ MULTIPART_MIXED
+ " stream, content type header is "
+ contentType);
}

InputStream input
= ctx.getInputStream();

if (sizeMax >= 0 ) {
int requestSize = ctx.getContentLength();
if (requestSize == - 1 ) {
input
= new LimitedInputStream(input, sizeMax) {
protected void raiseError( long pSizeMax, long pCount)
throws IOException {
FileUploadException ex
=
new SizeLimitExceededException(
" the request was rejected because "
+ " its size ( " + pCount
+ " ) exceeds the configured maximum "
+ " ( " + pSizeMax + " ) " ,
pCount, pSizeMax);
throw new FileUploadIOException(ex);
}
};
}
else {
// /问题就在这里
if (sizeMax >= 0 && requestSize > sizeMax) {
throw new SizeLimitExceededException(
" the request was rejected because its size ( "
+ requestSize
+ " ) exceeds the configured maximum ( "
+ sizeMax + " ) " ,
requestSize, sizeMax);
}
}
}

String charEncoding
= headerEncoding;
if (charEncoding == null ) {
charEncoding
= ctx.getCharacterEncoding();
}

boundary
= getBoundary(contentType);
if (boundary == null ) {
throw new FileUploadException(
" the request was rejected because "
+ " no multipart boundary was found " );
}

notifier
= new MultipartStream.ProgressNotifier(listener,
ctx.getContentLength());
multi
= new MultipartStream(input, boundary, notifier);
multi.setHeaderEncoding(charEncoding);

skipPreamble
= true ;
findNextItem();

如果InteceptorA上传的是40M的真实文件.那么此时拦截器InteceptorA会访问国际化信息:struts.messages.error.file.too.larges对应的值.

当且仅当上传文件<=30M的时候,InteceptorA才会成功上传.

下面是解决struts.multipart.maxSize提示信息不友好的问题.

当超过50M时.commons-fileupload抛出运行时异常,struts2会把这个异常看到是action级别的异常.所以会将异常信息

the request was rejected because its size (XXXX) exceeds the configured maximum (XXXX)写到actionError里面.我们需要做的就是在action里覆盖addActionError方法

 
  
@Override
public void addActionError(String anErrorMessage) {
//改从国际化里取值
if (anErrorMessage
.startsWith(
" the request was rejected because its size " )) {
super .addActionError(getText( " struts.multipart.maxSize.limit " ));
}
else {
super .addActionError(anErrorMessage);
}
}

相应的配置文件

struts.multipart.maxSize.limit=系统上传的文件最大为50M
struts.messages.error.file.too.larges=新广告批量上传的文件最大为5M
struts.messages.error.content.type.not.allowed=上传的文件格式目前仅支持xls格式
struts.messages.error.uploading=上传文件失败
struts.messages.invalid.token=您已经提交了表单,请不要重复提交。
fileupload.filenums.exceed=已经有超过5个文件在运行,请稍候再试
filedownload.rows.exceed=由于您选择的广告组内广告数量太多,请分组下载
accountNotExist=客户不存在
invalidTask=无效的任务

注意,由于inteceptor中途返回,原来页面上输入的其他文本内容也都不见了,也就是说params注入失败。

 

这个是没办法的,因为这个异常是在文件上传之前捕获的,文件未上传,同时params也为注入,所以这时最好重定向到一个jsp文件,提示上传失败,然后重写填写相应信息。

解决办法:最好跳到一个专门显示错误的页.而不要返回操作页.

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

注意,拦截器所谓的同名配置覆盖,是重复执行的,比如defaultStack中是包含fileUpload,token的. 如果将<interceptor-ref name="defaultStack" />放到显示定义的拦截器之后,会覆盖显示定义的拦截器.

下面是正确的拦截器顺序:

 
  
< action name ="BatchMIADOperation!*" method ="{1}"
class
="com.*****.***.action.multiidea.batchad.BatchMIADOperationAction" >
< interceptor-ref name ="defaultStack" />
< interceptor-ref name ="fileUpload" >
< param name ="maximumSize" > 5242880 </ param >
<!--
<param name="allowedTypes">
application/vnd.ms-excel
</param>
-->
</ interceptor-ref >
< interceptor-ref name ="token" >
< param name ="excludeMethods" >
init,search,updateBatchCpcMatch,batchExportMIAD,downloadWhenError
</ param >
</ interceptor-ref >
< result name ="input" >
/WEB-INF/jsp/multiidea/batchad/BatchMIAD.jsp
</ result >
< result name ="success" >
/WEB-INF/jsp/multiidea/batchad/BatchMIAD.jsp
</ result >
< result name ="invalid.token" >
/WEB-INF/jsp/multiidea/batchad/BatchMIAD.jsp
</ result >
</ action >

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Struts2本身并不提供上进度条的功能,但可以使用一些第三方插件来实现。 一种实现方式是使用jQuery插件uploadify,它可以在页面上显示上进度条。步骤如下: 1. 引入jQuery和uploadify的js和css文件。 2. 在页面上定义一个文件的input控件,并将其与uploadify关联。 ```html <input id="file_upload" name="file_upload" type="file" multiple="true"> ``` ```javascript $(function() { $('#file_upload').uploadify({ 'swf': 'uploadify.swf', 'uploader': 'uploadify.php', 'multi': true, 'auto': true, 'fileSizeLimit': '1024MB', 'onUploadProgress': function(file, bytesUploaded, bytesTotal, totalBytesUploaded, totalBytesTotal) { // 计算上进度并更新进度条 var percent = totalBytesUploaded / totalBytesTotal * 100; $('#progress').css('width', percent + '%'); } }); }); ``` 3. 在页面上定义一个进度条控件,用于显示上进度。 ```html <div id="progress-bar"> <div id="progress"></div> </div> ``` 4. 在服务器端处理文件,并返回上进度。 ```java public String upload() throws Exception { // 获取上文件的输入流 InputStream inputStream = new FileInputStream(upload); // 获取上文件的总大小 long fileSize = upload.length(); // 创建输出流,用于保存上文件 OutputStream outputStream = new FileOutputStream(new File(uploadFileName)); byte[] buffer = new byte[1024]; int bytesRead = 0; long totalBytesRead = 0; // 读取上文件并保存 while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); totalBytesRead += bytesRead; // 计算上进度并返回给客户端 double progress = (double) totalBytesRead / fileSize * 100; response.getWriter().print(progress); response.flushBuffer(); } outputStream.close(); inputStream.close(); return SUCCESS; } ``` 需要注意的是,上进度条的实现需要在客户端和服务器端同时进行。客户端使用uploadify插件显示进度条,服务器端在文件过程中计算上进度并返回给客户端。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值