文件上传:
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
分享到:
相关推荐
在struts2基础上实现的包括单文件、多文件的上传及下载
struts2上传下载项目,代码详细,注释清晰,简单易懂
struts2上传下载实例包含了很多种不同的方法
struts2文件上传和下载 struts2文件上传和下载 struts2文件上传和下载 struts2文件上传和下载 struts2文件上传和下载
http://blog.csdn.net/johnjobs/article/details/8076832博文中附件的下载链接
struts2上传下载struts2上传下载
使用struts2框架进行文件的上传并限制文件的大小与类型,使用struts2框架实现文件下载
基于Struts2的文件上传下载功能的完整源代码。经过测试可正确运行。
完整的基于Struts2的上传下载功能实现,开发平台为MyEclipse
struts2 文件上传 struts2上传标签file fileuploadstruts2 文件上传 struts2上传标签file fileuploadstruts2 文件上传 struts2上传标签file fileupload
struts2上传和下载文件详细源码,适合初学者,转载
3.Struts2进行下载处理,能对上传的所有文件进行下载(多个) 4.文件保存的名称UUID生成,不过显示并下载的名称都是原文件名称 (通过UploadFiles处理) 5.对配置文件中的路径可以进行动态读取(不重启服务器) ...
Struts2中文件上传与下载案例
struts2文件上传struts2文件上传struts2文件上传struts2文件上传struts2文件上传struts2文件上传struts2文件上传struts2文件上传
Struts2 上传下载
struts2中实现文件上传 struts2中实现CRUD struts2中的OGNL struts2的新表单标志的使用 struts2与AJAX一 struts2与AJAX二 struts2与AJAX三 struts2中用Spring实现IOC struts2中的零配置与IOC struts2介绍之使用连接...
Struts2文件上传下载和表单重复提交问题
struts实现的文件上传下载功能,最大可以上传到150MB
struts2 上传 下载 项目 mysql dao mvc