5,657
社区成员
发帖
与我相关
我的任务
分享public class DownloadFileAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = -4434797914813536697L;
private InputStream inputStream;
private String fileName;
private long contentLength;
public String execute() throws FileNotFoundException {
File fileToDownload = new File("D:/DownLoad/fileABC.txt");
inputStream = new FileInputStream(fileToDownload);
fileName = fileToDownload.getName();
contentLength = fileToDownload.length();
return SUCCESS;
}<package name="Struts2FileDownload" extends="struts-default">
<action name="downloadFile" class="com.ryf.action.DownloadFileAction">
<result name="success" type="stream">
<param name="contentType">text/plain</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">attachment;filename="${fileName}"</param>
<param name="bufferSize">4096</param>
</result>
</action>
</package><%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Download File</title>
</head>
<body>
<h1 align="center">Struts2 File Download Demo</h1>
<h3 align="center">
<a href="/downloadFile.action">Download this file</a>
</h3>
</body>
</html>