2016-04-28 163 views
0

我有2個服務器,其中一個是公開的,另一個是我的用戶隱藏的。首先可以看到本地網絡中的第二個,但用戶無法訪問公共網絡上的第二臺計算機。重定向到Java中隱藏的URL

我想將一些URL(具有特定模式)從公共計算機重定向到隱藏。隱藏的URL可能會返回響應中的任何標題。

目前我通過重定向流實現此功能,但問題是(最終用戶/客戶端)的響應沒有全部頭文件。

隱藏迴應:重定向數據流後

Accept-Ranges: bytes 
Connection: keep-alive 
Content-Disposition: filename="0.jpg" 
Content-Length: 38903 
Content-Range: bytes 0-38902/38903 
Content-Type: image/jpeg;charset=UTF-8 
Date: Thu, 28 Apr 2016 05:11:48 GMT 
Last-Modified: Mon, 25 Apr 2016 06:28:17 GMT 
Server: Apache-Coyote/1.1 

響應:

Connection: keep-alive 
Content-Length: 38903 
Date: Thu, 28 Apr 2016 05:14:06 GMT 
Server: Apache-Coyote/1.1 

我的代碼是什麼鏈接這樣的:

private void redirectGalleyStream(long diskId, String code, HttpServletResponse response) 
     throws IOException { 
    final InputStream is = new URL(Settings.i().getVodServer() + "/downloadGalleryImage" + 
      "?diskId=" + diskId + "&code=" + code).openStream(); 
    fastCopy(is, response.getOutputStream()); 
} 


public static void fastCopy(final InputStream src, final OutputStream dest) throws IOException { 
    try { 
     final ReadableByteChannel inputChannel = Channels.newChannel(src); 
     final WritableByteChannel outputChannel = Channels.newChannel(dest); 
     fastCopy(inputChannel, outputChannel); 
    } finally { 
     try { 
      src.close(); 
     } catch (Exception ignored) { 
     } 
     try { 
      dest.close(); 
     } catch (Exception ignored) { 
     } 
    } 
} 

public static void fastCopy(final ReadableByteChannel src, final WritableByteChannel dest) throws IOException { 
    try { 
     final ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024); 

     while (src.read(buffer) != -1) { 
      buffer.flip(); 
      dest.write(buffer); 
      buffer.compact(); 
     } 

     buffer.flip(); 

     while (buffer.hasRemaining()) { 
      dest.write(buffer); 
     } 
    } finally { 
     try { 
      src.close(); 
     } catch (Exception ignored) { 
     } 
     try { 
      dest.close(); 
     } catch (Exception ignored) { 
     } 
    } 
} 

我怎麼可以在客戶端的所有頭?

回答

1

我已經解決了這個問題。

public static void fastCopy(String url,HttpServletResponse response, HttpServletRequest request)throws IOException URL obj = new URL(url); URLConnection conn = obj.openConnection();

Enumeration<String> headers = request.getHeaderNames(); 
    while (headers.hasMoreElements()) { 
     final String headerName = headers.nextElement(); 
     conn.addRequestProperty(headerName, request.getHeader(headerName)); 
    } 

    // Cast to a HttpURLConnection 
    if (conn instanceof HttpURLConnection) { 
     HttpURLConnection httpConnection = (HttpURLConnection) conn; 
     int code = httpConnection.getResponseCode(); 

     if (code < 300) { 
      Map<String, List<String>> map = conn.getHeaderFields(); 
      for (Map.Entry<String, List<String>> entry : map.entrySet()) { 
       final String key = entry.getKey(); 
       if(Objects.equals(key, "Transfer-Encoding")) continue; 
       String value = String.valueOf(map.get(key)); 
       if (value != null && value.length() > 0) 
        value = value.substring(1, value.length() - 1); 
       response.setHeader(key, value); 
      } 
      fastCopy(conn.getInputStream(), response.getOutputStream()); 
     } 
     response.setStatus(code); 
    } else { 
     System.err.println("error - not a http request!"); 
    } 
}