When Java implements file download, the file storage window pops up.

Source: Internet
Author: User
response.setContentType("application/octet-stream");            response.setContentType("application/OCTET-STREAM;charset=UTF-8");            response.setHeader("Content-Disposition", "attachment;filename=" + fileName1);File file = new File(filePath + fileName);try {    fis = new FileInputStream(file);    out = new BufferedOutputStream(response.getOutputStream());    byte[] buffer = new byte[1024];    int len;    while ((len = fis.read(buffer)) != -1) {         out.write(buffer, 0, len);         out.flush();    }}catch.......

 

 

 

An online open or Download Method

Public String executecommandudownload (actioncontext context) throws exception {httpservletresponse response = context. getresponse (); response. setcharacterencoding ("UTF-8"); Boolean isonline = false; string filename = context. getrequest (). getparameter ("FILENAME"); connection con = NULL; httpservletrequest request = context. getrequest (); string filepath = NULL; bufferedinputstream buffer = NULL; outputstream ou T = NULL; try {con = This. getconnection (context); If ("". equals (filename) | filename = NULL) {fileinfobean bean = new fileinfobean (); filename = bean. findname (con, ID);} file F = new file (filepath); // check whether the file exists if (! F. exists () {response. senderror (404, "file not found! "); Return" file not found! ";} Buffer = new bufferedinputstream (New fileinputstream (f); byte [] Buf = new byte [1024]; int Len = 0; response. reset (); // very important if (isonline) {// online open mode URL u = new URL ("file: //" + filepath); response. setcontenttype (U. openconnection (). getcontenttype (); response. setheader ("content-disposition", "inline; filename =" + (F. getname ()). getbytes ("GBK"); // the file name should be encoded into a UTF-8} else {// pure download method response. setcontenttype ("application/X-msdownload"); response. setheader ("content-disposition", "attachment; filename =" + java.net. urlencoder. encode (F. getname (), "UTF-8");} Out = response. getoutputstream (); While (LEN = buffer. read (BUF)> 0) out. write (BUF, 0, Len);} catch (throwable e) {e. printstacktrace ();} finally {try {buffer. close (); out. close ();} catch (throwable e) {e. printstacktrace () ;}} 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.