Struts2-上传/下载文件功能实现
来源:互联网 发布:德芙巧克力知乎 编辑:程序博客网 时间:2024/05/17 04:15
上传文件
项目:
(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;}@Overridepublic String execute() throws Exception {// TODO Auto-generated method stubfor(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");}@Overridepublic String execute() throws Exception {// TODO Auto-generated method stubreturn "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>
下载文件实例截图:
0 0
- Struts2-上传/下载文件功能实现
- struts2实现文件上传与下载功能
- struts2 实现简单的文件上传、下载功能
- struts2实现文件上传功能
- Struts2实现文件下载功能
- struts2文件的上传和下载功能
- 基于struts2实现文件上传功能
- Struts2+FileFlashUpload实现多文件上传功能
- struts2文件上传功能的实现
- struts2中实现文件上传功能
- Struts2中实现多文件上传功能
- Struts2.x中实现文件上传功能
- Struts2实现文件的上传功能
- Struts2之实现文件上传与下载
- Struts2之实现文件上传与下载
- Struts2之实现文件上传与下载
- struts2 实现文件的上传与下载
- Struts2实现文件上传和下载
- 【HDU 1398】【母函数入门题】Square Coins【硬币种类有1^2,2^2,3^2,4^2...17^2,这几种;输入n;求出能够组合成n的组合有多少种】
- 机器数 真值 原码 反码 补码
- 文章标题
- servlet执行流程
- centos + python + uwsgi 搭建网站
- Struts2-上传/下载文件功能实现
- memcache的搜索次数,每页数据存在memcache中,分页普通页面
- 树莓派搭建web系列
- 无向图欧拉路的判定
- html5 输入某范围求水仙花数
- android中EditText监听键盘输入
- Hiho 121 后缀数组二·重复旋律2(最长不可重叠重复子串问题)
- 161023
- KThread