1 struts2的文件上传
1.1 文件上传准备
1.1.1 页面配置
input type="file" name="upload"/>
- 表单提交方式为post
- enctypt为multipart/form-data
<FORM id=form1 name=form1
action="${pageContext.request.contextPath }/customer_save.action"
method=post enctype="multipart/form-data">
1.1.2 Action的配置
- 需要有三个重要属性
- private String uploadFileName // 命名要求:Jsp页面文件上传的name属性值+FileName
- private File upload; // 命名要求:Jsp页面文件上传的name属性值
- private String uploadContentType; // 命名要求:Jsp页面文件上传的name属性值+ContentType
- 并分别为这三个属性添加set方法
1.1.3 代码实现
public class CustomerAction extends ActionSupport implements ModelDriven<Customer>
{
private Customer customer=new Customer();
private CustomerService customerService;
private String uploadFileName;
private File upload;
private String uploadContentType;
public void setUploadFileName(String uploadFileName)
{
this.uploadFileName = uploadFileName;
}
public void setUpload(File upload)
{
this.upload = upload;
}
public void setUploadContentType(String uploadContentType)
{
this.uploadContentType = uploadContentType;
}
public void setCustomerService(CustomerService customerService)
{
this.customerService = customerService;
}
public Customer getModel()
{
return customer;
}
public String save() throws IOException {
System.out.println(upload);
if(upload!=null) {
String path="D:\\Java\\WorkSpace\\uploadFile";
String fileName=FileUploadUtils.getUuidFileName(uploadFileName);
String dirPath=FileUploadUtils.getFilePath(fileName);
String dir=path+dirPath;
File fileDir=new File(dir);
if(!fileDir.exists()) {
fileDir.mkdirs();
}
File file=new File(fileDir,fileName);
FileUtil.copyFile(upload, file);
customer.setCust_image(fileDir+"/"+fileName);
}
customerService.save(customer);
return "saveSuccess";
}
}
1.1.4 文件上传工具类
public class FileUploadUtils
{
public static String getUuidFileName(String uploadFileName) {
String uuid=uuid();
if(uploadFileName!=null&& uploadFileName.indexOf(".")!=-1) {
String suffix=uploadFileName.substring(uploadFileName.lastIndexOf("."));
uuid=uuid+suffix;
}
return uuid;
}
public static String getFilePath(String uuidFileName) {
int hashCode = uuidFileName.hashCode();
int temp=hashCode;
StringBuilder sb=new StringBuilder();
for(int i=0;i<8;i++) {
sb.append("/").append(temp & 0xf);
temp=temp>>>1;
}
return sb.toString();
}
public static String uuid() {
String uuid=UUID.randomUUID().toString();
return uuid.replace("-", "");
}
}
1.1.5 设置文件上传大小和格式限制:struts2.xml配置
- 注意当文件格式或大小不匹配的时候,会出现input错误,所以在action需要配置input
<!-- 设置上传文件的临时存储路径 -->
<!-- <constant name="struts.multipart.saveDir" value="d:/upload"/> -->
<!-- 设置单次上传文件大小为5M -->
<constant name="struts.multipart.maxSize" value="5242880"/>
<action name="customer_*" class="customerAction" method="{1}">
<result name="saveSuccess" type="redirectAction">customer_findAll.action</result>
<result name="input">/jsp/customer/add.jsp</result>
<interceptor-ref name="defaultStack">
<!-- 设置单个文件最大大小 -->
<param name="fileUpload.maximumSize">2097152</param>
<!-- 设置上传文件类型 -->
<param name="fileUpload.allowedExtensions">.jpg</param>
</interceptor-ref>
</action>