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);
}
}
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-732382.html
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-732382.html
到了這里,關(guān)于Springboot 項(xiàng)目下載資源目錄下的 Word 文件的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!