Struts2的下载功能

jsp页面部分
<a href="download.action"><font size="5"color="red">下载插件</font></a> 

Action部分:
package com.hrbsys.luzhiwen.action;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URLEncoder;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class FileDownloadAction extends ActionSupport {

private static final long serialVersionUID = 1L;

//基本设置类型
private String contentType;
private int contentLength;
private String contentDisposition;

//输入流
private InputStream inputStream;
private String inputPath;

public String execute() throws Exception {
inputStream = new FileInputStream(inputPath);
contentLength = inputStream.available();
return SUCCESS;
}


public String getContentType() {
return contentType;
}

public String getInputPath() {
return inputPath;
}

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

public void setContentType(String contentType) {
this.contentType = contentType;
}

public int getContentLength() {
return contentLength;
}

public void setContentLength(int contentLength) {
this.contentLength = contentLength;
}

public String getContentDisposition() {
return contentDisposition;
}

public void setContentDisposition(String contentDisposition){
this.contentDisposition = contentDisposition;
}

public InputStream getInputStream() {
return inputStream;
}

public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}

}

Struts2.xml部分
<action name="download"class="com.hrbsys.luzhiwen.action.FileDownloadAction">
<!--指定被下载资源的位置 -->
<paramname="inputPath">D:\kqxtJQ\software\ZWY_ActiveX.zip</param>
<!--配置结果类型为stream的结果 -->
<result name="success"type="stream">
<!--指定下载文件的文件类型 -->
<paramname="contentType">application/zip</param>
<paramname="inputName">inputStream</param>
<!--指定下载文件的下载方式及下载时的保存文件名,filename保存时的文件名必须有扩展名,扩展名指示了下载类型的图标-->
<paramname="contentDisposition">
attachment;filename=ZWY_ActiveX.zip
</param>
<!--指定下载文件的缓冲区大小 -->
<paramname="bufferSize">4096</param>
</result>
</action>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值