实例如下所示
Controlador : @RequestMapping (value = "/{empréstimo}/{atmttype}") public void Doget (@PathVariable ("empréstimo") String empréstimo,@PathVariable ("atmttype") string atmttype, httpsletrequest req, httretrStletrStRensPonsPonsensonsensensensensew), httyServleTrequest req, httretrStrefSonsPonsensPonsensons) Hashmap <string, string> (); map.put ("empréstimo", empréstimo); Mapa <string, string> urlbyloanid = new hashmap <string, string> (); // mapa <string, string> urlbyloanid = zcmqueryinfoservice.queryurlbyloanid (mapa); tente {if (urlbyloanid! = null) {string wjurl = "http://10.0.15.11:8080/gateway//nfs/marvel-core-admin/2017/10/11/compact_seal_11111119371231615_7131313131313131313131313151515. // string wjurl = urlbyloanid.get ("url"); int i = wjurl.lastindexof ("/"); String filename = wjurl.substring (i+1); Url url = novo url (wjurl); HttpurlConnection Conn = (httpurlConnection) url.openconnection (); // 设置超时间为 3 秒 Conn.SetConnectTimeout (3*1000); // 防止屏蔽程序抓取而返回 403 错误 Conn.SetRequestProperty ("User-Agent", "Mozilla/4.0 (Compatível; MSIE 5.0; Windows nt; DigExt)"); // 得到输入流 inputStream inputStream = conn.getInputStream (); // 获取自己数组 byte [] bs = readInputStream (inputStream); Response.setContentType ("Application/Octet-Stream; Charset = ISO8859-1"); Saída bufferedutputStream = null; BufferInputStream input = null; tente {output = new BufferoutOutputStream (Response.getOutputStream ()); // 中文文件名必须转码为 ISO8859-1, 否则为乱码 String filenOdown = new String (FILENAME.GETBYTES (), "ISO8859-1"); // 作为附件下载 Response.setheader ("Content-Disposition", "APRENCIAMENTO; FILENAME =" + FILENODOWN); output.Write (BS); Response.flushBuffer (); } catch (Exceção e) {log.error ("Baixar erro de arquivo de log", e); } // 用户可能取消了下载 finalmente {if (input! = Null) tente {input.close (); } catch (ioexception e) {e.printStackTrace (); } if (output! = null) tente {output.close (); } catch (ioexception e) {e.printStackTrace (); }}}} catch (Exceção e) {e.printStackTrace (); }}/ ** * 从输入流中获取字节数组 * @param inputStream * @return * @throws ioexception */ public static byte [] readinputStream (inputStream inputStream) lança ioexception {byte [] buffer = new Byte [1024]; int len = 0; ByteArrayOutputStream bos = new ByteArrayOutputStream (); while ((len = inputStream.read (buffer))! = -1) {bos.write (buffer, 0, len); } bos.close (); return bos.tobytearray (); }以上这篇 Java 通过 url 下载文件并输出的方法就是小编分享给大家的全部内容了 , 希望能给大家一个参考 , 也希望大家多多支持武林网。