我用 public InputStream getDownloadFile()
{
// 获得 ServletContext 对象
ServletContext servletContext =
ServletActionContext.getServletContext(); // 返回被下载文件的 InputStream 对象
return servletContext.getResourceAsStream(inputPath);
}
获取到了一个输入流对象,怎么只是下载到浏览器上?
<action name="download" class="org.usc.file.DownloadAction">
<result name="success" type="stream">
<param name="contentDisposition">attachment;filename="${fileName}"</param>
<param name="inputName">downloadFile</param>
</result>
</action>
struts已经配置过了
{
// 获得 ServletContext 对象
ServletContext servletContext =
ServletActionContext.getServletContext(); // 返回被下载文件的 InputStream 对象
return servletContext.getResourceAsStream(inputPath);
}
获取到了一个输入流对象,怎么只是下载到浏览器上?
<action name="download" class="org.usc.file.DownloadAction">
<result name="success" type="stream">
<param name="contentDisposition">attachment;filename="${fileName}"</param>
<param name="inputName">downloadFile</param>
</result>
</action>
struts已经配置过了
* 下载文件
* @param filePath 文件路径(物理路径)
* @param fileName 源文件名称
*/
public void downLoadFile(String filePath, String fileName) {
File file = new File(filePath);
if (!file.exists() || file.isDirectory()) {
return;
}
InputStream input =null;
OutputStream output = null;
try {
input = new FileInputStream(file);
output = getResponse().getOutputStream();
getResponse().setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
byte[] buffer = new byte[1024];
int i = 0;
while ((i = input.read(buffer)) != -1) {
output.write(buffer, 0, i);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(null !=output) {
output.flush();
output.close();
}
if(null !=input) {
input.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
我把段代码封装在父类action中,需要做下载时,直接调用一下就OK了!