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);
}
}
文章来源地址https://www.toymoban.com/news/detail-732382.html
文章来源:https://www.toymoban.com/news/detail-732382.html
到了这里,关于Springboot 项目下载资源目录下的 Word 文件的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!