从URL读取InputStream并通过Servlet编写

时间:2013-11-22 15:53:37

标签: java url servlets response inputstream

我正在尝试编写一个InputStream,这是URL doGet 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

我可以在控制台中看到{{1}}是200,但它没有在页面中写任何内容。在Firefox中我得到:

  

图片   “the_context_root /坝/无图像感知-的servlet URL = HTTP%3A //本地主机%3A80 /文件/ MHIS044662&安培;移交逃犯= 164FixedWidth&安培; noSaveAs = 1”   无法显示,因为它包含错误。

我无法找到我做错的事。任何指针都会非常有用。

1 个答案:

答案 0 :(得分:2)

我尝试稍微修改您的代码,以解决getResponseCode()connect()的错误顺序以及其他一些小问题。

特别要确保在出现问题时总是返回错误状态代码(2xx除外)(例如,在其他服务器上找不到文件,IOException,非法URL,...),否则浏览器总是得到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!
}