struts-下载

简介:
一、创建项目
    项目名称:demodowload
二、添加struts jar包
    commons-fileupload-1.2.2.jar
    commons-io-2.0.1.jar
    commons-lang3-3.1.jar
    freemarker-2.3.19.jar
    javassist-3.11.0.GA.jar
    ognl-3.0.5.jar
    struts2-core-2.3.4.jar
    xwork-core-2.3.4.jar
三、在web.xml文件中配置核心过滤器
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
      <display-name>Struts2_13fileUpload</display-name>
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
      <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
      </filter>
      <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>
    </web-app>
四、在WebRoot下创建存储文件的目录
    /download
    在该目录下存放图片.jpeg
五、在项目中添加struts配置文件中添加核心配置文件
    1.在项目中创建conf目录
        /conf
    2.在conf目录下不回struts.xml文件
        <?xml version="1.0" encoding="UTF-8" ?>
        <!DOCTYPE struts PUBLIC
            "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
            "http://struts.apache.org/dtds/struts-2.3.dtd">
        <struts>
            <constant name="struts.enable.DynamicMethodInvocation" value="false" />
            <constant name="struts.devMode" value="true" />
            <constant name="struts.ui.theme" value="simple"/>    
        </struts>
六、创建Action
    1.在src下创建包
        包名:action
    2.在包下创建Action
        Action名称:DownloadAction.java
        Action内容:
        public class DownloadAction extends ActionSupport {
            private String fileName;
            /*
             * 把读取文件的流对象返回,Web服务器会读取流对象中的数据并封装到response中,然后发送到客户端
             */
            public InputStream getInputStream() throws FileNotFoundException, UnsupportedEncodingException{
                InputStream is = null;
                this.fileName="图片.jpeg";
                //把URL路径转化为服务器的本地路径
                String realPath = ServletActionContext.getServletContext().getRealPath("/download/"
                        +this.fileName);
                is = new FileInputStream(realPath);        
                return is;
            }
            public String doDownloadFile(){
                //此处可加入业务,如判断用户是否有下载权限
                return SUCCESS;
            }
            public String getFileName() throws UnsupportedEncodingException {
                //编码的目的是为了解决中文文件名出现乱码的问题
                return java.net.URLEncoder.encode(fileName, "UTF-8");//该编码方式在IE6下可能有该问题:文件名长度不能超过16个字符
                //return new String(this.fileName.getBytes("GBK"),"ISO-8859-1");//该编码方式可兼容IE6,但GBK不利于实现国际化
                
            }
            public void setFileName(String fileName) {
                this.fileName = fileName;
            }
        }
    3.在struts.xml文件中进行action配置
        <package name="default" namespace="/" extends="struts-default"> 
             <action name="doDownloadFile" class="action.DownloadAction" method="doDownloadFile">
                 <result name="success" type="stream">
                     <param name="inputName">inputStream</param><!--文件对应的流对象  -->
                     <param name="contentDisposition">attachment;filename="${fileName}"</param><!--发送给客户端的文件名,需要注意中文乱码问题  -->
                     <param name="contentType">application/octet-stream</param><!--文件类型,application/octet-stream是不限制类型  -->
                     <param name="bufferSize">1024</param><!--缓冲区大小  -->
                 </result>
             </action>
        </package>
七、页面准备
    页面名称:downloadFile.jsp
    页岩内容:
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@ taglib prefix="s" uri="/struts-tags"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
        <s:a action="doDownloadFile">下载文件</s:a>
    </body>
    </html>

本文转自  素颜猪  51CTO博客,原文链接:http://blog.51cto.com/suyanzhu/1559526

    

相关文章
|
前端开发 数据库
struts2实现的文件上传下载案例(二)、FileUpLoad文件的下载
struts2实现的文件上传下载案例(二)、FileUpLoad文件的下载
78 0
|
Java 开发者
Struts2的文件下载 | 学习笔记
快速学习 Struts2的文件下载,介绍了 Struts2的文件下载系统机制, 以及在实际应用过程中如何使用。
73 0
|
Java API
Struts2的下载和安装
Struts2的下载   使用Struts2框架,进行Web开发,或者运行Struts2的程序,必须,先下载并安装好Struts2   从Struts2的官网中进行下载   http://struts.
1435 0
|
XML Java 数据格式
|
Java 容器 开发框架
|
Web App开发 Java
Struts2文件的下载
1、下载登录页面download.jsp 1: 3: 4: 5: 6: 7: 8: 下载页面 9: 10: 11: 下载前的登录页面 12: 13: 14: ...
703 0
|
Java Apache 数据格式
struts2 下载
struts.xml &lt;?xml version="1.0" encoding="UTF-8" ?&gt; &lt;!DOCTYPE struts PUBLIC     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"     "http://struts.apache.org/
1117 0
|
Java 前端开发
struts2 文件下载
文件下载是一个很常见的功能,用struts2实现文件下载的步骤: 一)定义一个Action类,FileDownload.java [java] view plaincopy package com.struts2.filedownload;      import java.io.InputStream;  
1213 0