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>
Struts2的下载功能
最新推荐文章于 2021-06-08 10:04:30 发布