Struts下载中struts.xml配置信息
<action name="download" class="com.lzw.web.action.DownloadAction" method="download">
<!-- 以流的形式把文件反应给客服端 -->
<result type="stream">
<!-- 输入流名称 -->
<param name="inputName">input</param>
<param name="contentDisposition">attachment;filename=${fileName}</param>
<param name="contentType">application/octet-stream</param>
</result>
</action>
动作类
package com.lzw.web.action;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import com.opensymphony.xwork2.ActionSupport;
/**
* 在Action中写一个输入流属性
* 提供get方法
* 写一个download方法,给input赋值
* 写struts.xml文件添加action
* @author HP
*
*/
public class DownloadAction extends ActionSupport{
private InputStream input;
private String fileName;
public String getFileName() {
return fileName;
}
public InputStream getInput() {
return input;
}
public String download() throws FileNotFoundException, UnsupportedEncodingException {
fileName = "a.jpg";
String path = "D:\\" + fileName;
input = new FileInputStream(path);
//中文显示
// fileName = "照片.jpg";
// fileName = URLEncoder.encode(fileName, "UTF-8");
return SUCCESS;
}
}