
流式中继publicvoidrelay(PathVariableStringfileId,ServletResponseservletResponse)throwsException{// 1. 从 A 获取流URLurlnewURL(http://A/download/fileId);URLConnectionconnurl.openConnection();InputStreamaInconn.getInputStream();// 2. 打开 B 的输出流HTTP chunkedURLbUrlnewURL(http://B/upload);HttpURLConnectionbConn(HttpURLConnection)bUrl.openConnection();bConn.setDoOutput(true);bConn.setFixedLengthStreamingMode(conn.getContentLength());bConn.setRequestProperty(Content-Type,application/octet-stream);OutputStreambOutbConn.getOutputStream();// 3. 边读边写核心byte[]buffernewbyte[8*1024];longtotal0;intlen;while((lenaIn.read(buffer))!-1){bOut.write(buffer,0,len);totallen;}bOut.flush();bOut.close();aIn.close();// 4. 可选检查 B 响应if(bConn.getResponseCode()!200){thrownewRuntimeException(B upload failed);}}