从URL读InputStream和通过Servlet的写(Reading InputStream f

2019-10-19 00:35发布

我试图写一个InputStream ,这是我从一个获取URLdoGet方法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中我得到:

图像“the_context_root /水坝/无图像感知小服务程序?URL = HTTP%3A //本地主机%3A80 /文件/ MHIS044662和移交逃犯= 164FixedWidth&noSaveAs = 1”无法显示,因为它包含错误。

我无法找到我在做什么错。 任何指针将是非常有益的。

Answer 1:

我试图修改代码一点点解决的错误的顺序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!
}


文章来源: Reading InputStream from URL and write it by Servlet