最近项目中需要做一个开发平台,第一个功能就是实现文档对外提供下载功能,项目中用到的是Struts2框架,因此写了一个简单的ACTION在此记录学习。

第一步:首先需要新建一个ACTION:

import ins.framework.web.Struts2Action;
import org.apache.commons.codec.binary.Base64;import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;/*** 合作方下载资料平台扩展* @author  Tanyunlong on 2016/10/11.*/
public class OpenPlatformAction extends Struts2Action {//下载页面点击跳转参数private String index;//下载文件名private String fileName;//输入流private InputStream fileStream;public String openPlatformMethod(){System.out.println("开放平台openPlatformMethod 执行====");if ("0".equals(index)){return  "success0";//跳转到平台首页}else if ("1".equals(index)){return "success1";//跳到平台概述}else if ("2".equals(index)){return "success2";//跳到业务流程}else if ("3".equals(index)){return "success3";//跳到下载接口文档}return  null;}public InputStream download(){if ("a".endsWith(fileName)){fileName="(通用版).docx";}else if ("b".endsWith(fileName)){fileName="解答.doc";}else if ("c".endsWith(fileName)){fileName="webservice调用样例-JAVA.java";}else if ("d".endsWith(fileName)){fileName="webservice调用样例-PHP.php";}else if ("e".endsWith(fileName)){fileName="已上线省市区.xls";}else if ("f".endsWith(fileName)){fileName="全国省市区码表.xls";}else if ("g".endsWith(fileName)){fileName="合作流程图.vsdx";}else {return null;}//获取下载文件路径String filenamedownload=getServletContext().getRealPath("/webnew/common/"+fileName);//转换文件名编码if (getRequest().getHeader("User-Agent").toLowerCase().indexOf("firefox")>0){try {fileName=new String(fileName.getBytes("UTF-8"),"iso-8859-1");}catch (UnsupportedEncodingException e){e.printStackTrace();}}else {try{fileName= URLEncoder.encode(fileName,"UTF-8");}catch (UnsupportedEncodingException e){e.printStackTrace();}}InputStream fis=null;try {fis=new FileInputStream(filenamedownload);}catch (FileNotFoundException e){e.printStackTrace();}return fis;}public  String fileDownload(){System.out.println("fileDownload()============");fileStream=download();return  SUCCESS;}public InputStream getFileStream() {return fileStream;}public void setFileStream(InputStream fileStream) {this.fileStream = fileStream;}public String getIndex() {return index;}public void setIndex(String index) {this.index = index;}public String getFileName() {return fileName;}public void setFileName(String fileName) {this.fileName = fileName;}
}

第二步:配置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="openPlatform" extends="nsp" namespace="/openPlatform"><action name="openPlatformMethod" class="OpenPlatformAction" method="openPlatformMethod"><result name="success0">/openPlatform/openPlatform.jsp</result><result name="success1"></result><result name="success2"></result><result name="success3">/openPlatform/openPlatform_down.jsp</result></action><action name="fileDownload" class="OpenPlatformAction" method="fileDownload"><result name="success" type="stream"><param name="contentType">application/octet-stream;charset=ISO8859-1</param><param name="inputName">fileStream</param><param name="contentDisposition">attachment;filename=${fileName}</param><param name="bufferSize">1024</param></result></action></package>
</struts>

三:对于struts.xml中几个标签的介绍:

contentType 指定下载文件的文件类型 —— application/octet-stream 表示无限制
inputName 流对象名 —— 比如这里写inputStream,它就会自动去找Action中的getInputStream方法。
contentDisposition 使用经过转码的文件名作为下载文件名 —— 默认格式是attachment;filename="${fileName}",将调用该Action中的getFileName方法。其中:attachment :下载时会打开下载框
bufferSize 下载文件的缓冲大小

Struts2实现文件下载功能相关推荐

  1. java实现文件下载的两种方式6_struts2实现文件下载功能

    本文实例为大家分享了struts2下实现文件下载功能,供大家参考,具体内容如下 下面以实现一个图片下载功能为例: 1. 项目结构 2. web.xml xmlns="http://java. ...

  2. 用Jsp来实现文件下载功能的几种方式

    用Jsp来实现文件下载功能的几种方式 1.最直接最简单的,方式是把文件地址直接放到html页面的一个链接中.这样做的缺点是把文件在服务器上的路径暴露了,并且还无法对文件下载进行其它的控制(如权限).这 ...

  3. ASP.NET网页中RAR、DOC、PDF等文件下载功能实例源代码

    以前做asp.net下载功能的时候都是采用: <a href="http://www.wang0214.com/wgcms">下载</a> 的方式来实现下载 ...

  4. ASP.NET MVC 向浏览器发送文件以提供文件下载功能

    撑到大三了,结果发现周围的同学更加堕落了,尤其是某些人,表面上看起来很认真,实际上三天打鱼,两天晒网,结果一事无成,却还要抱怨学校教育失败. 为了吸取他们的教训,就算是一个小小的编码问题,我也要努力解 ...

  5. Spring Boot入门(11)实现文件下载功能

      在这篇博客中,我们将展示如何在Spring Boot中实现文件的下载功能.   还是遵循笔者写博客的一贯风格,简单又不失详细,实用又能让你学会.   本次建立的Spring Boot项目的主要功能 ...

  6. php下载的文件不是汉字,php实现支持中文的文件下载功能示例

    前言 本文主要给大家介绍了关于php实现支持中文的文件下载功能的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧. 问题说明 文件下载,通常有一种最为简单的方法,那就是将url ...

  7. java文件日志功能_JAVA文件下载功能问题解决日志

    今天给报告系统做了个下载功能,遇到了挺多问题,通过查资料一一解决了. 1.首先遇到的问题是:java后台的输出流输出之后,没有任何报错,浏览器端不弹出保存文件的对话框,原本是ajax请求到后台的con ...

  8. Struts2之文件下载

    众所周知,在struts.xml中,每个action元素内,可以有一个或多个的result子元素,然后我们可以通过在Action类的execute方法的返回值与result元素的name属性进行匹配, ...

  9. Spring Boot基础学习笔记15:实现文件下载功能

    文章目录 零.学习目标 一.文件下载概述 二.实现文件下载功能 (一)创建Spring Boot项目 (二)整合Bootstrap (三)准备待下载文件 (四)编写文件下载页面 (五)编写文件下载控制 ...

最新文章

  1. 20175221曾祥杰 实验四《Android程序设计》
  2. WSDM 2022 | 点击率模型特征交叉方向的发展及CAN模型介绍
  3. python的print输出_python中的print()输出
  4. ubuntu下安装mssql(sqlserver)客户端及使用
  5. SpringBoot 之 跳转页面的几种方法
  6. 数据库简介(初步了解数据库)
  7. 都说直播带货不行了 国美为啥还要继续趟这个水?
  8. 恒生UFX交易接口基本介绍说明
  9. 猿人学web端爬虫攻防大赛赛题解析_第六题:js 混淆 - 回溯
  10. 人工智能:《时代周刊》2019年度100大最佳发明榜单发布!
  11. ds5100更换电池 ibm_IBMDS5100更换电池
  12. 自然语言处理是什么?学习自然语言处理(NLP)
  13. 最浪漫的程序员表白代码打动美人心
  14. 洛谷 P1878 舞蹈课 —— 小顶堆
  15. 【安全开发】IOS安全编码规范
  16. html自动点击屏幕,虚拟按键大师(屏幕自动点击辅助器)
  17. webrtc笔记(5): 基于kurento media server的多人视频聊天示例
  18. 华为1+X网络系统建设与运维(中级)——链路聚合
  19. 高精度加法(C++基础算法)
  20. MobaXterm(终端工具)下载安装

热门文章

  1. NBIOT模组M5310接入OneNET平台
  2. 聚合函数——MySQL
  3. GifCam gif录制软件
  4. 删除文件时显示该文件不在此文件夹中的原因
  5. 百度网盘文件分享设置个性密码
  6. 处理、分析iOS App的Crash Reports
  7. ABAP中时间戳的处理
  8. html中的colspan是什么意思
  9. [数据结构]二叉树的结构及实现
  10. 2020年房价趋势_2020年最大的设计趋势