java将文件转成流文件返回给前端

环境:jdk1.8,springboot2.5.3,项目端口号:9100

1.待转换的文件

一、路径

二、文件内容

2.controller中代码

java 复制代码
package com.example.pdf.controller;

import com.example.pdf.service.GetFileStreamService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 
 * @date 2024/3/29 16:28
 * @describe
 */
@RestController
@RequestMapping(value = "test")
public class GetFileStreamController {

    @Resource
    private GetFileStreamService getFileStreamService;
	
	/**
     * 获取文件流
     */
    @GetMapping("getFileStream")
    public void getFileStream(HttpServletResponse response) {
        getFileStreamService.getFileStream(response);

    }

}

3.service中代码

java 复制代码
package com.example.pdf.service;

import javax.servlet.http.HttpServletResponse;

/**
 * @author 
 * @date 2024/3/29 16:30
 * @describe
 */
public interface GetFileStreamService {

    /**
     * 获取文件流
     * @param response
     */
    void getFileStream(HttpServletResponse response);
}

4.实现类代码

java 复制代码
package com.example.pdf.service.impl;

import com.example.pdf.service.GetFileStreamService;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;

/**
 * @author
 * @date 2024/3/29 16:31
 * @describe
 */
@Service
public class GetFileStreamServiceImpl implements GetFileStreamService {
    /**
     * 获取文件流
     */
    @Override
    public void getFileStream(HttpServletResponse response) {
        // 指定文件路径,获取file文件
        File file = new File("E:\\Desktop\\temps\\test.pdf");
        try {
            // 将文件转为文件输入流
            FileInputStream fileInputStream = new FileInputStream(file);
            // 获取响应的输出流
            OutputStream outputStream = response.getOutputStream();
            // 将文件转成字节数组,再将数组写入响应的输出流
            byte[] buffer = new byte[1024];
            int bytesRead = -1;
            while ((bytesRead = fileInputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
            // 刷新输出流
            outputStream.flush();
            // 关闭流
            fileInputStream.close();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

5.postman中访问结果示例

6.浏览器中访问结果示例

相关推荐
花阴偷移9 分钟前
kotlin语法(上)
android·java·开发语言·kotlin
XuanRanDev12 分钟前
【编程语言】Kotlin快速入门 - 泛型
开发语言·kotlin
普通网友13 分钟前
Android kotlin Jetpack mvvm 项目
android·开发语言·kotlin
木易 士心14 分钟前
Go、Rust、Kotlin、Python 与 Java 从性能到生态,全面解读五大主流编程语言
java·golang·rust
Crogin15 分钟前
快速简单入门Kotlin——基础语法(第一天)
android·开发语言·kotlin
qq_3363139317 分钟前
java基础-set系列集合
java·开发语言·python
U***l83224 分钟前
【Spring】IDEA中创建Spring项目
java·spring·intellij-idea
好好沉淀25 分钟前
IDEA 报错:You aren‘t using a compiler supported by lombok (1分钟解决方案)
java·spring·intellij-idea
lzhdim26 分钟前
C#开发的应用启动菜单应用(普通版) - 开源研究系列文章 - 个人小作品
开发语言·c#
Kyln.Wu34 分钟前
【python实用小脚本-309】HR×Python改造面试路线规划|从手工排程到智能调度的化学反应,轻松实现路径优化
开发语言·python·面试