我有一个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中获取它。
我错过了什么吗?
答案 0 :(得分:3)
“Content-Disposition:”末尾有一个冒号。没有它,应该拿起文件名。
答案 1 :(得分:0)
也许这有助于你
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);