一、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的更多相关文章

  1. Struts2框架学习(一)——Struts2的概念及搭建

    一.Struts2的概念 使用优势:1)自动封装参数 2)参数校验 3)结果的处理(转发|重定向) 4)国际化 5)显示等待页面 6)防止表单重复提交 Struts2具有更加先进的架构以及思想 Str ...

  2. 第3章 Struts2框架--1、Struts2环境搭建

    第3章 Struts2框架--1.Struts2环境搭建 搭建步骤: 1.从下载http://struts.apache.org 没找到Struts2.3.16版,就下载了2.3.29 2.拷贝后解压 ...

  3. Struts2框架之类型转换 --Struts2框架

    Struts2框架实现了大多数常见的用于类型转换的转换器,开发人员不用自己编写类型转换代码,就可以实现数据类型的转换.下面一个Struts2框架类型转换的简单事例, 本例可在使用validate()方 ...

  4. struts2框架之文件下载(参考第三天学习笔记)

    下载 1. 下载是一种响应方式 正常的响应:响应正文是html:response.getWriter().print("html"); 下载的响应: 1.一个流:字节数据:resp ...

  5. struts2框架概述

    框架概述 什么是框架,为什么使用框架,框架优点 框架(framework)是一个基本概念上的结构,用于去解决或者处理复杂的问题 框架,即framework.其实就是某种应用的半成品,就是一组组件,供你 ...

  6. Struts2框架简介和示例

    struts2框架 Struts2是java web的框架,在Java Web开发中,表示层框架,其核心是通过扩展Servlet来帮助处理http请求. Struct2的基本流程 Struct2的框架 ...

  7. MVC模式-----struts2框架(2)

    MVC模式-----struts2框架 第一个struts2程序 struts2框架是通过一个过滤器将struts2集成到Web应用程序中的,这个过滤器的对象是StrutsprepareAndExec ...

  8. Struts2框架起源

    曾经也用过S2SH框架做过几个项目,都不是工作中的,学习WEB开发的时候接触的第一套框架也是S2SH,可是工作之后一直没实用到S2SH 框架进行开发. 感觉曾经用这个框架的时候根本没有深入去了解这个框 ...

  9. Java之struts2框架学习

    Java之struts2框架学习 About Struts2 Struts也是一款MVC框架 , Struts2是Struts的下一代产品,是在Struts1和WebWork的技术基础上进行了合并的全 ...

随机推荐

  1. Redis 安装,配置以及数据操作

    Nosql介绍 Nosql:一类新出现的数据库(not only sql)的特点 不支持SQL语法 存储结构跟传统关系型数据库中那种关系表完全不同,nosql中存储的数据都是k-v形式 Nosql的世 ...

  2. Connections in Galaxy War (逆向并查集)题解

    Connections in Galaxy War In order to strengthen the defense ability, many stars in galaxy allied to ...

  3. switch反汇编(C语言)

    在分支较多的时候,switch的效率比if高,在反汇编中我们即可看到效率高的原因 0x01分支结构不超过3个 #include <stdio.h> void main() { int x ...

  4. 【重新分配分片】Elasticsearch通过reroute api重新分配分片

    elasticsearch可以通过reroute api来手动进行索引分片的分配. 不过要想完全手动,必须先把cluster.routing.allocation.disable_allocation ...

  5. SSM项目问题中遇到 ArrayList添加元素的问题

    记录项目开发中 一次有趣的debug经历 本来是在做单元测试的,但是发现如下代码 有问题.. ProductCategory p = new ProductCategory(); for (int i ...

  6. Asp.Net 之 OnClientClick 与 OnClick 的执行顺序

    Asp.net 中 OnClientClick 与 OnClick 的执行顺序为:客户端的OnClientClick先执行,服务器端的OnClick后执行. 拓展:在执行完客户端的OnClientCl ...

  7. P2055 [ZJOI2009]假期的宿舍

    思路 看到复杂的匹配条件,发现要让一个人和一个床匹配,所以就每个有床的人(指本校学生)和t连一条边,每个需要床的人(指外校的人和不回家的人)和s连一条边,i和j互相认识就把i和j的床连在一起,自己和自 ...

  8. netty基础

    1,ServerBootstrap  [Bootstrap] 

  9. 论文笔记:Parallel Tracking and Verifying: A Framework for Real-Time and High Accuracy Visual Tracking

    Parallel Tracking and Verifying: A Framework for Real-Time and High Accuracy Visual Tracking  本文目标在于 ...

  10. Terminal run py文件

    cd Documents cd PythonCode python3 hello.py Text Editor: Atom Atom 可以用来写 python 脚本 (文件后缀名 .py). 但是不用 ...