The examples in this article describe the way JSP implements downloading files from different servers. Share to everyone for your reference, specific as follows:
Recently encountered a template download problem in the project: when the server for Tomcat, the file can be downloaded normally, but when the offline (WebLogic server) downloaded template is empty, the solution is recorded.
public void Importsuccess () throws Exception {try {httpservletresponse response = Struts2utils.getresponse ();
Reset response Response.reset ();
Set the response head to binary stream 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 key to the problem, WebLogic server to add a "/" to read. 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 ("Download export template failed", e);
}
}
Save it for later.
I hope this article will help you with JSP program design.