1、導入struts包

2、配置web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- id="WebApp_ID" version="2.5">
- <display-name>NewJava</display-name>
- <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>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>NewJava</display-name>
<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>
3、上傳JSP頁面
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <%@ taglib prefix="s" uri="/struts-tags" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>檔案上傳</title>
- </head>
- <body>
- <div align="center">
- <br/><br/>
- <img alt="檔案上傳" src="images/upload.png">
- <s:form method="post" action="Upload" theme="simple" enctype="multipart/form-data">
- 檔案名稱:<s:textfield name="title"></s:textfield>
- 檔案位址:<s:file name="upload"></s:file>
- <s:submit value="上傳"></s:submit>
- </s:form>
- </div>
- </body>
- </html>
4、編寫Action
- package com.action;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import javax.servlet.http.HttpServletRequest;
- import org.apache.struts2.ServletActionContext;
- import com.opensymphony.xwork2.ActionSupport;
- public class UploadAction extends ActionSupport {
- private String title;
- private File upload;
- private String uploadContentType;
- // 封裝上傳檔案名的屬性
- private String uploadFileName;
- // 直接在struts.xml檔案中配置的屬性
- public String execute() throws Exception {
- //以伺服器的檔案儲存位址和原檔案名建立上傳檔案輸出流
- String path = ServletActionContext.getServletContext().getRealPath ("/load/");//擷取伺服器存放實體路徑
- System.out.println(path+ getUploadFileName());
- FileOutputStream fos = new FileOutputStream(path+"\\"+ getUploadFileName());
- FileInputStream fis = new FileInputStream(getUpload());
- byte[] buffer = new byte[1024];
- int len = 0;
- while ((len = fis.read(buffer)) > 0)
- { fos.write(buffer , 0 , len);
- }
- System.out.println("上傳成功");
- System.out.println(path+"\\"+ getUploadFileName());
- return SUCCESS;
- }
- public String getTitle() {
- return title;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- public File getUpload() {
- return upload;
- }
- public void setUpload(File upload) {
- this.upload = upload;
- }
- public String getUploadContentType() {
- return uploadContentType;
- }
- public void setUploadContentType(String uploadContentType) {
- this.uploadContentType = uploadContentType;
- }
- public String getUploadFileName() {
- return uploadFileName;
- }
- public void setUploadFileName(String uploadFileName) {
- this.uploadFileName = uploadFileName;
- }
- }
package com.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport {
private String title;
private File upload;
private String uploadContentType;
// 封裝上傳檔案名的屬性
private String uploadFileName;
// 直接在struts.xml檔案中配置的屬性
public String execute() throws Exception {
//以伺服器的檔案儲存位址和原檔案名建立上傳檔案輸出流
String path = ServletActionContext.getServletContext().getRealPath ("/load/");//擷取伺服器存放實體路徑
System.out.println(path+ getUploadFileName());
FileOutputStream fos = new FileOutputStream(path+"\\"+ getUploadFileName());
FileInputStream fis = new FileInputStream(getUpload());
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0)
{ fos.write(buffer , 0 , len);
}
System.out.println("上傳成功");
System.out.println(path+"\\"+ getUploadFileName());
return SUCCESS;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
}
5、配置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>
- <constant name="struts.enable.DynamicMethodInvocation" value="true" />
- <constant name="struts.devMode" value="true" />
- <package name="example" extends="struts-default">
- <action name="Upload" class="com.action.UploadAction">
- <result >/download.jsp</result>
- </action>
- </package>
- </struts>
<?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>
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="true" />
<package name="example" extends="struts-default">
<action name="Upload" class="com.action.UploadAction">
<result >/download.jsp</result>
</action>
</package>
</struts>
6、看看目錄結構 運作吧 檔案上傳到load檔案夾下面 可能有點慢