Struts2-上传/下载文件功能实现
2016-10-23 19:09
666 查看
上传文件
项目:(1)web.xml文件配置
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>Struts2-test-7-FileDown</display-name>
<filter>
<filter-name>Struts2-test-7-FileDown</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Struts2-test-7-FileDown</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
(2)选择上传文件界面
FileUpload.jsp<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
<form action="Upload.action" method="post" enctype="multipart/form-data">
file:<input type="file" name="file"><br/>
<input type="submit" value="上传">
</form>
</body>
</html>
(3)Action文件实现具体功能
UploadAction.java/*
*@Author swxctx
*@time 2016年9月9日
*/
package com.sw.struts2;
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.commons.fileupload.servlet.ServletRequestContext;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction2 extends ActionSupport {
//实现多文件上传
private String username;
private List<File> file;//file需要与客户端所传文件名一致
private List<String> fileFileName;//文件名
private List<String> fileContType;//类型
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
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> getFileContType() {
return fileContType;
}
public void setFileContType(List<String> fileContType) {
this.fileContType = fileContType;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
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,fileFileName.get(i));
OutputStream os=new FileOutputStream(destFile);
byte[] buffer=new byte[400];
int length=0;
while((length=is.read(buffer))!=-1){
os.write(buffer, 0, length);
}
is.close();
os.close();
}
return "SUCCESS";
}
}
(4)结果页面
FileUploadResult.jap<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!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>result</title>
</head>
<body>
FileName:<s:property value="fileFileName"/><br/>
Type:<s:property value="fileContentType"/><br/>
</body>
</html>
(5)struts.xml配置文件
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 指定文件上传大小 -->
<constant name="struts.multipart.maxSize" value="1048576000"></constant>
<package name="Struts2-test-7-FileDown" extends="struts-default">
<!-- action配置 -->
<action name="Upload" class="com.sw.file.UploadAction">
<result name="success">/FileUploadResult.jsp</result>
</action>
</package>
</struts>
上传文件实例截图:
下载文件
项目:(1)web.xml配置文件
(如上配置,其使用同一项目)(2)下载文件界面
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!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>
<a href="downloadFile.action">下载文件</a>
</body>
</html>
(3)Action文件下载
DownloadAction.java:/*
*@Author swxctx
*@time 2016年10月23日
*/
package com.sw.file;
import java.io.InputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class DownloadAction extends ActionSupport {
//文件下载
public InputStream getDownloadFile(){
return ServletActionContext.getServletContext().getResourceAsStream("/upload/Upload.txt");
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
return "success";
}
}
(4)struts.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 下载文件action配置 -->
<action name="downloadFile" class="com.sw.file.DownloadAction">
<result type="stream" name="success">
<param name="contentDisposition">attachment;filename="Upload.txt"</param>
<param name="inputName">downloadFile</param>
</result>
</action>
</package>
</struts>
下载文件实例截图:
相关文章推荐
- java中struts2实现文件上传下载功能
- java中struts2实现文件上传下载功能实例解析
- 用Struts2更好的实现文件的上传、下载功能以及解决中文名称问题
- struts2实现文件上传与下载功能
- JavaEE中struts2实现文件上传下载功能实例解析
- struts2 实现简单的文件上传、下载功能
- JavaWeb中struts2实现文件上传下载功能实例解析
- Struts2实现文件的上传与动态下载功能。
- JavaEE中struts2实现文件上传下载功能实例解析
- Java程序员从笨鸟到菜鸟之(七十一)细谈struts2(十三)struts2实现文件上传和下载详解
- Struts2实现单文件的上传功能实例详解及源代码
- struts2实现文件的上传和下载实例
- 【SSI开发总结.8】Struts2中实现文件上传功能
- Java程序员从笨鸟到菜鸟之(七十一)细谈struts2(十三)struts2实现文件上传和下载详解
- 利用Apache的commons-net实现FTP的文件读取、上传、下载、删除和移动功能
- 使用Struts2实现文件的上传与动态下载(1)
- Struts2.0实现的文件上传(单附件和多附件)以及附件下载功能
- 用Struts2实现文件上传时显示进度条功能
- struts2文件的上传和下载功能
- Struts2之实现文件上传与下载