我的问题是我有一个程序可以制作文件并将其流式传输给用户我有两个问题:
1)首先如何更改文件名和类型(x.sql)
2)如果在制作文件时出现异常,我想将用户重定向到错误页面,因为当我们想写入servlet中的输出流时,我的方法的返回类型是无效的!
我的程序是这样的:
@RequestMapping(value = "/test/{jobid}")
public void dumpData(@PathVariable("jobid") long jobid, HttpServletResponse response) {
try {
response.setContentType("application/octet-stream");
ServletOutputStream out = response.getOutputStream();
String downloadOutput = "";
// AM I ABLE TO SET FILENAME SO THE USER DOWNLOAD THE FILE WITH THAT NAME ?
// DOES SETHEADER HELP ME IN THIS CASE ?
... (making downloadOutput String by values coming from somewhere)
out.write(downloadOutput.getBytes("UTF-8"));
out.flush();
out.close();
} catch(SomeException e){
//WHEN THE RETURN TYPE IS VOID HOW TO REDIRECT USER TO ERROR PAGE IN CASE OF SOME PROBLEM ?
//(INFO:)IF WE DEFINE STRING AS RETURN TYPE THE PROGRAM WILL GET EXCEPTION
}