Struts2上传文件
1.上传页面: Index.jsp
<%@ 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>
</head>
<body>
<s:form action="upload.action" method="POST" enctype="multipart/form-data">
<s:file label="选择文件" name="upFile"></s:file>
<s:submit label="上传" />
</s:form>
</body>
</html>
2.上传成功页面(上传成功并显示图片): upSuc.jsp
<%@ 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>
<title>ok</title>
</head>
<body>
上传<br/>
<img src="<%=basePath%><s:property value="savePath + '/' + upFileFileName"/>"/>
</body>
</html>
3.ACTION类: UploadAction.java
package cn.test.action;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport {
private File upFile;
private String upFileFileName;//上传的文件名 (1.系统自动注入 2.变量命名有规则: 前台对象名+"FileName")
private String upFileContentType;//文件类型 (1.系统自动注入 2.变量命名有规则: 前台对象名+"ContentType")
private String savePath;
@Override
public String execute() throws Exception {
String path = ServletActionContext.getServletContext().getRealPath(getSavePath()) + "/" + upFileFileName;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try{
bis = new BufferedInputStream(new FileInputStream(upFile));
bos = new BufferedOutputStream(new FileOutputStream(path));
byte[] buf = new byte[(int)upFile.length()];
int len = 0;
while(((len=bis.read(buf))!=-1)){
bos.write(buf, 0, len);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(bos!=null){
bos.close();
}
if(bis!=null){
bis.close();
}
}catch(Exception e){
bos = null;
bis = null;
}
}
return SUCCESS;
}
public File getUpFile() {
return upFile;
}
public void setUpFile(File upFile) {
this.upFile = upFile;
}
public String getUpFileFileName() {
return upFileFileName;
}
public void setUpFileFileName(String upFileFileName) {
this.upFileFileName = upFileFileName;
}
public String getUpFileContentType() {
return upFileContentType;
}
public void setUpFileContentType(String upFileContentType) {
this.upFileContentType = upFileContentType;
}
public String getSavePath() {
return savePath;
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
}
4.struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" extends="struts-default" >
<action name="upload" class="cn.test.action.UploadAction">
<param name="savePath">/pic</param>
<result name="success">/upSuc.jsp</result>
<result name="input">/index.jsp</result>
<interceptor-ref name ="fileUpload">
<param name ="allowedTypes">image/bmp,image/png,image/gif,image/jpeg</param>
</interceptor-ref>
<interceptor-ref name ="defaultStack"/>
</action>
</package>
</struts>
5.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>