zoukankan      html  css  js  c++  java
  • struts 文件下载

    =============================struts 文件下载  ==================================

    步骤一: JSP页面
    <a href="download.action?fileName=IMG_0443.JPG">点击此处下载图片</a>

    步骤二: Action页面

    package org.zm.action;

    import java.io.BufferedInputStream;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.InputStream;

    import org.apache.struts2.ServletActionContext;

    import com.opensymphony.xwork2.ActionSupport;

    public class DownLoadAction extends ActionSupport{
    //读取下载文件的目录
    private String inputPath;
    //下载文件的文件名
    private String fileName;
    //读取下载文件的输入流
    private InputStream inputStream;

    //下载文件的类型
    private String conetntType;

    //创建InputStream输入流
    public InputStream getInputStream() throws FileNotFoundException{
    String path=ServletActionContext.getServletContext().
    getRealPath(inputPath);
    return new BufferedInputStream(new FileInputStream(path+"\"+
    fileName));
    }

    @Override
    public String execute() {
    return SUCCESS;
    }
    public void setInputStream(InputStream inputStream) {
    this.inputStream = inputStream;
    }
    public String getFileName() {
    return fileName;
    }
    public void setFileName(String fileName) {
    this.fileName = fileName;
    }

    public String getConetntType() {
    return conetntType;
    }


    public String getInputPath() {
    return inputPath;
    }


    public void setInputPath(String inputPath) {
    this.inputPath = inputPath;
    }
    public void setConetntType(String conetntType) {
    this.conetntType = conetntType;
    }
    }

    步骤三: Struts.xml文件
    <action name="download" class="org.zm.action.DownLoadAction">
    <param name="inputPath">/upload</param>
    <result name="success" type="stream">
    <param name="contentType">image/pjpeg</param>
    <param name="inputName">inputStream</param>
    <param name="contentDisposition">attachment;filename="${fileName}"</param>
    <param name="bufferSize">4096</param>
    </result>
    </action>


    提示: Stream结果类型
    contentType: 设置发送到浏览器的MIME类型
    contentLength: 文件大小
    contentDisposition: 设置响应的HTTP头信息的Content-Disposition参数的值
    inputName: 指定Action提供的inputStream类型的属性名称
    bufferSize:设置读取和下载文件时缓冲区的大小

    struts.xml文件和Action 这两处位置的变量名称一定要正确。

  • 相关阅读:
    About_Web
    神奇的 SQL 之性能优化 → 让 SQL 飞起来
    Java实现Kafka生产者和消费者的示例
    Android屏幕绘制一问到底(无代码)
    关于数据库事务和锁的必会知识点,你掌握了多少?
    【Azure Cloud Services】云服务频繁发生服务器崩溃的排查方案
    Choreographer全解析
    气之争,聊聊算法岗位的门户之见!
    资深首席架构师预测:2021年云计算的8个首要趋势
    【并发编程】- 内存模型(针对JSR-133内存模型)篇
  • 原文地址:https://www.cnblogs.com/Theladyflower/p/4624228.html
Copyright © 2011-2022 走看看