spring boot 返回文件流

import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource; 
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ContentDisposition;


@RequestMapping("/download")
public ResponseEntity<Resource> download(HttpServletRequest request, @RequestBody QueryBHDownLoadFileVo param) {
    String path = "D:\\VipSoft.xlsx";
    String contentDisposition = ContentDisposition
            .builder("attachment")
            .filename(path)
            .build().toString();

    File file = new File(path);
    if (file.exists()) {
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, contentDisposition)
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .body(new FileSystemResource(path));
    } else {
        return ResponseEntity.badRequest()
                .header(HttpHeaders.CONTENT_DISPOSITION, contentDisposition + " Not Found")
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .body(null);
    }

}

  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值