Struts2 - Struts2的文件上传 [单个文件上传](从零开始学习Strust2_09)
2012-03-17 11:18
531 查看
开发环境:
Eclipse IDE for Java EE Developers(下载地址)
struts-2.3.1.2(下载地址)
apache-tomcat-6.0.35(下载地址)
效果图:
注意添加upload文件夹用于存放上传的文件
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/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>struts2_20120316_01</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>upload.jsp</welcome-file>
</welcome-file-list>
</web-app>
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.devMode" value="true" />
<constant name="struts.multipart.saveDir" value="C://struts2 upload temp"></constant>
<package name="default" extends="struts-default">
<action name="UploadAction" class="com.zeph.struts2.action.UploadAction">
<interceptor-ref name="fileUpload">
<param name="maximumSize">2000</param>
<param name="allowedTypes"></param>
</interceptor-ref>
<interceptor-ref name="defaultStack" />
<param name="savePath">/upload</param>
<result name="success">/uploadSuccess.jsp</result>
<result name="input">/upload.jsp</result>
</action>
</package>
</struts>
UploadAction.java
package com.zeph.struts2.action;
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 static final long serialVersionUID = 1L;
private File upload;
private String uploadFileName;
private String uploadContentType;
private String savePath;
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
@SuppressWarnings("deprecation")
public String getSavePath() {
return ServletActionContext.getRequest().getRealPath(savePath);
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
@Override
public String execute() throws Exception {
FileOutputStream fos = new FileOutputStream(getSavePath() + "//"
+ getUploadFileName());
FileInputStream fis = new FileInputStream(getUpload());
byte[] buffer = new byte[1024];
int length = 0;
while ((length = fis.read(buffer)) > 0) {
fos.write(buffer, 0, length);
}
return SUCCESS;
}
}
upload.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Upload Page</title>
</head>
<body>
<s:form action="UploadAction" method="post"
enctype="multipart/form-data">
<s:file name="upload" label="Upload File" />
<s:submit />
</s:form>
</body>
</html>
uploadSuccess.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Upload Success Page</title>
</head>
<body>
file name:
<s:property value="uploadFileName" />
<br> file type:
<s:property value="uploadContentType" />
<br>
<s:property value="savePath" />
<br>
</body>
</html>
效果图:
Eclipse IDE for Java EE Developers(下载地址)
struts-2.3.1.2(下载地址)
apache-tomcat-6.0.35(下载地址)
效果图:
注意添加upload文件夹用于存放上传的文件
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/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>struts2_20120316_01</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>upload.jsp</welcome-file>
</welcome-file-list>
</web-app>
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.devMode" value="true" />
<constant name="struts.multipart.saveDir" value="C://struts2 upload temp"></constant>
<package name="default" extends="struts-default">
<action name="UploadAction" class="com.zeph.struts2.action.UploadAction">
<interceptor-ref name="fileUpload">
<param name="maximumSize">2000</param>
<param name="allowedTypes"></param>
</interceptor-ref>
<interceptor-ref name="defaultStack" />
<param name="savePath">/upload</param>
<result name="success">/uploadSuccess.jsp</result>
<result name="input">/upload.jsp</result>
</action>
</package>
</struts>
UploadAction.java
package com.zeph.struts2.action;
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 static final long serialVersionUID = 1L;
private File upload;
private String uploadFileName;
private String uploadContentType;
private String savePath;
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
@SuppressWarnings("deprecation")
public String getSavePath() {
return ServletActionContext.getRequest().getRealPath(savePath);
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
@Override
public String execute() throws Exception {
FileOutputStream fos = new FileOutputStream(getSavePath() + "//"
+ getUploadFileName());
FileInputStream fis = new FileInputStream(getUpload());
byte[] buffer = new byte[1024];
int length = 0;
while ((length = fis.read(buffer)) > 0) {
fos.write(buffer, 0, length);
}
return SUCCESS;
}
}
upload.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Upload Page</title>
</head>
<body>
<s:form action="UploadAction" method="post"
enctype="multipart/form-data">
<s:file name="upload" label="Upload File" />
<s:submit />
</s:form>
</body>
</html>
uploadSuccess.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Upload Success Page</title>
</head>
<body>
file name:
<s:property value="uploadFileName" />
<br> file type:
<s:property value="uploadContentType" />
<br>
<s:property value="savePath" />
<br>
</body>
</html>
效果图:
相关文章推荐
- Struts学习总结(五)---使用Struts2实现单个文件上传
- Struts2学习8--文件上传(单个文件上传)
- Struts2 - Struts2的文件下载(从零开始学习Strust2_10)
- struts2学习笔记--上传单个和批量文件示例
- Struts2 - 在Tag中使用Properties属性文件(从零开始学习Strust2_04)
- struts2学习笔记--上传单个和批量文件示例
- struts2学习笔记--上传单个和批量文件示例
- struts2学习笔记--上传单个和批量文件示例
- struts2实现文件上传(单个、批量文件上传)
- struts2实现上传文件(单个、多个)
- [Struts2]struts2 实现文件上传【学习笔记】
- struts2之单个文件上传
- Struts2学习笔记之文件的上传与下载
- struts2学习之文件的上传与下载
- struts2学习笔记之十:文件上传
- Struts2学习笔记之文件的上传与下载
- 【J2EE核心开发学习笔记 012】Struts2文件的上传与下载
- 文件的上传下载---struts2学习笔记
- Struts2实现图片上传功能 (单个图片文件)
- struts2 单个文件上传的三种方法以及多文件上传