JSP implements the method of downloading files from different servers _jsp programming

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.