Save Java as a downloaded file and rename the file

Source: Internet
Author: User
Public void filedown () throws exception {string filename = inputpath. replace (inputpath. substring (0, inputpath. lastindexof ("/", inputpath. length (), "/data/backup"); // the original file path string filepath = inputname. substring (inputname. lastindexof ("/") + 1, inputname. length (); // name of the file saved as // string filename = inputpath. substring (inputpath. lastindexof ("/") + 1, inputpath. length (); file = new file (inputpath); servl Etactioncontext. getresponse (). setheader ("content-disposition", "attachment; filename =" + new string (filepath. getbytes ("gb2312"), "iso-8859-1"); // outputstream = servletactioncontext. getresponse (). getoutputstream (); // byte [] mybyte = new byte [1024]; // inputstream input = new fileinputstream (File); // int temp =-1; // while (temp = input. read (mybyte ))! =-1) {// outputstream. write (mybyte, 0, temp); //} // outputstream. flush (); // new file input/output stream outputstream output = NULL; fileinputstream FD = NULL; try {// new file object file F = new file (filename ); // create a file input/output stream object output = servletactioncontext. getresponse (). getoutputstream (); FCM = new fileinputstream (f); // you can specify the cache size for each write. byte [] B = new byte [(INT) F. length ()]; // out. print (F. length (); // write the output stream to the client int I = 0; while (I = f Is. read (B)> 0) {output. write (B, 0, I); system. out. println ("print" + I);} output. flush ();} catch (exception e) {e. printstacktrace ();} finally {If (FS! = NULL) {fiis. Close (); fiis = NULL;} If (output! = NULL) {output. Close (); Output = NULL ;}} return ;}

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.