spring boot实现文件的下载

本文介绍了一个简单的SpringBoot文件下载功能实现方法。通过设置HTTP响应头并读取指定路径的文件内容,可以实现在浏览器中直接下载文件。此方法适用于需要在Web应用中提供文件下载服务的场景。

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

创建好springboot后用以下代码,亲测有效,很简单

package com.example.file_download.Controller;

import java.io.*;
import java.net.URLEncoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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

@Controller
public class File_Download {

    @PostMapping(value = "/downloadFile")
    @CrossOrigin
    public Response downloadFile(@RequestBody Map<String, Object> param, HttpServletResponse response) throws UnsupportedEncodingException {
        String path = CyDataType.getString(param.get("attachmenturl"));
        //下载的文件名
        String fileName = CyDataType.getString(param.get("attachmentname"));
        // 如果文件名不为空,则进行下载
        if (fileName != null) {
            //设置文件路径
            String realPath = path;
            File file = new File(realPath, fileName);
            // 如果文件名存在,则进行下载
            if (file.exists()) {
                // 配置文件下载
                response.setHeader("content-type", "application/octet-stream");
                response.setContentType("application/octet-stream");
                // 下载文件能正常显示中文
                response.setHeader("Content-Disposition", "attachment;filename=" + 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 the song successfully!");
                } catch (Exception e) {
                    System.out.println("Download the song failed!");
                } finally {
                    if (bis != null) {
                        try {
                            bis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (fis != null) {
                        try {
                            fis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        return null;
    }

}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值