2013-11-22 111 views
1

我正在嘗試編寫InputStream,我從URL獲得的doGet方法。這裏是我的代碼:從URL中讀取InputStream並寫入Servlet

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    String requestedUrl = request.getParameter("url"); 

    if (StringUtils.isNotBlank(requestedUrl)) { 
     ReadableByteChannel inputChannel = null; 
     WritableByteChannel outputChannel = null; 

     try { 
      URL url = new URL(requestedUrl); 
      HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection(); 
      int responseCode = httpConnection.getResponseCode(); 

      System.out.println(responseCode); 

      if (responseCode == HttpURLConnection.HTTP_OK) { 
       response.setContentType("image/jpg"); 
       httpConnection.connect(); 
       InputStream imageStream = url.openStream(); 
       OutputStream outputStream = response.getOutputStream(); 

       inputChannel = Channels.newChannel(imageStream); 
       outputChannel = Channels.newChannel(outputStream); 
       ByteBuffer buffer = ByteBuffer.allocate(10240); 

       while (inputChannel.read(buffer) != -1) { 
        buffer.flip(); 
        outputChannel.write(buffer); 
        buffer.clear(); 
       } 
      } 
     } catch (Exception ex) { 
      Log.error(this, ex.getMessage(), ex); 
     } finally { 
      if (ObjectUtils.notEqual(outputChannel, null)) { 
       try { 
        outputChannel.close(); 
       } catch (IOException ignore) { 
       } 
      } 

      if (ObjectUtils.notEqual(inputChannel, null)) { 
       try { 
        inputChannel.close(); 
       } catch (IOException ignore) { 
       } 
      } 
     } 
    } 
} 

我可以在控制檯的responseCode是200看到,但它不是寫在頁面什麼。在Firefox中我得到:

圖像0​​「the_context_root /水壩/無圖像感知,服務器URL = HTTP%3A //本地主機%3A80 /文件/ MHIS044662 &移交逃犯= 164FixedWidth & noSaveAs = 1 「 無法顯示,因爲它包含錯誤。

我無法找到我做錯了什麼。任何指針都會非常有幫助。

+0

您可以在httpConnection.connect()之前使用響應代碼嗎? – Taylor

+0

@泰勒我不知道我抓到你。你是否想在httpConnection.getResponseCode();之前調用'httpConnection.connect()'? –

+0

是的。如果您在連接之前有響應代碼,我會很驚訝。 – Taylor

回答

2

我試着修改一下你的代碼來解決getResponseCode()connect()以及其他一些小問題的錯誤順序。

如果出現問題(例如,在其他服務器上找不到文件,IOException,非法URL等),請務必返回錯誤狀態代碼(2xx除外),否則瀏覽器始終會獲得200- OK但沒有數據!

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    String requestedUrl = request.getParameter("url"); 

    if (StringUtils.isBlank(requestedUrl)) { 
     // TODO: send error code 400 - Bad Request 
     return; 
    } 

    try { 
     URL url = new URL(requestedUrl); 
     HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection(); 
     response.setContentType("image/jpg"); 
     httpConnection.connect(); 

     int responseCode = httpConnection.getResponseCode(); 

     if (responseCode == HttpURLConnection.HTTP_OK) { 
      // TODO: set correct content type to response 

      OutputStream outputStream = response.getOutputStream(); 

      try (InputStream imageStream = url.openStream()) { 
       IOUtils.copy(imageStream, outputStream); 
      } 
     } else { 
      // TODO: send error code (depends on responseCode), probably 500 
     } 
    } catch (Exception ex) { 
     Log.error(this, ex.getMessage(), ex); 
     // TODO: send error code 400 if malformed url 
     // TODO: send error code 404 if image not found (responseCode == 404) 
     // TODO: send error code 500 else 
    } 
    // Don't close the response-OutputStream! You didn't open it either! 
}