Saya menemukan fungsi mengunduh file di tempat kerja. Saya mengekstraknya sendiri. Kode sederhana. Saya berharap ini dapat membantu semua orang. Oke, jangan katakan banyak, tolong tambahkan kodenya!
public void downloadfile (file file, string downname, permintaan httpservletRequest, respons httpservletResponse) {outputStream out = null; FileInputStream fin = null; BufferedInputStream bin = null; coba {if (file.exists ()) {string finalFileName = null; String agent = request.getHeader ("User-agent"); boolean ismsie = (agen! = null && agent.indexof ("msie")! = -1); if (isMsie) {finalFileName = urlencoder.encode (downname, "utf8"); } else {finalFileName = string baru (downname.getbytes ("utf-8"), "iso-8859-1"); } response.setContentType ("Application/X-MSDownload"); response.setHeader ("Disposisi Konten", "Lampiran; FileName =". Concat (FinalFileName)); out = response.getoutputStream (); fin = new fileInputStream (file); bin = BufferedInputStream baru (FIN); untuk (int data = bin.read (); data> -1; data = bin.read ()) {out.write (data); }} else {}} catch (Exception e) {e.printstacktrace (); } akhirnya {coba {if (bin! = null) bin.close (); if (fin! = null) fin.close (); if (out! = null) out.close (); } catch (Exception e2) {e2.printstacktrace (); }}}Di atas adalah kode yang diunduh dalam artikel ini. Saya berharap ini akan membantu untuk pembelajaran semua orang, dan saya harap semua orang akan lebih mendukung wulin.com.