首页 技术 正文
技术 2022年11月8日
0 收藏 337 点赞 1,877 浏览 6583 个字

一、Struts2指定类型文件的下载

1、最终功能实现的截图:(点击文件下载链接,下载文件 )

基于Struts2框架的文件下载 — Struts2

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%>" rel="external nofollow" rel="external nofollow" > <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" rel="external nofollow" rel="external nofollow" >点击链接下载文件</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、火狐浏览器下测试对比后,很明显可以看出添加资源文件后,下载时文件的类型被设置成文件真实类型。

基于Struts2框架的文件下载 — Struts2

基于Struts2框架的文件下载 — Struts2

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

基于Struts2框架的文件下载 — Struts2

基于Struts2框架的文件下载 — Struts2

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%>" rel="external nofollow" rel="external nofollow" > <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" rel="external nofollow" rel="external nofollow" >点击链接下载文件(doc)</a><br>
<a href="<%=path%>/downloadFile?download=UploadFile/thanlon.pdf" rel="external nofollow" >点击链接下载文件(pdf)</a><br>
<a href="<%=path%>/downloadFile?download=UploadFile/demo.gif" rel="external nofollow" >点击链接下载文件(gif)</a><br>
<a href="<%=path%>/downloadFile?download=UploadFile/logo.png" rel="external nofollow" >点击链接下载文件(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什么的还得多向大家学习……)

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,564
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,412
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,185
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905