读书人

Struts2下传上载(转)(二)

发布时间: 2012-08-22 09:50:34 作者: rapoo

Struts2上传下载(转)(二)

?

文件上传:

1,upload.jsp如下:

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
??? + request.getServerName() + ":" + request.getServerPort()
??? + path + "/";
%>

<title>My JSP 'upload.jsp' starting page</title>

<!--下面script实现多文件上传-->
?? <script type ="text/javascript">
??
?? function onMore(){
??? var td =document.getElementById("more");
??? var br = document.createElement("br");
??? var input = document.createElement("input");
??? var button = document.createElement("input");
???
??? input.type = "file";
??? input.name="file";
??? button.type="button";
??? button.value="删除";
??? //当点击删除时,删除一行。
??? button.onclick = function(){
????
???? td.removeChild(br);
???? td.removeChild(input);
???? td.removeChild(button);
??? }
???
??? // 下面三句增加一行。
??? td.appendChild(br);
??? td.appendChild(input);
??? td.appendChild(button);
?? }
??
?? </script>

</head>

<body>
?? <table align = "center">
??? <tr>
???? <td><s:fielderror cssStyle="color:red"></s:fielderror></td>
??? </tr>
?? </table>
?? <s:form action="upload" theme="simple" method="post" enctype="multipart/form-data">
??? <table align="center" border="1" width="60%">
???????? <tr>
???? <td>文件</td>
???? <td id = "more">
????? <s:file name="file"></s:file>
????? <input type = "button" value = "更多.." onclick = "onMore()">
???? </td>
??? </tr><br>
???
??? <tr>
???? <td>
????? <s:submit value="submit"></s:submit>
???? </td>
???? <td>
????? <s:reset value="reset"></s:reset>
???? </td>
??? </tr>
?? </table>
?? </s:form>
</body>
</html>

?

2.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.custom.i18n.resources" value="message"></constant>
?? <!-- 上传文件编码 -->
?? <constant name="struts.i18n.encoding" value="gbk"></constant>
?? <!-- 上传文件临时文件位置 -->
?? <constant name="struts.multipart.saveDir" value="c:\"></constant>

?? <package name="struts2" extends="struts-default">

<action name="upload" class = "com.struct2.test.UploadAction">
???? <result name = "success">/UpLoad/uploadresult.jsp</result>
???? <result name = "input">/UpLoad/upload.jsp</result>
???? <interceptor-ref name="fileUpload">
????? <!-- 单个上传文件的最大值-->
????? <param name="maximumSize">409600</param>
????? <!-- 只能上传的文件的类型,可到tomcat的web-xml中查看各种文件类型-->
????? <param name="allowedTypes">text/html</param>
???? </interceptor-ref>
???? <interceptor-ref name="defaultStack"></interceptor-ref>
??? </action>

</package>

?

?

?

?

3.UploadAction.jsp:

?

package com.struct2.test;

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;//文件名+FileName
private List<String> fileContentType;//文件名+ContentType
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){
??? System.out.println(file.get(i));
??? if(!file.get(i).exists())
??? {return INPUT;}
??? //拿到上传的文件
??? 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;
}
}
</struts>

4.uploadresult.jsp

...............................

<body>
?? ?? username:<s:property value="username"/><br>
?? ?? password:<s:property value = "password"/><br>
?? ?? file:<s:property value = "fileFileName"/>
</body>

?

?

文件下载:

1.download.jsp:

<body>
?? <a href = "download.action">下载</a>
</body>

2.struts.xml中配置:

<action name="download" class = "com.struct2.test.DownloadAction">
???? <result name = "success" type = "stream">
???? <!-- 设置为attachment,否则浏览器直接打开而不会出现下载页面 -->
????? <param name="contentDisposition">attachment;filename=${fileName}</param>
????? <!-- downloadFile为DownloadAction中的方法名中的属性-->
????? <param name="inputName">downloadFile</param>
????? <!-- 下载文件的类型,可到tomcat的web-xml中查看各种文件类型-->
????? <param name="contentType">text/html</param>
????? <!-- 输出时缓冲区的大小 -->
????? <param name="bufferSize">4096</param>
?????
?????
???? </result>

3.DowmloadAction.java

package com.struct2.test;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class DownloadAction extends ActionSupport {
//String fileName = "";
public InputStream getDownloadFile(){
??
?? return ServletActionContext.getServletContext().getResourceAsStream("/下载中文文件测试.html");
}

@Override
public String execute() throws Exception {
?? //这里添加下载权限设置.
?? return SUCCESS;
}

public String getFileName() {
??????? // DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
???????? String fileName = "下载中文文件测试.html";
?????? // fileName = "序列号(" + df.format(new Date()) + ").html";
???????? try {
???????????? //设置下载文件名编码

???? return new String(fileName.getBytes(), "ISO8859-1");
??????? } catch (UnsupportedEncodingException e) {
??????????? return "impossible.txt";
??????? }
}

}

国际化资源文件message.properties:

xwork.default.invalid.fieldvalue={0}\u8f93\u5165\u9519\u8bef

struts.messages.error.file.too.large=File too large
struts.messages.error.content.type.not.allowed=file type not be allowed

读书人网 >软件架构设计

热点推荐