This commit is contained in:
tanghc
2019-09-02 19:37:22 +08:00
parent 0cafb5829b
commit e47cd0e002
8 changed files with 197 additions and 24 deletions

View File

@@ -2,15 +2,15 @@ package com.gitee.sop.storyweb.controller;
import com.gitee.sop.servercommon.annotation.ApiMapping;
import com.gitee.sop.storyweb.controller.param.StoryParam;
import org.apache.commons.io.FileUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.IOUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import java.io.File;
import java.io.IOException;
/**
@@ -18,20 +18,24 @@ import java.io.IOException;
*
* @author tanghc
*/
@Api(tags = "文件下载")
@Controller
public class DownloadController {
@ApiMapping(value = "story.download")
public ResponseEntity<byte[]> export(StoryParam param) throws IOException {
@ApiOperation(value = "文件下载", notes = "演示文件下载")
@ApiMapping(value = "story.download", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE/* 这个一定要加,不然沙箱文档不起作用 */)
public ResponseEntity<byte[]> download(StoryParam param) throws IOException {
HttpHeaders headers = new HttpHeaders();
// 假设下载classpath下的application.properties文件
ClassPathResource resource = new ClassPathResource("/application.properties");
File file = resource.getFile();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentDispositionFormData("attachment", file.getName());
headers.setContentDispositionFormData("attachment", resource.getFilename());
return new ResponseEntity<>(FileUtils.readFileToByteArray(file), headers, HttpStatus.OK);
return ResponseEntity
.ok()
.headers(headers)
.body(IOUtils.toByteArray(resource.getInputStream()));
}
}