在Struts2中为我们提供了比较简单的文件上传方式.
首先引入commons-fileupload-x.jar、commons-io-x.jar 这两个jar包,然后引入Struts2相关jar包
接下来新建一个jsp文件:写一个文件上传表单,这里需要特别注意要在form加上enctype="multipart/form-data" method="post" .
<form action="/admin/center/brandAction!addBrandProcess" enctype="multipart/form-data" method="post">
<table width="90%" border="0" cellspacing="2" cellpadding="3"
align="center">
<tr bgcolor="f5f5f5">
<td width="22%">
<div align="right">图片:</div>
</td>
<td width="78%"><input type="file" name="uploadFile" />
</td>
</tr>
<tr bgcolor="f5f5f5">
<td colspan="2">
<div align="center">
<input type="submit" value=" 确 定 ">
</div>
</td>
</tr>
</table>
</form>
下一步:添加我们的Action类:
package gd.hz.shopping.action;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.ActionSupport;
@Controller("brandAction")
@Scope("prototype")
public class Test extends ActionSupport {
private static final long serialVersionUID = 1L;
//此属性对应于表单中文件字段的名称
private File uploadFile;
//下面的这两个属性的命名必须遵守上定的规则,即为"表单中文件字段的名称" + "相应的后缀"
private String uploadFileContentType; // 得到上传的文件的数据类型,
private String uploadFileFileName; // 得到上传的文件的名称
public File getUploadFile() {
return uploadFile;
}
public void setUploadFile(File uploadFile) {
this.uploadFile = uploadFile;
}
public String getUploadFileContentType() {
return uploadFileContentType;
}
public void setUploadFileContentType(String uploadFileContentType) {
this.uploadFileContentType = uploadFileContentType;
}
public String getUploadFileFileName() {
return uploadFileFileName;
}
public void setUploadFileFileName(String uploadFileFileName) {
this.uploadFileFileName = uploadFileFileName;
}
public String addBrandProcess() throws IOException
{
String realPath = ServletActionContext.getServletContext().getRealPath("/images");
SimpleDateFormat date = new SimpleDateFormat("/yyyy/MM/dd");
String dateTime = date.format(new Date());
realPath += dateTime;
uploadFileFileName = UUID.randomUUID().toString() + uploadFileFileName.substring(uploadFileFileName.lastIndexOf('.'));
System.out.println(uploadFileContentType);
//控制图片类型
if(uploadFileContentType.equals("image/gif") || uploadFileContentType.equals("image/jpeg") ||
uploadFileContentType.equals("image/png") || uploadFileContentType.equals("image/bmp") ||
uploadFileContentType.equals("image/x-icon") || uploadFileContentType.equals("image/pjpeg"))
{
//判断文件是否为空,并且文件不能大于2M
if(uploadFile != null && uploadFile.length() < 2097152)
{
//根据 parent 抽象路径名和 child 路径名字符串创建一个新 File 实例。
File filePath = new File(new File(realPath), uploadFileFileName);
//判断路径是否存在
if(!filePath.getParentFile().exists())
{
//如果不存在,则递归创建此路径
filePath.getParentFile().mkdirs();
}
System.out.println(uploadFileFileName);
System.out.println(filePath.getParentFile());
//将文件保存到硬盘上,Struts2会帮我们自动删除临时文件
try {
FileUtils.copyFile(uploadFile, filePath);
} catch (IOException e) {
System.out.println("图片上传失败");
e.printStackTrace();
}
}
}
return "success";
}
}
上面还是要遵守约定大于配置,注意名称的命名,uploadFileContentType和uploadFileFileName必须遵守Struts2的规则.
Struts2默认文件的上传大小是2M,当大于2M时会抛出异常,这里我们可以在struts.xml文件里做如下配置,控制上传文件的大小:
<struts> <constant name="struts.multipart.maxSize" value="10485761"/> </struts>