Java File Download principle + Struts2 File Download principle detailed: ~~myself__java

Source: Internet
Author: User
# # #struts2 complete file Download:1. First clear the basic knowledge: InputStream and FileInputStream, etc. are for memory, then the file input stream must be to the memory input stream, which is useful for file downloading. Essential Understanding Download File process: The first step: is to download the file to put or call the link to the input stream: The file to the memory of the new FileInputStream ("D:\\test.txt") The second step: the input flow into the output stream, that is, data from the memory dry to the file, This completes the file download function. Ye's IP grab download Excel principle is to use this kind of output flow way: InputStream in=new FileInputStream (temp); OutStream (in); Temp.delete (); The third step: cooperate with the following control to the browser: GetResponse (). setContentType ("application/vnd.ms-excel; CHARSET=GBK "); String filename=buildtitle () + ". xls"; GetResponse (). SetHeader ("Content-disposition", "attachment; filename=" +new String (Filename.getbytes ("GB2312"), " Iso-8859-1 "));
This is the whole principle of file downloading.
2.struts2 Download files: The framework is really simple--> still use XML configuration, instead of cumbersome hard coding. First step: Add one to the action:
Public InputStream getInputStream () throws Exception {
return new FileInputStream ("D:\\test.txt");
Step Two: Add configuration in Struts.xml: <action name= "Topexporttxt" class= "Cn.cstnet.trafficView.action.flow.CatchIPTopAction" > <result name= "Success" type= "Stream" > <param name= "ContentType" >text/plai                             n</param> <param name= "InputName" >inputStream</param>                             <param name= "contentdisposition" >attachment;filename= "Struts2down.txt" </param> <param name= "buffersize" >4096</param> </result> </action>
The above two steps are completely buttoned up, very sharp, when the specific changes to meet business needs can be.

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.