:
Controller : @RequestMapping (value = "/ {prêteId} / {atttype}") public void doget (@pathvariable ("wANDID") String LoanID, @ pathvariable ("atttype") String atttype, httpservletRequest req, httpserSerRespespesponse réponse) {map <string, string> new, httpserReRespèse Répondre) {map <string, string> new map Hashmap <string, string> (); map.put ("prêté", prêté); Map <string, string> urlbyloanid = new hashmap <string, string> (); // map <string, string> urlbyloanid = zcmQueryInfoservice.QueryUrlByLoanid (map); Essayez {if (urlByLoanid! = null) {String wjurl = "http://10.0.15.11:8080/gateway//nfs/marvel-core-admin/2017/10/11/compact_seal_17101119371231615_7.pdf";; // String wjurl = urlByLoanid.get ("url"); int i = wjurl.lastIndexof ("/"); String filename = wjurl.substring (i + 1); Url url = nouvelle URL (wjurl); HttpurlConnection conn = (httpurlconnection) url.openconnection (); // 设置超时间为 3 秒 Conn.SetConnectTimeout (3 * 1000); // 防止屏蔽程序抓取而返回 403 错误 Conn.SetRequestProperty ("User-Agent", "Mozilla / 4.0 (compatible; MSIE 5.0; Windows NT; DIGEXT)"); // 得到输入流 inputStream inputStream = Conn.getInputStream (); // 获取自己数组 octet [] bs = readInputStream (inputStream); Response.SetContentType ("Application / OCTET-Stream; charSet = ISO8859-1"); BufferedOutputStream output = null; BufferedInputStream Input = null; try {Output = new BufferedOutputStream (réponse.getOutputStream ()); // 中文文件名必须转码为 ISO8859-1, 否则为乱码 String filenameDown = new String (filename.getBytes (), "iso8859-1"); // 作为附件下载 Response.sethEader ("Content-Disposition", "attachement; filename =" + filenamedown); output.write (BS); réponse.flushBuffer (); } catch (exception e) {log.Error ("Télécharger l'erreur de fichier journal", e); } // 用户可能取消了下载 enfin {if (input! = Null) try {input.close (); } catch (ioException e) {e.printStackTrace (); } if (output! = null) try {output.close (); } catch (ioException e) {e.printStackTrace (); }}}} catch (exception e) {e.printStackTrace (); }} / ** * 从输入流中获取字节数组 * @param inputStream * @return * @throws ioException * / public static byte [] readInputStream (inputStream inputStream) lève ioException {byte [] buffer = new Byte [1024]; int len = 0; BytearrayoutputStream bos = new bytearrayoutputStream (); while ((len = inputStream.read (tampon))! = -1) {bos.write (tampon, 0, len); } bos.close (); return bos.toByTearray (); }以上这篇 java 通过 url 下载文件并输出的方法就是小编分享给大家的全部内容了 , 希望能给大家一个参考 , 也希望大家多多支持武林网。