使用Struts2做一个简单的文件下载。

首先,导包,写配置文件就不说了。

进入主题。

文件下载操作类:FileDownload.java

import java.io.InputStream;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class FileDownload extends ActionSupport{
 
 private InputStream inputStream;
 private String fileName;

public String getFileName() {
  return fileName;
 }

public void setFileName(String fileName) {
  this.fileName = fileName;
 }

public InputStream getInputStream() {
  fileName="144003645679.jpg";
  return ServletActionContext.getServletContext().getResourceAsStream("/upload/144003645679.jpg");
  
 }
 
 public String execute() {
  return SUCCESS;
 }
}

Struts.xml文件

<package name="fileaction" namespace="/" extends="struts-default">

<action name="filedownload" class="com.dkx.action.FileDownload" >
    <result name="success" type="stream">
    <!-- contentType 指定下载文件的文件类型 —— application/octet-stream 表示无限制   -->
               <param name="contentType">text/plain</param>
              
               <!-- contentDisposition 使用经过转码的文件名作为下载文件名 ——
                 默认格式是attachment;filename="${fileName}",
                 将调用该Action中的getFileName方法。 --> 
                
               <param name="contentDisposition">attachment;fileName="${fileName}"</param> 
              
               <!-- inputName 流对象名 ——
                比如这里写inputStream,它就会自动去找Action中的getInputStream方法。 -->
              
               <param name="inputName">inputStream</param> 
              
               <!-- 指定下载文件的缓冲大小 -->
              
               <param name="bufferSize">50000000</param>
         </result> 
    </action>
   </package>

Jsp页面文件:

<a href="${basePath }filedownload.action">下载</a>

至此大功告成。点击下载。。

Struts2 文件下载的更多相关文章

  1. Struts2文件下载浅析

    Struts2极大的简化了文件上传和下载,本文将主要介绍一下Struts2文件下载的实现1.功能主要是,在下载页面点击下载后,则下载相应的文件 2.代码部分jsp页面downloadPage:< ...

  2. struts2文件下载 <result type="stream">

    <!--struts.xml配置--> <action name="download" class="com.unmi.action.DownloadA ...

  3. struts2文件下载及 <param name="inputName">inputStream</param>的理解

    转自:http://blog.csdn.net/wnczwl369/article/details/7483290 转自:http://hi.baidu.com/c2_sun/item/934a542 ...

  4. struts2文件下载相关信息

    struts.xml文件配置: <span style="font-size:16px;"><?xml version="1.0" encod ...

  5. Struts2文件下载

    1). Struts2 中使用 type="stream" 的 result 进行下载 2). 可以为 stream 的 result 设定如下参数 contentType: 结果 ...

  6. struts2文件下载出现Can not find a java.io.InputStream with the name的错误

    今天在用struts2就行文件下载时出现如下错误: Servlet.service() for servlet default threw exception java.lang.IllegalArg ...

  7. Struts2 文件下载(中文处理方法以及控制下载文件名称和扩展名)

    Struts2的框架提供了现成的文件下载方式,大大简化了开发下载功能的便利性.网上的例子有很多,我把一些大家普遍比较关注的点,集中一下,给出一个整体方案. 一般我们照着书本或者网上的列子写出了一个De ...

  8. struts2文件下载,动态设置资源地址

    转自:http://blog.csdn.net/ctrl_shift_del/article/details/6277340 ServletActionContext.getServletContex ...

  9. struts2文件下载 出现Can not find a java.io.InputStream with the name的错误

    成功代码: 前台界面jsp: <a style="text-decoration:none;" href="<%=path %>/main/frontN ...

随机推荐

  1. Longest Palindromic Substring——LeetCode

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  2. Matlab:拟合(1)

    拟合练习: function f = curvefun(x, tdata) f = (x()*x()*x()) / (x()-x()) * ( exp(-x()*tdata)/(x()-x()) + ...

  3. [LeetCode] 187. Repeated DNA Sequences 解题思路

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  4. UVA196-Spreadsheet(拓扑排序)

    Spreadsheet In 1979, Dan Bricklin and Bob Frankston wrote VisiCalc, the first spreadsheet applicatio ...

  5. Migration workstation vms to openstack kvm

    Migration workstation vms to openstack kvm 分别分成(磁盘是否拆分,vms 是否 有多个磁盘驱动器) 1, linux迁移 vmware workstatio ...

  6. wxPython跨线程调用

    版权所有,转载请注明出处:http://guangboo.org/2013/08/23/wxpython-non-gui-thread-call-gui-method 之前有介绍了<wxPyth ...

  7. js跨域问题新方案

    只要创建一个空图片. js代码: var data = "http://localhost:8080/test?id="+id+"&content="+ ...

  8. 在EF中执行SQL语句

    你可能要问,我用EF不就为了避免写SQL吗?如果要写SQL我不如直接用ADO.NET得了.话虽然这么说没错,可有些时候使用EF操作数据还是有一些不方便,例如让你根据条件删除一组记录,如果按照正常的流程 ...

  9. jQuery表单验证以及将表单序列化为json对象小练习

    jquery表单验证(非实时验证),同时,将表单序列化为json对象提交表单. <!DOCTYPE html> <html lang="en"> <h ...

  10. cocos日记

    n/2/2015 ADT       E:\All Setup\eclipse_past\Eclipse_plugins_ADT 15/2/2015 配置cocos win32 开发环境 链接:htt ...