[英]Spring REST file download from server
我有一个Spring REST应用程序,端点之一是下载链接,下载的文件是在运行时生成的。
除文件名错误外,其他所有方法均有效。
这是控制器的相关部分:
@RestController
@RequestMapping("/export")
public class ExportREST {
@RequestMapping(method=RequestMethod.GET)
public void export(HttpServletResponse response) throws Exception {
//stuff omitted...
writeCsvResponse(response);
}
private void writeCsvResponse(HttpServletResponse response) throws IOException {
String fileName = "db.export."+dateFormat.format(new Date());
response.setContentType( "application/octet-stream" );
response.setHeader( "Content-Disposition:", "attachment;filename=" + "\"" + fileName + "\"" );
//write stuff to response...
response.setContentLength(totalLength);
response.setBufferSize(1024);
response.flushBuffer();
pout.close();
}
}
因此,我想要的是带有生成时间戳记的文件名,但实际上文件名始终是export
,大概是从URL获取文件名。
我错过了什么吗?
“ Content-Disposition:”的末尾有一个冒号。 没有它,文件名应该被选择。
也许这对你有帮助
if (mimeType == null) {
// set to binary type if MIME mapping not found
mimeType = "application/octet-stream";
}
System.out.println("MIME type: " + mimeType);
// set content attributes for the response
response.setContentType(mimeType);
有关servlet的更多详细信息, 请参见: https : //stackoverflow.com/questions/41914092/how-change-servlet-which-download-single-file-but-can-folderfew-files-in-fold
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.