1.创建项目,添加struts2的支持,然后:
代码:FileUpload类完成文件上传:
package com.myphome;
import java.io.File;
import java.util.UUID;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
/**
* struts2文件上传处理类
* */
public class FileUpload {
private File upload;//保存上传的文件,必须和页面上传“type="file""的name相同
private String uploadFileName;//保存上传的文件名称,必须为“name”+FileName
private String uploadContentType;//保存上传的文件类型,必须为“name”+ContentType
//提供属性的set、get方法
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 uploadFile() throws Exception{
//获取文件上传的类型,实现文件类型安全验证
if(!(uploadContentType.equalsIgnoreCase("image/pjpeg")||uploadContentType.equalsIgnoreCase("image/gif")||uploadContentType.equalsIgnoreCase("image/x-png"))){
ActionContext.getContext().put("message", "文件格式不正确");
return Action.SUCCESS;
}
System.out.println(uploadContentType);
if(upload!=null){
//上传文件在服务器上的保存位置
String strfolder=ServletActionContext.getServletContext().getRealPath("/images");
System.out.println(strfolder);
File folder=new File(strfolder);
if(folder.exists()==false){
folder.mkdir();
}
//更换上传文件名
String ext=uploadFileName.substring(uploadFileName.lastIndexOf("."));
String newFileName=UUID.randomUUID().toString()+ext;
System.out.println(newFileName);
//设置目标对象
File dest=new File(folder, newFileName);
//把临时文件拷贝到目标文件
FileUtils.copyFile(upload, dest);
//设置视图提示内容
ActionContext.getContext().put("message", "文件上传成功");
}
return Action.SUCCESS;
}
}
struts.xml配置:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<!-- 此属性规定了struts2文件上传的内容允许 的最大字节数 -->
<constant name="struts.multipart.maxSize" value="2097152"></constant>
<package name="load" namespace="/struts" extends="struts-default" >
<action name="upload" class="com.myphome.FileUpload" method="uploadFile">
<result name="success" type="dispatcher">/index.jsp</result>
</action>
</package>
</struts>
界面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>struts2文件上传</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<s:if test="%{message==null}">
<span style="color: red;">文件格式必须为图片,大小不大于2G</span>
</s:if>
<s:if test="%{message!=null}">
${message}
</s:if>
<form action="${pageContext.request.contextPath}/struts/upload" method="post" enctype="multipart/form-data" name="formup" id="formup" >
上传文件:<input type="file" name="upload" id="upload">
<input type="submit" name="submit" value="确认上传">
</form>
</body>
</html>