• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

springboot实现文件下载呢

武飞扬头像
qq_25073223
帮助3

转自:

springboot如何实现文件下载呢?

下文笔者讲述SpringBoot实现文件下载的方法分享,如下所示:

文件存储在服务器指定位置

import java.io.*;
import java.net.URLEncoder;
import java.util.*;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
 
import javax.servlet.http.HttpServletResponse;
@RestController
public class Controller {
 
    @GetMapping(value = "/downloadFile", consumes = MediaType.ALL_VALUE)
    void downloadFile(final HttpServletResponse response)
            throws Exception {
 
        // 获取文件
        File file = new File("D:\java265.txt");
        //文件名
        String fileName = file.getName();
 
        // 清空缓冲区,状态码和响应头(headers)
        response.reset();
        // 设置ContentType,响应内容为二进制数据流,编码为utf-8,此处设定的编码是文件内容的编码
        response.setContentType("application/octet-stream;charset=utf-8");
        // 以(Content-Disposition: attachment; filename="filename.jpg")格式设定默认文件名,设定utf编码,此处的编码是文件名的编码,使能正确显示中文文件名
        response.setHeader("Content-Disposition", "attachment;fileName="  fileName  ";filename*=utf-8''" URLEncoder.encode(fileName,"utf-8"));
 
        // 实现文件下载
        byte[] buffer = new byte[1024];
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        try {
            fis = new FileInputStream(file);
            bis = new BufferedInputStream(fis);
            // 获取字节流
            OutputStream os = response.getOutputStream();
            int i = bis.read(buffer);
            while (i != -1) {
                os.write(buffer, 0, i);
                i = bis.read(buffer);
            }
            System.out.println("Download successfully!");
        }
        catch (Exception e) {
            System.out.println("Download failed!");
        }
        finally {
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
学新通

创建文件数据并下载

import java.io.*;
import java.net.URLEncoder;
import java.util.*;
import java.util.List;
 
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
 
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
@RestController
public class Controller {
 
    @GetMapping(value = "/downloadExcel", consumes = MediaType.ALL_VALUE)
    void downloadExcel(final HttpServletResponse response)
            throws Exception {
        String name = "测试."   ExcelTypeEnum.XLSX;
        // 清空缓冲区,状态码和响应头(headers)
        response.reset();
        // 设置ContentType,响应内容为文本数据,编码为utf-8,此处设定的编码是文件内容的编码
        response.setContentType("text/plain;charset=utf-8");
        // 以(Content-Disposition: attachment; filename="filename.jpg")格式设定默认文件名,设定utf编码,此处的编码是文件名的编码,使能正确显示中文文件名
        response.setHeader("Content-Disposition", "attachment;fileName="   name   ";filename*=utf-8''"   URLEncoder.encode(name, "utf-8"));
 
        // 响应输出流
        OutputStream out = response.getOutputStream();
        // 建立excel
        ExcelWriter excelWriter = EasyExcel.write(out).build();
        // 建立sheet
        WriteSheet writeSheet = EasyExcel.writerSheet("sheet1").build();
        // 指定sheet并写数据
        excelWriter.write(getListString(), writeSheet);
        // 不要忘记
        excelWriter.finish();
        out.flush();
    }
 
    // 生成excel内容
    List<List<String>> getListString() {
        List<List<String>> result = new LinkedList<>();
        List<String> data1 = new LinkedList<>();
        data1.add("1");data1.add("maomao");data1.add("java265");
        result.add(data1);
        List<String> data2 = new LinkedList<>();
        data1.add("2");data1.add("maomao-2");data1.add("java265-3");
        result.add(data2);
        List<String> data3 = new LinkedList<>();
        data1.add("3");data1.add("maomao-3");data1.add("java265-4");
        result.add(data3);
        return result;
    }
}
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfhekcc
系列文章
更多 icon
同类精品
更多 icon
继续加载