直接上代码
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
@Controller
@RequestMapping("/evaluate")
public class EvaluateController {
private static String templateFilePath = "D:\\temp\\templateFile.xlsx";
private static String templateFileName = "批量导出模板.xlsx";
@GetMapping("downloadPathExportTemplate")
@ResponseBody
public ResponseEntity<Object> downloadFile() throws FileNotFoundException, UnsupportedEncodingException {
File file = new File( templateFilePath);
InputStreamResource resource = new InputStreamResource ( new FileInputStream( file ) );
HttpHeaders headers = new HttpHeaders();
headers.add ( "Content-Disposition",String.format("attachment;filename=\"%s",new String(templateFileName.getBytes("GBK"), "ISO-8859-1")));
headers.add ( "Cache-Control","no-cache,no-store,must-revalidate" );
headers.add ( "Pragma","no-cache" );
headers.add ( "Expires","0" );
ResponseEntity<Object> responseEntity = ResponseEntity.ok()
.headers ( headers )
.contentLength ( file.length ())
.contentType(MediaType.parseMediaType ( "application/txt" ))
.body(resource);
return responseEntity;
}
}
浏览器输入: http://localhost:8080/evaluate/downloadPathExportTemplate
内容无变化
restEasy 文件下载如下:
https://blog.csdn.net/zzhongcy/article/details/19966965
restEasy 文件上传如下: https://blog.csdn.net/zhangzz1127/article/details/17428173