转自:http://blog.csdn.net/hzc543806053/article/details/7538723

文件上传链接:

1)Servlet 文件上传 ————  点击打开链接

2)Struts2 文件上传 ———— 点击打开链接

文件下载是一个很常见的功能,用struts2实现文件下载的步骤:

一)定义一个Action类,FileDownload.java

  1. package com.struts2.filedownload;
  2. import java.io.InputStream;
  3. import org.apache.struts2.ServletActionContext;
  4. import com.opensymphony.xwork2.ActionSupport;
  5. //文件下载
  6. public class FileDownload extends ActionSupport{
  7. private int number ;
  8. private String fileName;
  9. public int getNumber() {
  10. return number;
  11. }
  12. public void setNumber(int number) {
  13. this.number = number;
  14. }
  15. public String getFileName() {
  16. return fileName;
  17. }
  18. public void setFileName(String fileName) {
  19. this.fileName = fileName;
  20. }
  21. //返回一个输入流,作为一个客户端来说是一个输入流,但对于服务器端是一个 输出流
  22. public InputStream getDownloadFile() throws Exception
  23. {
  24. if(1 == number)
  25. {
  26. this.fileName = "Dream.jpg" ;
  27. //获取资源路径
  28. return ServletActionContext.getServletContext().getResourceAsStream("upload/Dream.jpg") ;
  29. }
  30. else if(2 == number)
  31. {
  32. this.fileName = "jd2chm源码生成chm格式文档.rar" ;
  33. //解解乱码
  34. this.fileName = new String(this.fileName.getBytes("GBK"),"ISO-8859-1");
  35. return ServletActionContext.getServletContext().getResourceAsStream("upload/jd2chm源码生成chm格式文档.rar") ;
  36. }
  37. else
  38. return null ;
  39. }
  40. @Override
  41. public String execute() throws Exception {
  42. return SUCCESS;
  43. }
  44. }

二)在struts.xml文件中配置相关信息

  1. <struts>
  2. <package name="struts2" extends="struts-default">
  3. <action name="FileDownload" class="com.struts2.filedownload.FileDownload">
  4. <result name="success" type="stream">
  5. <param name="contentType">text/plain</param>
  6. <param name="contentDisposition">attachment;fileName="${fileName}"</param>
  7. <param name="inputName">downloadFile</param>
  8. <param name="bufferSize">1024</param>
  9. </result>
  10. </action>
  11. </package>
  12. </struts>

1.结果类型必须要写成 type="stream"  ,与之对应的处理类是 org.apache.struts2.dispatcher.StreamResult

2.涉及到的参数:

3.

1)  <param name="contentDisposition">attachment;fileName="${fileName}"</param>

contentDisposition默认是 inline(内联的), 比如说下载的文件是文本类型的,就直接在网页上打开,不能直接打开的才会打开下载框自己选择

2)  attachment :下载时会打开下载框

3)  fileName="${fileName}" :在这定义的名字是一个动态的,该名字是显示在下载框上的文件名字

4.<param name="inputName">downloadFile</param>,这个downloadFile名字要和FileDownload.java类中的getDownloadFile()方法名去掉get 一致

三)用于显示下载的链接界面 filedownload.jsp

    1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    2. <%
    3. String path = request.getContextPath();
    4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    5. %>
    6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    7. <html>
    8. <head>
    9. <base href="<%=basePath%>">
    10. <title>My JSP 'filedownload.jsp' starting page</title>
    11. <meta http-equiv="pragma" content="no-cache">
    12. <meta http-equiv="cache-control" content="no-cache">
    13. <meta http-equiv="expires" content="0">
    14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    15. <meta http-equiv="description" content="This is my page">
    16. <!--
    17. <link rel="stylesheet" type="text/css" href="styles.css">
    18. -->
    19. </head>
    20. <body>
    21. <h2>文件下载内容:</h2><br/>
    22. Dream.jpg:<a href="FileDownload.action?number=1">点击下载</a><br/>
    23. jd2chm源码生成chm格式文档.rar:<a href="FileDownload.action?number=2">点击下载2</a>
    24. </body>
    25. </html>

struts.xml中可以使用el表达式和ognl表达式的更多相关文章

  1. struts.xml中出现Package struts2 extends undefined package struts-default解决办法

    在struts.xml中出现extends undefined package struts-default,经过查阅资料原来是因为没有联网的缘故.这样解决:在myeclipse中关联本地的dtd文件 ...

  2. 在Struts.xml中的result元素指的是:指定动作类的动作方法执行完后的结果视图.

    result结果集 上一篇文章主要讲Struts2框架(4)---Action类访问servlet这篇主要讲result结果集 在Struts.xml中的result元素指的是:指定动作类的动作方法执 ...

  3. struts.xml中出现extends undefined package struts-default解决的方法

    在struts.xml中出现extends undefined package struts-default,经过查阅资料原来是由于没有联网的缘故. 这样解决:在myeclipse中关联本地的dtd文 ...

  4. el 表达式 和 ognl表达式

    el (expression language) el 基础操作符 el 能够隐含对象(就是可以直接访问的) el 的两种使用方式,第二种好像在jsp中没有什么用,主要用于jsf el能够访问的对象( ...

  5. 关于Struts2中的值栈与OGNL表达式

    1.1.1    OGNL概述: Object Graphic Navigation Language(对象图导航语言)的缩写 * EL     :OGNL比EL功能强大很多倍. 它是一个开源项目. ...

  6. struts.xml中的配置常量的含义

    struts.serve.static.browserCache 该属性设置浏览器是否缓存静态内容.当应用处于开发阶段时,我们希望每次请求都获得服务器的最新响应,则可设置该属性为false. stru ...

  7. el表达式跟ognl表达式的区别(转)

    EL表达式: >>单纯在jsp页面中出现,是在四个作用域中取值,page,request,session,application.>>如果在struts环境中,它除了有在上面的 ...

  8. el表达式跟ognl表达式的区别

    :  EL表达式 单纯在jsp页面中出现,是在四个作用域中取值,page,request,session,application. 如果在struts环境中,它除了有在上面的四个作用域的取值功能外,还 ...

  9. struts2 ValueStack详解,页面获取值el表达式、ognl表达式

    http://www.blogjava.net/max/archive/2007/04/28/114417.html 我想用的是el表达式! http://www.cnblogs.com/belief ...

随机推荐

  1. JAVA命令大全

    1.java.exe:======================运行java程序,这个相信每一位用Java的人知道了. 2.javac.exe:======================编译的Ja ...

  2. PhpStorm 设置php代码格式

    phpstorm 代码格式化方法: 快捷键:Ctrl + Alt + L 设置代码样式:File -> Settings -> Code Style ->PHP 根据个人php代码规 ...

  3. Clover(资源管理器增强)

    Clover(资源管理器增强) 下载地址:http://www.orsoon.com/Soft/13157.html 功能: Windows Explorer 资源管理器的一个扩展,为其增加类似谷歌  ...

  4. NGUI 粒子显示在上级

    http://bbs.taikr.com/thread-2272-1-1.html [NGUI]3.0+版本,粒子在UI后面显示 -- : 48人阅读 评论() 收藏 举报 [Unity3D][NGU ...

  5. node.js模拟qq漂流瓶

    (文章是从我的个人主页上粘贴过来的,大家也可以访问我的主页 www.iwangzheng.com) node.js模拟简易漂流瓶,页面有扔瓶子和捡瓶子的功能,一个瓶子只能被捡到一次,阅读完就置状态位, ...

  6. nginx学习(一):基本安装

    转载自http://summervast.blog.51cto.com/690507/385511 注意:可能因版本不同,个别指令不起作用,需要注意版本灵活安装,我在安装时也遇到过此问题 开始学习ng ...

  7. 【SpringMVC】SpringMVC系列10之视图与视图解析器

    10.视图与视图解析器 10.1.概述     请求处理方法执行完成后,最终返回一个 ModelAndView处理方法,Spring MVC 也会在内部将它们装配成一个ModelAndView 对象, ...

  8. 【云计算】Netflix 开源持续交付平台 Spinnaker

    oschina        发布于: 2015年11月19日 (0评)          分享到:    收藏 +1 CDS首都在线全球云主机.全球私有网络,开工送礼,免费试用! »   日前,Ne ...

  9. Bulls and Cows

    You are playing the following Bulls and Cows game with your friend: You write down a number and ask ...

  10. static_cast dynamic_cast const_cast reinterpret_cast总结对比

    [本文链接] http://www.cnblogs.com/hellogiser/p/static_cast-dynamic_cast-const_cast-reinterpret_cast.html ...