Tapestry 5.4 文件下载

本文介绍了一个简单的Java文件下载实现方案,通过自定义类`FileDownload`来处理文件的下载请求,包括文件流的读取与返回,适用于Web应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.实现下载类

package top.zbeboy.rs.pages;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.tapestry5.StreamResponse;
import org.apache.tapestry5.services.Response;

/**
 * Created by zbeboy on 2016/11/15.
 */
public class FileDownload implements StreamResponse{
    /**
     *  下载文件路径
     */
    private String filePath = null;

    /**
     *  下载文件主题
     */
    private String fileName = null;

    /**
     *  构造方法
     *
     * @param fileName 生成文件名主题
     * @oaran filePath 下载文件路径
     */
    public FileDownload(String fileName, String filePath) {

        this.fileName = fileName;
        this.filePath = filePath;
    }

    /**
     * 无参构造
     */
    public FileDownload() {

    }

    /**
     *  获取返回流
     */
    public InputStream getStream() throws IOException {

        // 待下载文件
        File downFile = new File(filePath,fileName);

        ByteArrayOutputStream baos = null;
        if (filePath != null && downFile.exists()) {

            // 生成返回的文件流
            FileInputStream fis = new FileInputStream(downFile);

            BufferedInputStream bis = new BufferedInputStream(fis);

            baos = new ByteArrayOutputStream();

            byte[] buf = new byte[1024];

            // 读取bis流
            int byt = bis.read(buf, 0, 1024);

            while(byt != -1){
                baos.write(buf, 0, byt);
                byt = bis.read(buf, 0, 1024);
            }

            bis.close();
        }

        return new ByteArrayInputStream(baos != null ? baos.toByteArray() : new byte[0]);
    }

    /**
     *  设置返回流的 ContentType
     */
    public String getContentType() {

        return "application/octet-stream";
    }

    /**
     * 返回流相关设置
     */
    public void prepareResponse(Response response) {
        try {
            response.setHeader("content-disposition", "attachment; filename="
                    + new String((fileName).getBytes(), "ISO-8859-1"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * @return the fileName
     */
    public String getFilePath() {
        return filePath;
    }

    /**
     * @param fileName the fileName to set
     */
    public void setFilePath(String fileName) {
        this.filePath = fileName;
    }

    /**
     * @return the fileName
     */
    public String getFileName() {
        return fileName;
    }

    /**
     * @param fileName the fileName to set
     */
    public void setFileName(String fileName) {
        this.fileName = fileName;
    }
}

2.在下载java文件类中添加如下属性

  @InjectPage
  private FileDownload fileDownload;

 Object onActionFromDownload(String fileName)
  {
    fileDownload.setFileName(fileName);
    fileDownload.setFilePath("file/location/");
    return fileDownload;
  }

3.在tml中

<t:loop source="files" value="filesBean">
            <li class="list-group-item">
                <div class="row">
                    <div class="col-md-6">
                        <t:actionlink t:id="download" context="${filesBean.fileName}">${filesBean.fileName}</t:actionlink>
                    </div>
                    <div class="col-md-6 text-right">
                        ${filesBean.modifyTime}
                    </div>
                </div>
            </li>
        </t:loop>


评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值