飞道的博客

springboot如何实现文件下载呢?

328人阅读  评论(0)

转自:

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;
    }
}
 

转载:https://blog.csdn.net/qq_25073223/article/details/127917013
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场