[英]How can I get download location browser in spring mvc controller?
The problem is next necessary to make file on server and after sending to client and print. 接下来的问题是在服务器上制作文件以及将其发送到客户端并打印后,接下来是必须的。
@RequestMapping(value = "/some", method = RequestMethod.GET)
public void getFileForPrint(HttpServletResponse response,
@RequestParam(value = "id", required = true) Long id,
@RequestParam(value = "print", defaultValue = "false") Boolean print) throws Exception {
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + fname);
ServletOutputStream out = response.getOutputStream();
somefile.write(out);
Desktop.getDesktop.print(new File("download location browser"));}
}
If you mean by when you are hitting an end point you want to download a file immediately then the following code should help you. 如果您指的是到达终点时想要立即下载文件,则以下代码应为您提供帮助。
@RequestMapping(value = "/somePath", method = RequestMethod.GET)
@ResponseBody
public void getFileForPrint(HttpServletResponse response,
@RequestParam(value = "id", required = true) Long id,
@RequestParam(value = "print", defaultValue = "false") Boolean print) throws Exception {
String filename="nameOfFileWhenDownloaded.txt";
try {
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
OutputStreamWriter osw = new OutputStreamWriter(response.getOutputStream());
osw.write(contentsOfTheFile);
osw.flush();
osw.close();
response.flushBuffer();
} catch (Exception e) {
e.printStackTrace();
throw new Exception("Error is sending file");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.