spring boot 返回文件流

所需依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

完整的Controller代码

import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

@Controller
public class FileDownloadController {

    @GetMapping("/download")
    @ResponseBody
    public ResponseEntity<InputStreamResource> downloadFile() throws IOException {
        File file = new File("实际的文件路径");
        InputStream inputStream = new FileInputStream(file);
        InputStreamResource resource = new InputStreamResource(inputStream);

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=实际的文件名");

        return ResponseEntity.ok()
                .headers(headers)
                .contentLength(file.length())
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .body(resource);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将Spring Boot文档的文件转换为docx文件,你可以使用Apache POI库的XWPFDocument类。下面是一个示例代码: ```java import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.springframework.core.io.InputStreamResource; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.IOException; @RestController public class DocxController { @GetMapping(value = "/docx", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE) public ResponseEntity<InputStreamResource> convertToDocx() throws IOException { // 读取Spring Boot文档的文件 FileInputStream inputStream = new FileInputStream("path/to/spring-boot-doc.txt"); // 将文本内容转换为docx格式 XWPFDocument document = new XWPFDocument(); document.createParagraph().createRun().setText(inputStreamToString(inputStream)); // 创建输出 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); // 写入docx内容到输出 document.write(outputStream); outputStream.close(); // 设置响应头 HttpHeaders headers = new HttpHeaders(); headers.add("Content-Disposition", "attachment; filename=spring-boot-doc.docx"); // 返回文件 return ResponseEntity .ok() .headers(headers) .contentType(MediaType.APPLICATION_OCTET_STREAM) .body(new InputStreamResource(new ByteArrayInputStream(outputStream.toByteArray()))); } private String inputStreamToString(FileInputStream inputStream) throws IOException { StringBuilder stringBuilder = new StringBuilder(); int ch; while ((ch = inputStream.read()) != -1) { stringBuilder.append((char) ch); } return stringBuilder.toString(); } } ``` 在上面的示例代码中,我们使用`FileInputStream`从Spring Boot文档中读取文件,并将其转换为字符串。然后,我们使用`XWPFDocument`创建一个新的docx文档,并将文本内容添加到文档中。最后,我们将docx文档的内容写入到输出`ByteArrayOutputStream`中,并将其作为文件返回给客户端。 请将代码中的`path/to/spring-boot-doc.txt`替换为实际的Spring Boot文档路径。你可以通过访问`/docx`路径来获取docx文件,并将其保存为文件或进行其他操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值