转自: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. sql批量获取wordpress所有留言者的邮件地址

    如果你的wordpress博客有很多读者互动的话,他们的留言都会留下具体的联系邮箱,我们如何批量导出这些联系信息呢?可以试试下面的sql语句 SELECT DISTINCT comment_autho ...

  2. bc.34.B.Building Blocks(贪心)

    Building Blocks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  3. cocos2d-x 在android环境下开发遇到的一些bug

    今天在弄一个关于android环境下解析xml的东东,遇到了2个比较麻烦问题 1.android的apk下文件是压缩文件,io.open模式无法读取到数据的, 解决思路就是: CCFileUtils: ...

  4. unity3d 截屏

    原地址:http://www.cnblogs.com/88999660/archive/2013/01/21/2869747.html void OnGUI(){ if(GUI.Button(new ...

  5. Tooltip jqueryui

    (文章是从我的个人主页上粘贴过来的,大家也可以访问我的主页 www.iwangzheng.com) http://jqueryui.com/tooltip/ <meta charset=&quo ...

  6. 一个很不错的适合PHPER们书单,推荐给大家【转】

    来我博客的访客们中,有一些是PHP的初学者,是不是很迷茫PHP应该怎么学?应该买什么样的书?到处问人,到处求助?这下好了. 正好看到黑夜路人在博客上推荐了一个书单,看上去都非常不错,很多我也没有读过, ...

  7. 用JAVA代码实现验证邮箱地址是否符合

    public class Test{ public static void main(String[] args){ Test t = new Test(); String email = " ...

  8. Trailing Zeros

    Write an algorithm which computes the number of trailing zeros in n factorial. Have you met this que ...

  9. Linux运维

    概要:http://os.51cto.com/art/201312/423616.htm 论坛: http://www.linux360.cn/ https://www.centos.bz/ http ...

  10. iterator与const_iterator及const iterator区别

    如果你传递过来一个const类型的容器,那么只能用const_iterator来遍历.  C++ Code  12345   void Method(const vector<int> v ...