struts.xml<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- 系统常量定义,定义上传文件临时存放路径 -->
<constant name="struts.multipart.saveDir" value="c:\upload\"></constant> <include file="struts-default.xml" />
<package name="base" extends="struts-default">
<interceptors>
<interceptor name="Example" class="interceptors.ExampleInterceptor">
<param name="param">iamxu</param>
</interceptor>
</interceptors>
<package name="C04.4" extends="struts-default">
<!-- Action名字,类以及导航页面定义 -->
<!-- 通过Action类处理才导航的的Action定义 -->
<action name="upload" class="base.UploadAction">
<result name="input">/upload.jsp</result>
<result name="success">/result.jsp</result>
</action>
</package>
</struts>
UploadAction.javapackage base;import org.apache.struts2.ServletActionContext;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
import javax.servlet.http.HttpServletRequest;
public class UploadAction extends ActionSupport{
public String execute(){
try {
uploadFile();
System.out.println("------AfterUpLoad--------");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "success";
}
private void uploadFile() throws FileNotFoundException, IOException {
try {
path = ServletActionContext.getRequest().getRealPath(UPLOADPATH);
InputStream in = new FileInputStream(file);
File uploadFile = new File(path, this.getFileFileName());
OutputStream out = new FileOutputStream(uploadFile);
byte[] buffer = new byte[1024 * 1024];
int length;
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
} in.close();
out.close();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
} public String getFileFileName() {
return fileFileName;
} public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
} public String getFileContentType() {
return fileContentType;
} public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
} public File getFile() {
return file;
} public void setFile(File file) {
this.file = file;
}
private final static String UPLOADPATH = "/upload";
private String fileFileName;
private String fileContentType;
private File file;
private String path;
}result.jsp<%@ page language="java" pageEncoding="GB18030"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
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>result</title>
</head>
<body>
<S:property value="fileFileName"/>
</body>
</html>upload.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</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:form action="upload" method="post" enctype="multipart/form-data">
<tr>
<!-- 上传文件标签定义 -->
<td>上传文件:<s:file name="file"></s:file></td>
</tr>
<tr>
<!-- <td>再次上传文件:<s:file name="file"></s:file></td>-->
</tr>
<tr>
<td align="left"><s:submit name="submit" value="提交"></s:submit></td>
</tr>
</s:form>
</body>
</html>
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- 系统常量定义,定义上传文件临时存放路径 -->
<constant name="struts.multipart.saveDir" value="c:\upload\"></constant> <include file="struts-default.xml" />
<package name="base" extends="struts-default">
<interceptors>
<interceptor name="Example" class="interceptors.ExampleInterceptor">
<param name="param">iamxu</param>
</interceptor>
</interceptors>
<package name="C04.4" extends="struts-default">
<!-- Action名字,类以及导航页面定义 -->
<!-- 通过Action类处理才导航的的Action定义 -->
<action name="upload" class="base.UploadAction">
<result name="input">/upload.jsp</result>
<result name="success">/result.jsp</result>
</action>
</package>
</struts>
UploadAction.javapackage base;import org.apache.struts2.ServletActionContext;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
import javax.servlet.http.HttpServletRequest;
public class UploadAction extends ActionSupport{
public String execute(){
try {
uploadFile();
System.out.println("------AfterUpLoad--------");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "success";
}
private void uploadFile() throws FileNotFoundException, IOException {
try {
path = ServletActionContext.getRequest().getRealPath(UPLOADPATH);
InputStream in = new FileInputStream(file);
File uploadFile = new File(path, this.getFileFileName());
OutputStream out = new FileOutputStream(uploadFile);
byte[] buffer = new byte[1024 * 1024];
int length;
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
} in.close();
out.close();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
} public String getFileFileName() {
return fileFileName;
} public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
} public String getFileContentType() {
return fileContentType;
} public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
} public File getFile() {
return file;
} public void setFile(File file) {
this.file = file;
}
private final static String UPLOADPATH = "/upload";
private String fileFileName;
private String fileContentType;
private File file;
private String path;
}result.jsp<%@ page language="java" pageEncoding="GB18030"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
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>result</title>
</head>
<body>
<S:property value="fileFileName"/>
</body>
</html>upload.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</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:form action="upload" method="post" enctype="multipart/form-data">
<tr>
<!-- 上传文件标签定义 -->
<td>上传文件:<s:file name="file"></s:file></td>
</tr>
<tr>
<!-- <td>再次上传文件:<s:file name="file"></s:file></td>-->
</tr>
<tr>
<td align="left"><s:submit name="submit" value="提交"></s:submit></td>
</tr>
</s:form>
</body>
</html>
改成
<s:property value="fileFileName"/>
File uploadFile = new File(path, this.getFileFileName());
你这里getFileFileName()根本就是空字符串的。
比如,你在定义fileFileName 时就赋值,如private String fileFileName = "temp1";
结果页面中就能看到了