Springboot 项目下载资源目录下的 Word 文件

java 复制代码
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.InputStreamResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class FileController {

    private final ResourceLoader resourceLoader;

    @Autowired
    public FileController(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    @GetMapping("/downloadWord")
    public ResponseEntity<InputStreamResource> downloadWord() throws IOException {
        Resource resource = resourceLoader.getResource("classpath:templates/word.docx");
        InputStream inputStream = resource.getInputStream();
        InputStreamResource inputStreamResource = new InputStreamResource(inputStream);

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=word.docx");

        return ResponseEntity.ok()
                .headers(headers)
                .contentType(MediaType.parseMediaType("application/msword"))
                .body(inputStreamResource);
    }
}
相关推荐
wtsolutions6 小时前
比Word邮件合并功能更强大的Sheet-to-Doc优势功能
word·批量生成·邮件合并·wtsolutions·sheet-to-doc
Nonoas6 小时前
动态代理:发布订阅的高级玩法
java·ide·intellij-idea
程序员-周李斌7 小时前
Java 死锁
java·开发语言·后端
皮皮林5517 小时前
Prometheus+Grafana,打造强大的监控与可视化平台
java
JasmineWr7 小时前
CompletableFuture相关问题
java·开发语言
零雲8 小时前
java面试:知道java的反射机制吗
java·开发语言·面试
weixin_425023008 小时前
Spring Boot + MyBatis Plus JOIN 分页多表查询项目文档
spring boot·后端·mybatis
java1234_小锋8 小时前
Java进程占用的内存有哪些部分?
java
sxlishaobin8 小时前
Spring Bean生命周期详解
java·后端·spring
曹牧9 小时前
Java:Assert.isTrue()
java·前端·数据库