实例如下所示 :
controller:@RequestMapping(value = "/{loanid}/{atmtType}") public void doGet(@PathVariable("loanid") String loanid,@PathVariable("atmtType") String atmtType, HttpServletRequest req,HttpServletResponse response) { Map<String,String> map = new Hashmap <string, string> (); map.put ("prandarid", préstamo); Map <string, string> urlbyloanId = new HashMap <String, String> (); // map <string, string> urlByloanId = ZCMQueryInfoservice.QueryUrlByloanId (MAP); Pruebe {if (urlbyloanid! = null) {string wjurl = "http://10.0.15.11:8080/gateway//nfs/marvel-core-admin/2017/10/11/compact_seal_1710119371231615_7.pdf"; // string wjurl = urlbyloanid.get ("url"); int i = wjurl.lastindexof ("/"); Cadena filename = wjurl.substring (i+1); Url url = nueva 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 (); // 获取自己数组 byte [] bs = readInputStream (inputStream); Respuesta.SetContentType ("Aplicación/Octet-Stream; Charset = ISO8859-1"); BufferedOutputStream output = null; BufferedInputStream entrada = nulo; intente {output = new BufferedOutputStream (respuesta.getOutputStream ()); // 中文文件名必须转码为 ISO8859-1, 否则为乱码 String FileNadown = new String (FileName.getBytes (), "ISO8859-1"); // 作为附件下载 Response.setheader ("Contenido-Dispositivo", "Adjunto; FileName =" + FileNadown); output.write (BS); respuesta.flushbuffer (); } catch (excepción e) {log.error ("Descargar error de archivo de registro", e); } // 用户可能取消了下载 Finalmente {if (input! = Null) try {input.close (); } catch (ioException e) {E.PrintStackTrace (); } if (salida! = 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) lanza IOException {byte [] buffer = new byte [1024]; int len = 0; ByteArRayOutputStream bos = new ByteArRaReOutputStream (); while ((len = inputStream.read (buffer))! = -1) {bos.write (buffer, 0, len); } bos.close (); return bos.tobytearray (); }以上这篇 java 通过 url 下载文件并输出的方法就是小编分享给大家的全部内容了 , 希望能给大家一个参考 也希望大家多多支持武林网。