package com.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.io.IOUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 8926090411591986144L;
private String allowTypes ;
private int maximumSize ;
private File upload;
private String uploadFileName;
private String uploadContentType;
private String savePath;
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 String getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
public String getSavePath() {
return savePath;
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public String getAllowTypes() {
return allowTypes;
}
public void setAllowTypes(String allowTypes) {
this.allowTypes = allowTypes;
}
public int getMaximumSize() {
return maximumSize;
}
public void setMaximumSize(int maximumSize) {
this.maximumSize = maximumSize;
}
@Override
public String execute() throws Exception {
String checkUploadType = checkUploadType();
if(checkUploadType != null) {
this.addFieldError("upload", "上传文件类型只能是:" + allowTypes);
return checkUploadType;
}
String checkSize = checkUploadSize();
if(checkSize != null) {
this.addFieldError("upload", "上传文件大小大于" + maximumSize);
return checkSize;
}
String saveDir = ServletActionContext.getRequest().getSession().getServletContext().getRealPath(savePath);
OutputStream out = new FileOutputStream(saveDir + "\\" + uploadFileName);
InputStream in = new FileInputStream(upload);
IOUtils.copy(in, out);
return SUCCESS;
}
public String checkUploadSize() {
long uploadLength = upload.length();
if(uploadLength > maximumSize) {
return INPUT;
} else {
return null;
}
}
public String checkUploadType() {
String result = INPUT;
String[] types = allowTypes.split(",");
for (String str : types) {
if(str.equals(uploadContentType)) {
return null;
}
}
return result;
}
}
<action name="uploadAction" class="com.test.UploadAction"> <param name="allowTypes">image/bmp,image/png,image/gif,image/jpeg</param> <param name="maximumSize">10000</param> <param name="savePath">/upload</param> <result name="input">/upload.jsp</result> <result>/success.jsp</result> </action>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<s:form action="uploadAction.action" method="post" enctype="multipart/form-data">
<s:file name="upload" label="选择文件"></s:file>
<s:submit value="上传"></s:submit>
</s:form>
</body>
</html>
上面是手动拦截的上传文件类型和上传文件大小,没有使用拦截器,下面写个例子是使用的struts2里面默认配置的拦截器fileUpload实现上传的拦截。
代码如下:需要将struts.xml进行配置
<action name="uploadAction" class="com.test.UploadAction"> <param name="savePath">/upload</param> <interceptor-ref name="fileUpload"> <param name="maximumSize">10000</param> <param name="allowedTypes">image/pjpeg,image/bmp,image/png,image/gif,image/jpeg,image/jpg</param> </interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> <result>/success.jsp</result> <result name="input">/upload.jsp</result> </action>
其中这个allowedTypes如果不这么些,可能会出错,挺恶心的,
使用了这个拦截器后,就能限制文件上传类型和文件上传大小,但是相应的提示信息是英文的,如果想显示的是中文的出错提示,需要对两个地方进行修改。
就是在messageResource.properties文件中,修改这两个东西的值
struts.messages.error.content.type.not.allowed
struts.messages.error.file.too.large
其中第一个是文件类型,第二个是上传的文件大小。
如果不是这两个错误,而是发生了其他错误,就修改这个值
struts.message.error.uploading
做文件上传的时候最好指定这两个属性的值:
struts.multipart.saveDir的值这个是上传临时文件夹,如果不设置,使用的是javax.servlet.context.tempdir的值。也就是work/Catalina/localhost
struts.multipart.maxSize是上传的表单的请求内容的最大字节