由于struts2的上传机制是通过默认拦截器实现,而默认拦截器的默认大小是大约2M。所以如果设置不当,用户上传2M以上的文件就会被拦截器拦截并在后台抛出异常。异常如下:
严重: org.apache.commons.fileupload.FileUploadBase$SizeLimitExceededException:
the request was rejected because its size (6964497) exceeds the configured maximum (2097152)
想到在action中设置拦截器拦截文件大小=3M,如下:
<interceptor-ref name ="fileUpload"> <param name ="allowedTypes">image/bmp,image/png,image/gif,image/jpeg,image/jpg</param> <param name="maximumSize ">3000000</param> </interceptor-ref>
当发现,上传2M的文件时还是不行,照样被默认拦截器截下,并抛出上面的异常。看样子是出在默认拦截器的文件大小的设置上,接着自己把fileupload拦截器去除,自己在action中实现上传错误的业务逻辑。并把默认拦截器的默认最大值设为10M
发现程序终于可以运行到action中去了。
struts.properties
struts.multipart.maxSize=10000000
ACTION
package com;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.*;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class FileUploadAction extends ActionSupport {
private static final long serialVersionUID = 572146812454l;
private File[] upload;
private String[] uploadContentType;
private String[] uploadFileName;
private String[] imageFileName;
private String msg="";
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String[] getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String[] uploadContentType) {
this.uploadContentType = uploadContentType;
}
public File[] getUpload() {
return upload;
}
public void setUpload(File[] upload) {
this.upload = upload;
}
public String[] getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String[] uploadFileName) {
this.uploadFileName = uploadFileName;
}
public void setImageFileName(String[] imageFileName) {
this.imageFileName = imageFileName;
}
public String[] getImageFileName() {
return imageFileName;
}
private static void copy(File src, File dst) {
try {
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(src);
out = new FileOutputStream(dst);
int len=0;
byte[] buffer=new byte[1024];
while ((len=in.read(buffer)) > 0) {
out.write(buffer,0,len);
}
}
finally {
if (null != in) {
in.close();
}
if (null != out) {
out.close();
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
private boolean filetype(String current){
String[] type={"image/bmp","image/png","image/gif","image/jpeg","image/jpg"};
for(String now:type){
if(now.equals(current)){
return true;
}
}
return false;
}
private String judge(){
try{
int flag=0;
for(int i=0;i<upload.length;i++){
if(filetype(uploadContentType[i])==false){
msg+="<li>"+uploadFileName[i]+" 上传文件的类型错误,只能上传图片类型的文件!</li>"+"\n";
flag=1;
}
InputStream in=new FileInputStream(upload[i]);
int size=in.available();
if(size>1024*1024){
msg+="<li>"+uploadFileName[i]+"上传文件的大小错误, 单个文件大小小于1M!</li>"+"\n";
flag=1;
}
in.close();
}
if(flag==1){
return INPUT;
}else{
return SUCCESS;
}
}catch (Exception e) {
e.printStackTrace();
}
return ERROR;
}
@Override
public String execute() {
String result=judge();
String[] target=new String[upload.length];
if(result==SUCCESS){
for(int i=0;i<upload.length;i++){
target[i] = System.currentTimeMillis() + uploadFileName[i].substring(uploadFileName[i].lastIndexOf("."));
File imageFile = new File(ServletActionContext.getServletContext()
.getRealPath("/uploadImages")
+ "/" + target[i]);
copy(upload[i], imageFile);
}
setImageFileName(target);
}
return result;
}
}