1、新建upload.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head> <body>
<s:form action="upload" theme="simple" method="post" namespace="" enctype="multipart/form-data">
<table align="center" width="40%" border="1">
<tr>
<td>
username
</td>
<td>
<s:textfield name="username" ></s:textfield>
</td>
</tr>
<tr>
<td>
password
</td>
<td>
<s:textfield name="password" ></s:textfield>
</td>
</tr>
<tr>
<td>
file
</td>
<td>
<s:file name="file"></s:file>
</td>
</tr>
<tr>
<td>
<s:submit value=" submit "></s:submit>
</td>
<td>
<s:reset value=" reset "></s:reset>
</td>
</tr>
</table> </s:form>
</body>
</html>
2、新建uploadResult.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'uploadResult.jsp' starting page</title>
</head> <body>
username:<s:property value="username"></s:property>
password:<s:property value="password"></s:property>
file:<s:property value="fileFileName"></s:property>
</body>
</html>
3、新建UploadAction.java
package com.test.action; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class UploadAction extends ActionSupport { private static final long serialVersionUID = 1L; private String username; private String password;
//必须添加的三个属性
//file
private File file;
//fileFileName
private String fileFileName;
//fileContentType
private String fileContentType; public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public File getFile() {
return file;
} public void setFile(File file) {
this.file = file;
} 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 String execute() throws Exception
{
InputStream is = new FileInputStream(file);
String root = ServletActionContext.getServletContext().getRealPath("/upload");
File destFile = new File(root,this.getFileFileName());
OutputStream os = new FileOutputStream(destFile);
//字节数组
byte[] buffer = new byte[400];
int length = 0;
while ((length = is.read(buffer))>0) {
os.write(buffer);
}
is.close();
os.close();
return SUCCESS;
}
}
4、struts.xml
<action name="upload" class="com.test.action.UploadAction">
<result name="success">/uploadResult.jsp</result>
</action>
多文件上传:
1、upload.jsp
<body>
<s:form action="upload" theme="simple" method="post" namespace="" enctype="multipart/form-data">
<table align="center" width="40%" border="1">
<tr>
<td>
username
</td>
<td>
<s:textfield name="username" ></s:textfield>
</td>
</tr>
<tr>
<td>
password
</td>
<td>
<s:textfield name="password" ></s:textfield>
</td>
</tr>
<tr>
<td>
file1
</td>
<td>
<s:file name="file"></s:file>
</td>
</tr>
<tr>
<td>
file2
</td>
<td>
<s:file name="file"></s:file>
</td>
</tr>
<tr>
<td>
file3
</td>
<td>
<s:file name="file"></s:file>
</td>
</tr>
<tr>
<td>
<s:submit value=" submit "></s:submit>
</td>
<td>
<s:reset value=" reset "></s:reset>
</td>
</tr>
</table> </s:form>
</body>
2、uploadResult.jsp
<body>
username:<s:property value="username"></s:property>
password:<s:property value="password"></s:property>
file:<s:property value="fileFileName"></s:property>
</body>
3、uploadAction.jsp
package com.test.action; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List; import javax.swing.ListModel; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class UploadAction extends ActionSupport { private static final long serialVersionUID = 1L; private String username; private String password; private List<File> file; private List<String> fileFileName; private List<String> fileContentType; public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public List<File> getFile() {
return file;
} public void setFile(List<File> file) {
this.file = file;
} public List<String> getFileFileName() {
return fileFileName;
} public void setFileFileName(List<String> fileFileName) {
this.fileFileName = fileFileName;
} public List<String> getFileContentType() {
return fileContentType;
} public void setFileContentType(List<String> fileContentType) {
this.fileContentType = fileContentType;
} public String execute() throws Exception
{
for (int i = 0; i < file.size(); i++) {
InputStream is = new FileInputStream(file.get(i));
String root = ServletActionContext.getServletContext().getRealPath("/upload");
File destFile = new File(root,this.getFileFileName().get(i));
OutputStream os = new FileOutputStream(destFile);
//字节数组
byte[] buffer = new byte[400];
int length = 0;
while ((length = is.read(buffer))>0) {
os.write(buffer);
}
is.close();
os.close();
}
return SUCCESS;
}
}