spring下文件下载

该博客介绍了在SpringBoot环境下如何实现单个或多个文件的传统下载方式,以及利用SpringBoot创建ZIP压缩文件并提供下载的方法,特别提到了下载完成后会删除服务器上的临时ZIP文件。
摘要由CSDN通过智能技术生成

单/多文件下载(这个是传统上的下载,是基础用于理解更高层的知识)

package com.poi.testpoi.controller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class FileDown {

@RequestMapping("/singleFileDownload")
@ResponseBody
public String singleFileDownload(HttpServletResponse response) throws FileNotFoundException {
    // 单个要下载的文件
    File inputFile = new File("C:\\Users\\zy962\\Desktop\\tip说明.txt");
    String fileName = "test.txt";
    String absolute = "C:\\Users\\zy962\\Desktop\\";
    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;
    
    try {
        bis = new BufferedInputStream(new FileInputStream(inputFile));
        bos = new BufferedOutputStream(new FileOutputStream(absolute+fileName));
        
        byte[] buffer = new byte[1024];
        int length = 0;
        while ((length = bis.read(buffer)) != -1) {
            bos.write(buffer, 0, length);
        }
        bos.flush();
        bos.close();
        bis.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return "下载完成,请去相应目录查看";
}

@RequestMapping("/mutliFileDownload")
@ResponseBody
public String mutliFileDownload(HttpServletResponse response) {
    File 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值