实例如下所示:
コントローラー:@RequestMapping(value = "/{loanid}/{atmttype}")public void doget(@pathvariable( "loanid")string loanid、@pathvariable( "atmttype")string atmttype、httpservletrequest req、httpservletresporspenseponse response) hashmap <string、string>(); map.put( "loanid"、loanid); map <string、string> urlbyloanid = new hashmap <string、string>(); // map <string、string> urlbyloanid = zcmqueryinfoservice.queryurlbyloanid(map); try {if(urlbyloanid!= null){string wjurl = "http://10.0.15.11:8080/gateway//10/11/compact_seal_17101119371231615_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( "user-agent"、 "mozilla/4.0(互換; msie 5.0; windows nt; digext)"); // // byte [] bs = readinputStream(inputstream); Response.setContentType( "Application/Octet-Stream; charset = ISO8859-1"); BufferedOutputStream出力= null; bufferedinputStream input = null; try {output = new BufferedOutputStream(respons.getOutputStream()); //中文文件名必须转码为ISO8859-1、否则为乱码文字列filenAmedOwn = new String(filename.getBytes()、 "ISO8859-1"); // response.setheader( "content-disposition"、 "attachment; filename =" + filenamedown); output.write(bs); Response.flushbuffer(); } catch(例外e){log.error( "logファイルエラーをダウンロード"、e); } //用户可能取消了下载最後に{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)throws 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(); bos.tobytearray(); }以上这篇Java