基于Struts2框架的文件下载 --- Struts2
一、Struts2指定类型文件的下载
1、最终功能实现的截图:(点击文件下载链接,下载文件 )

2、核心代码
index.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<a href="<%=path%>/downloadFile?download=UploadFile/readme.doc">点击链接下载文件</a>
</body>
</html>
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>
<package name="default" namespace="/" extends="struts-default">
<action name="downloadFile" class="com.thanlon.action.DownloadFileAction">
<result name="success" type="stream"><!-- result类型是流(stream)类型 -->
<param name="inputName">inputStream</param><!-- inputName指向被下载文件的来源,对应Action中返回的InputStream -->
<param name="contentType">${contentType}</param><!-- 下载的内容类型,如图片类型、文档类型等…… -->
<param name="contentDisposition">
<!-- 指定文件下载的处理方式,内联(inline)和附件(attachment)两种方式,attachment会弹出文件保存对话框 -->
attachment;filename=${filename}
</param>
</result>
</action>
</package>
</struts>
DownloadFileAction.java:
package com.thanlon.action;
import java.io.InputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.Action;
public class DownloadFileAction implements Action {
private String downloadPath;// 下载的路径
private String contentType;// 下载文件的类型
private String fileName;// 文件类型
public String getDownloadPath() {
return downloadPath;
}
public void setDownloadPath(String downloadPath) {
this.downloadPath = downloadPath;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
downloadPath = ServletActionContext.getRequest().getParameter(
"download");
System.out.println(downloadPath);// downloadPath为相对路径,打印出“/UploadFile/readme.doc”
int position = downloadPath.lastIndexOf("/");// 借助lastIndexOf函数,从右向左查找第一个/
System.out.println(position);// 打印"/"所造的坐标
if (position > 0) {
fileName = downloadPath.substring(position + 1);// 得到文件的名字(全名,带后缀)
System.out.println(fileName);// 輸出文件名,本例輸出readme.doc
} else {
fileName = downloadPath;
}
contentType = "application/msword";// 指定下载问文件的类型
return SUCCESS;
}
/**
* 返回InputStream
*
* @return
*/
public InputStream getInputStream() {
InputStream inputStream = ServletActionContext.getServletContext()
.getResourceAsStream(downloadPath);
// System.out.println(inputStream);输出inputStream,如果为null表示路径出错
return inputStream;
}
}
二、Struts2多种类型(不指定)文件的下载
1、火狐浏览器下测试对比后,很明显可以看出添加资源文件后,下载时文件的类型被设置成文件真实类型。


下面同样是有无资源文件的对比,也是很明显可以看出。使用资源文件的,下载文件时带上了正确的文件后缀。


2、核心代码
index.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<p>下面是举例说明:</p>
<a href="<%=path%>/downloadFile?download=UploadFile/readme.doc">点击链接下载文件(doc)</a><br>
<a href="<%=path%>/downloadFile?download=UploadFile/thanlon.pdf">点击链接下载文件(pdf)</a><br>
<a href="<%=path%>/downloadFile?download=UploadFile/demo.gif">点击链接下载文件(gif)</a><br>
<a href="<%=path%>/downloadFile?download=UploadFile/logo.png">点击链接下载文件(png)</a>
</body>
</html>
struts.xml:
与上一例(Struts2指定类型文件的下载)的struts.xml相同
DownloadFileAction.java:
package com.thanlon.action; import java.io.InputStream; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport; public class DownloadFileAction extends ActionSupport { private String downloadPath;// 下载的路径
private String contentType;// 下载文件的类型
private String fileName;// 文件类型 public String getDownloadPath() {
return downloadPath;
} public void setDownloadPath(String downloadPath) {
this.downloadPath = downloadPath;
} public String getContentType() {
return contentType;
} public void setContentType(String contentType) {
this.contentType = contentType;
} public String getFileName() {
return fileName;
} public void setFileName(String fileName) {
this.fileName = fileName;
} @Override
public String execute() throws Exception {
// TODO Auto-generated method stub
downloadPath = ServletActionContext.getRequest().getParameter(
"download");
System.out.println(downloadPath);// downloadPath为相对路径,打印出“/UploadFile/readme.doc”
int position = downloadPath.lastIndexOf("/");// 借助lastIndexOf函数,从右向左查找第一个/
// System.out.println(position);// 打印"/"所在的坐标
if (position > 0) {
fileName = downloadPath.substring(position + 1);// 得到文件的名字(全名,带后缀)
System.out.println(fileName);// 輸出文件名
} else {
fileName = downloadPath;
}
int extPos = fileName.lastIndexOf(".");
System.out.println(extPos);
String contentTypeKey = null;
if (extPos > 0) {
contentTypeKey = "struts.contentType" + fileName.substring(extPos);//带.的后缀
System.out.println(contentTypeKey);
}else {
contentTypeKey = "struts.contentType.txt";
//如果没有在属性文件中指定类型:如果是文件,则下载的文件默认后缀为txt文件类型;如果是图片则不会是txt类型,会是自身类型,或者特殊类型,比如jpg会是jfif类型。
}
contentType =getText(contentTypeKey);// 指定下载问文件的类型此类继承了ActionSupport,直接使用getText()函数
System.out.println(contentTypeKey);
return SUCCESS;
}
/**
* 返回InputStream
*
* @return
*/
public InputStream getInputStream() {
InputStream inputStream = ServletActionContext.getServletContext()
.getResourceAsStream(downloadPath);
// System.out.println(inputStream);输出inputStream,如果为null表示路径出错
return inputStream;
}
}
DownloadFileAction.properties:
struts.contentType.gif=image/gif
struts.contentType.jpg=image/jpg
struts.contentType.jpeg=image/jpeg
struts.contentType.bmp=image/bmp
struts.contentType.png=image/png
struts.contentType.txt=text/plain
struts.contentType.swf=application/x-shockwave-flash
struts.contentType.doc=application/msword
struts.contentType.xls=application/vnd.ms-excel
struts.contentType.pdf=application/pdf
struts.contentType.ppt=application/vnd.ms-powerpoint
struts.contentType.exe=application/octet-stream
附:个人网站www.nxl123.cn(后台采用Python Flask框架搭建,2019年1月1日将升级完成并正式启用。哎,本人是学生狗呢!网站做的不好希望大家多多提意见或建议吧!?别骂我就好!……以后SEO什么的还得多向大家学习……)
基于Struts2框架的文件下载 --- Struts2的更多相关文章
- Struts2框架学习(一)——Struts2的概念及搭建
一.Struts2的概念 使用优势:1)自动封装参数 2)参数校验 3)结果的处理(转发|重定向) 4)国际化 5)显示等待页面 6)防止表单重复提交 Struts2具有更加先进的架构以及思想 Str ...
- 第3章 Struts2框架--1、Struts2环境搭建
第3章 Struts2框架--1.Struts2环境搭建 搭建步骤: 1.从下载http://struts.apache.org 没找到Struts2.3.16版,就下载了2.3.29 2.拷贝后解压 ...
- Struts2框架之类型转换 --Struts2框架
Struts2框架实现了大多数常见的用于类型转换的转换器,开发人员不用自己编写类型转换代码,就可以实现数据类型的转换.下面一个Struts2框架类型转换的简单事例, 本例可在使用validate()方 ...
- struts2框架之文件下载(参考第三天学习笔记)
下载 1. 下载是一种响应方式 正常的响应:响应正文是html:response.getWriter().print("html"); 下载的响应: 1.一个流:字节数据:resp ...
- struts2框架概述
框架概述 什么是框架,为什么使用框架,框架优点 框架(framework)是一个基本概念上的结构,用于去解决或者处理复杂的问题 框架,即framework.其实就是某种应用的半成品,就是一组组件,供你 ...
- Struts2框架简介和示例
struts2框架 Struts2是java web的框架,在Java Web开发中,表示层框架,其核心是通过扩展Servlet来帮助处理http请求. Struct2的基本流程 Struct2的框架 ...
- MVC模式-----struts2框架(2)
MVC模式-----struts2框架 第一个struts2程序 struts2框架是通过一个过滤器将struts2集成到Web应用程序中的,这个过滤器的对象是StrutsprepareAndExec ...
- Struts2框架起源
曾经也用过S2SH框架做过几个项目,都不是工作中的,学习WEB开发的时候接触的第一套框架也是S2SH,可是工作之后一直没实用到S2SH 框架进行开发. 感觉曾经用这个框架的时候根本没有深入去了解这个框 ...
- Java之struts2框架学习
Java之struts2框架学习 About Struts2 Struts也是一款MVC框架 , Struts2是Struts的下一代产品,是在Struts1和WebWork的技术基础上进行了合并的全 ...
随机推荐
- hog cython
pip安装cython之后,将下面代码写入hogtest2.pyx文件(我通过改文件后缀新建) import numpy as np from PIL import Image cimport num ...
- Install Virtualbox on ubuntu
1.Use the command: sudo apt-get install virtualbox
- 再谈fedora 23中的flash的安装
安装下载, flash插件的地址是 https://get.adobe.com/flashplayer/ ls 的显示, 记住两个很常用的选项: 指定 "假设的屏幕的宽度" , - ...
- Kubernetes(k8s)入门、单机版安装、kuberctl指令、k8s服务实例
1.切换root .关闭centos自带的防火墙 # systemctl disable firewalld # systemctl stop firewalld .安装etcd和kubernetes ...
- What is the difference between visibility:hidden and display:none?
What is the difference between visibility:hidden and display:none? 答案1 display:none means that the t ...
- win32 汇编学习(2):消息框
这一次,我们将用汇编语言写一个 Windows 程序,程序运行时将弹出一个消息框并显示"你好,我的第一个Win32汇编程序". 理论知识 Windows 为编写应用程序提供了大量的 ...
- R语言 格式化数字
x = 1111111234.6547389758965789345 y = formatC(x, digits = 8, format = "f") # [1] "11 ...
- LOJ#2427. 「POI2010」珍珠项链 Beads
题目地址 题目链接 题解 不会算复杂度真是致命,暴力枚举k每次计算是n/2+n/3+n/4+...+1的,用调和级数算是\(O(nlogn)\)的... 如果写哈希表的话能够\(O(nlogn)\), ...
- 【C#】Using的一个比较好的语言文字解释
其实很早就开始使用using了.但是对这个语法糖我自己一直没有总结也没有一个很好的文字描述解释.今天看其他的博文的时候发现有人对其做了简单的解释我觉得很好,很适合一种讲解.于是抄录下来 using ( ...
- codeforce 886C Petya and Catacombs (map,思路)
突然发现百度不到这题的单独题解(果然是因为这是水题么),那我就来写一个了~ 先把题给贴了. C. Petya and Catacombs time limit per test 1 second me ...