1、导入文件上传的两个jar包
commons-io-2.5.jar
commons-fileupload-1.3.2.jar
2、jsp上传页面代码
<form action="FileUpload2" enctype="multipart/form-data" method="post" >
上传文件:<input type="file" name="image"><br/>
<input type="submit" value="提交"/>
</form>
注意引入不能出错:
import java.io.File;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
3、action中的代码
/**
*
*/
package struts2.action;
import java.io.File;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* @author Administrator
*
*/
public class UploadFileImage extends ActionSupport{
private static final long serialVersionUID = 1L;
private File image; //上传的文件
private String imageFileName; //文件名称
private String imageContentType; //文件类型
public File getImage() {
return image;
}
public void setImage(File image) {
this.image = image;
}
public String getImageFileName() {
return imageFileName;
}
public void setImageFileName(String imageFileName) {
this.imageFileName = imageFileName;
}
public String getImageContentType() {
return imageContentType;
}
public void setImageContentType(String imageContentType) {
this.imageContentType = imageContentType;
}
public String execute() throws Exception{
//1.ServletActionContext获取request
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
System.out.println("nihao----ImageUpload");
String username1;
int Iduser1;
if(session.getAttribute("username")!=null) {
username1=(String) session.getAttribute("username");
}else {
return "ImageUpload_fail_needLogin";
}
if(session.getAttribute("Iduser")!=null) {
Iduser1=(int)session.getAttribute("Iduser");
}else {
return "ImageUpload_fail_needLogin";
}
//获取要保存文件夹的物理路径(绝对路径)
String realpath = ServletActionContext.getServletContext().getRealPath("/images");
System.out.println("realpath: "+realpath);
if(image != null){
File savefile = new File(new File(realpath), imageFileName);
System.out.println(savefile);
System.out.println(savefile.getParentFile());
/*if(savefile.getParentFile().exists()){
try {
//测试此抽象路径名表示的文件或目录是否存在。若不存在,创建此抽象路径名指定的目录,包括所有必需但不存在的父目录。
savefile.getParentFile().mkdirs();
//保存文件
FileUtils.copyFile(image, savefile);
} catch (IOException e) {
e.printStackTrace();
// return "ImageUpload_Fail";
}
ActionContext.getContext().put("message", "文件上传成功");
}*/
//测试此抽象路径名表示的文件或目录是否存在。若不存在,创建此抽象路径名指定的目录,包括所有必需但不存在的父目录。
if(!savefile.getParentFile().exists()) savefile.getParentFile().mkdirs();
try {
//保存文件
FileUtils.copyFile(image, savefile);
} catch (IOException e) {
e.printStackTrace();
}
request.setAttribute("savefile", savefile);
request.setAttribute("imageFileName", imageFileName);
return "ImageUpload_Success";
}else {
return "ImageUpload_Fail";
}
/**
* 若要存入数据库
* fileName是在entity实体类中声明存放文件名称的变量
* yu.setFileName(imageFileName) 这样将文件名称存入数据库
* 文件路径为:savefile
*/
}
}
3、在页面显示
<img class="media-object thumbnail" src="${pageContext.request.contextPath}/images/<%=imageFileName %>" alt="..." style="max-height: 165px;max-width: 165px;">
4、struts.xml
<struts>
<constant name="struts.devMode" value="true"/><!-- 开启开发者模式 -->
<constant name="struts.multipart.maxSize" value="1000000000"/>
<!--指定国际化资源文件(下一章会讲到)-->
<constant name="struts.custom.i18n.resources" value="messageResource"/>
<!--设置Struts应用的解码集-->
<constant name="struts.i18n.encoding" value="utf-8"/>
<action name="UploadFileImage" class="struts2.action.UploadFileImage">
<!-- 指定(限制)上传文件的类型,定义局部拦截器,修改默认拦截器的属性
"fileUpload.maximumSize" :限制上传最大的文件大小。
"fileUpload.allowedTypes":允许上传文件的类型。
"fileUpload.allowedExtensions":允许上传文件的可扩展文件类型。 -->
<interceptor-ref name="defaultStack">
<param name="fileUpload.maximumSize">500000000</param>
<param name="fileUpload.allowedTypes">image/jpeg,image/jpg,image/gif</param>
</interceptor-ref>
<result name="ImageUpload_fail_needLogin">/login.jsp</result>
<result name="ImageUpload_Success">/UserInfo.jsp</result>
<result name="ImageUpload_Fail">/error.jsp</result>
</action>