The Method of Downloading Files from Different Servers by JSP
- 2021-09-11 20:55:58
- OfStack
In this paper, an example is given to describe the method of downloading files from different servers by JSP. Share it for your reference, as follows:
Recently encountered in the project template download problem: When the server for Tomcat files can be downloaded normally, but when the online (WebLogic server) download template is empty, now record the solution.
public void importSuccess() throws Exception {
try {
HttpServletResponse response = Struts2Utils.getResponse();
// Reset response
response.reset();
// Set the response header to 2 Binary flow
response.setContentType("application/x-msdownload;charset=utf-8");
response.setContentType("APPLICATION/OCTET-STREAM ");
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition",
"attachment; filename=ComplaintsImportModel.xls");
String path = this.getClass().getClassLoader().getResource("/")// This is the crux of the problem. WebLogic The server wants to read and add 1 A "/"
.getPath();
path = path.substring(1, path.length());
String name = File.separator + path + "ComplaintsImportModel.xls";
logger.info("**********************************" + name
+ "*******************************");
InputStream is = new FileInputStream(name);
HSSFWorkbook wb = new HSSFWorkbook(is);
// Get File stream
OutputStream out = response.getOutputStream();
wb.write(out);
is.close();
out.flush();
out.close();
} catch (Exception e) {
logger.error(" Failed to download export template ", e);
}
}
Keep it for later use
I hope this article is helpful to everyone's jsp programming.