首页 技术 正文
技术 2022年11月19日
0 收藏 767 点赞 5,087 浏览 1840 个字

=============================struts 文件下载  ==================================

步骤一: JSP页面
<a href=”download.action?fileName=IMG_0443.JPG”>点击此处下载图片</a>

步骤二: Action页面

package org.zm.action;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class DownLoadAction extends ActionSupport{
//读取下载文件的目录
private String inputPath;
//下载文件的文件名
private String fileName;
//读取下载文件的输入流
private InputStream inputStream;

//下载文件的类型
private String conetntType;

//创建InputStream输入流
public InputStream getInputStream() throws FileNotFoundException{
String path=ServletActionContext.getServletContext().
getRealPath(inputPath);
return new BufferedInputStream(new FileInputStream(path+”\\”+
fileName));
}

@Override
public String execute() {
return SUCCESS;
}
public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}

public String getConetntType() {
return conetntType;
}

public String getInputPath() {
return inputPath;
}

public void setInputPath(String inputPath) {
this.inputPath = inputPath;
}
public void setConetntType(String conetntType) {
this.conetntType = conetntType;
}
}

步骤三: Struts.xml文件
<action name=”download” class=”org.zm.action.DownLoadAction”>
<param name=”inputPath”>/upload</param>
<result name=”success” type=”stream”>
<param name=”contentType”>image/pjpeg</param>
<param name=”inputName”>inputStream</param>
<param name=”contentDisposition”>attachment;filename=”${fileName}”</param>
<param name=”bufferSize”>4096</param>
</result>
</action>

提示: Stream结果类型
contentType: 设置发送到浏览器的MIME类型
contentLength: 文件大小
contentDisposition: 设置响应的HTTP头信息的Content-Disposition参数的值
inputName: 指定Action提供的inputStream类型的属性名称
bufferSize:设置读取和下载文件时缓冲区的大小

struts.xml文件和Action 这两处位置的变量名称一定要正确。

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