实例如下所示 :
컨트롤러 : @RequestMapping (value = "/{loantyid}/{atmttype}")) public void doget (@pathvariable ( "loanpatid") string loanid,@pathvarible ( "atmttype") String atmttype, httpservletrequest req, httpervletresponse 응답) {string, string, map = Hashmap <String, String> (); map.put ( "loanid", loanid); map <string, string> urlbyloanid = new Hashmap <String, String> (); // map <string, string> urlbyloanid = zcmqueryInfoservice.queryurlbyloanid (map); {if (urlbyloanid! = null) {String wjurl = "http://10.0.15.11:8080/gateway//nfs/marvel-core-admin/2017/10/11/compact_seal_17101119371615_7.pdf"; // String wjurl = urlbyloanid.get ( "url"); int i = wjurl.lastindexof ( "/"); 문자열 filename = wjurl.substring (i+1); url url = new URL (wjurl); httpurlconnection conn = (httpurlConnection) url.openConnection (); // 3 设置超时间为 Conn.setConnectTimeout (3*1000); // 403 防止屏蔽程序抓取而返回 Conn.setRequestProperty ( "사용자 에이전트", "Mozilla/4.0 (호환 가능; MSIE 5.0; Windows NT; Digext)"); // 得到输入流 inputStream inputStream = conn.getInputStream (); // byte [] bs = readInputStream (inputStream); Response.SetContentType ( "Application/Octet-stream; charset = iso8859-1"); BufferedOutputStream output = null; bufferedInputStream input = null; try {output = new bufferedOutputStream (response.getOutputStream ()); // 中文文件名必须转码为 iso8859-1, 否则为乱码 string filenamedown = new String (filename.getBytes (), "iso8859-1"); // 作为附件下载 response.setHeader ( "내용 분할", "첨부 파일; filename =" + filenamedown); output.write (bs); 응답 .FlushBuffer (); } catch (예외 e) {log.error ( "로그 파일 오류 다운로드", e); } // 用户可能取消了下载 마지막 {if (input! = null) try {input.close (); } catch (ioexception e) {e.printstacktrace (); } if (output! = null) try {output.close (); } catch (ioexception e) {e.printstacktrace (); }}}} catch (예외 e) {e.printstacktrace (); }}/ ** * 从输入流中获取字节数组 * @param inputStream * @return * @throws ioException */ public static byte [] readInputStream (inputStream inputStream) IoException {buber = 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 a url 下载文件并输出的方法就是小编分享给大家的全部内容了 下载文件并输出的方法就是小编分享给大家的全部内容了, 希望能给大家一个参考, 也希望大家多多支持武林网。 也希望大家多多支持武林网。