2016-04-28 7 views
0

Ich habe 2 Server, einer von ihnen ist öffentlich und ein anderer ist von meinen Benutzern versteckt. Erstens kann man den zweiten im lokalen Netzwerk sehen, aber Benutzer können nicht auf den zweiten Computer im öffentlichen Netzwerk zugreifen.Umleiten zu versteckten URL in Java

Ich möchte einige URLs (mit einem bestimmten Muster) öffentlichen Computer zu den versteckten umleiten. Die ausgeblendete URL gibt ggf. Header für die Antwort zurück.

Derzeit implementiere ich diese Funktion von Redirect-Streams, aber das Problem ist die Antwort (auf Endbenutzer/Client) hat nicht alle Header.

versteckte Antwort:

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 

Antwort nach Redirect-Streams:

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

Mein Code ist etwas, Link:

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) { 
     } 
    } 
} 

wie kann ich alle Header in Client?

Antwort

1

Ich habe das Problem gelöst.

public static void Fastcopy (String url, HttpServletResponse Antwort, HttpServletRequest Anfrage) 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!"); 
    } 
}