[b][size=xx-large]Struts2的文件上传和下载续二[/size][/b]
[color=red]说明:[/color]使用struts2的框架来实现文件的上传下载。
把之前在web.xml中注释掉的关于filter的代码恢复。
一、在WebRoot根目录下创建一个upload.jsp页面,这个页面是用struts2来实现的:
二、在struts2.xml中配置上传文件的Action,并且注视掉全部关于拦截器的配置,以便不会造成干扰:
三、在WebRoot根目录下创建一个显示信息的页面uploadResult.jsp:
[color=red]说明:[/color]用struts2实现上传功能已经完成,但是当表单中输入汉字的时候会出现乱码现象,如下图:
[img]http://dl.iteye.com/upload/attachment/402097/4ce14c9b-a7d4-3576-a369-df962957b7f2.jpg[/img]
解决的方法实在struts添加constant属性:<constant name="struts.i18n.encoding" value="gbk">
</constant>
<constant name="struts.multipart.saveDir" value="c:\"></constant>。修改之后如图:
[img]http://dl.iteye.com/upload/attachment/402110/23692079-d0eb-3283-8af4-7c0d1a2abe06.jpg[/img]
[color=red]功能:[/color]实现多个文件的上传,使用的是List列表来实现。
四、修改UploadAction.java文件:
五、修改一下文件上传的页面upload.jsp,再添加两个上传的控件:
显示效果如图:
[img]http://dl.iteye.com/upload/attachment/402139/e76b588d-ab51-3e4f-8653-db9132da19e9.jpg[/img]
[img]http://dl.iteye.com/upload/attachment/402141/3a90eac7-cc4d-3083-9cdf-7e67ed84701f.jpg[/img]
[color=red]说明:[/color]使用struts2的框架来实现文件的上传下载。
把之前在web.xml中注释掉的关于filter的代码恢复。
一、在WebRoot根目录下创建一个upload.jsp页面,这个页面是用struts2来实现的:
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ 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=GB18030">
<title>Insert title here</title>
</head>
<body>
<s:form action="upload" theme="simple">
<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:password name="password" ></s:password>
</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>
二、在struts2.xml中配置上传文件的Action,并且注视掉全部关于拦截器的配置,以便不会造成干扰:
<?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.custom.i18n.resources" value="message">
</constant>
<package name="struts2" extends="struts-default">
<!--
<interceptors>
<interceptor name="myInterceptor" class="com.test.interceptor.MyInterceptor">
<param name="hello">world</param>
</interceptor>
<interceptor name="myInterceptor2" class="com.test.interceptor.MyInterceptor2">
</interceptor>
<interceptor name="myInterceptor3" class="com.test.interceptor.MyInterceptor3">
</interceptor>
<interceptor name="auth" class="com.test.interceptor.AuthInterceptor">
</interceptor>
<interceptor-stack name="myStack">
<interceptor-ref name="myInterceptor"></interceptor-ref>
<interceptor-ref name="myInterceptor2"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="myStack"></default-interceptor-ref>
-->
<global-results>
<result name="login" type="redirect">/login2.jsp</result>
</global-results>
<action name="login" class="com.test.action.LoginAction">
<result name="input">/login2.jsp</result>
<result name="success">/register2.jsp</result>
<result name="failer">login2.jsp</result>
</action>
<action name="pointConverter" class="com.test.action.PointAction">
<result name="success">/output.jsp</result>
</action>
<action name="register" class="com.test.action.RegisterAction" method="test">
<result name="success">/success.jsp</result>
<result name="input">/register2.jsp</result>
<!--
<interceptor-ref name="auth"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="myInterceptor3">
<param name="excludeMethods">test,execute</param>
<param name="includeMethods">test</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
-->
</action>
<action name="upload" class="com.test.action.UploadAction">
<result name="success">/uploadResult.jsp</result>
</action>
</package>
</struts>
三、在WebRoot根目录下创建一个显示信息的页面uploadResult.jsp:
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ 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=GB18030">
<title>Insert title here</title>
</head>
<body>
username: <s:property value="username"/><br>
password: <s:property value="password"/><br>
file: <s:property value="fileFileName"/>
</body>
</html>
[color=red]说明:[/color]用struts2实现上传功能已经完成,但是当表单中输入汉字的时候会出现乱码现象,如下图:
[img]http://dl.iteye.com/upload/attachment/402097/4ce14c9b-a7d4-3576-a369-df962957b7f2.jpg[/img]
解决的方法实在struts添加constant属性:<constant name="struts.i18n.encoding" value="gbk">
</constant>
<constant name="struts.multipart.saveDir" value="c:\"></constant>。修改之后如图:
[img]http://dl.iteye.com/upload/attachment/402110/23692079-d0eb-3283-8af4-7c0d1a2abe06.jpg[/img]
[color=red]功能:[/color]实现多个文件的上传,使用的是List列表来实现。
四、修改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 java.util.List;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport {
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;
}
@Override
public String execute() throws Exception {
for(int i = 0 ; i < file.size() ; ++i){
InputStream is = new FileInputStream(file.get(i));
String root = ServletActionContext.getRequest().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,0,length);
}
is.close();
os.close();
}
return SUCCESS;
}
}
五、修改一下文件上传的页面upload.jsp,再添加两个上传的控件:
<body>
<s:form action="upload" theme="simple" enctype="multipart/form-data" method="post">
<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:password name="password" ></s:password>
</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>
显示效果如图:
[img]http://dl.iteye.com/upload/attachment/402139/e76b588d-ab51-3e4f-8653-db9132da19e9.jpg[/img]
[img]http://dl.iteye.com/upload/attachment/402141/3a90eac7-cc4d-3083-9cdf-7e67ed84701f.jpg[/img]