gpt4 book ai didi

java - 从URL读取InputStream并通过Servlet写入

转载 作者:行者123 更新时间:2023-11-30 04:08:44 27 4
gpt4 key购买 nike

我正在尝试编写一个 InputStream,它是通过 ServletdoGet 方法从 URL 获取的>。这是我的代码:

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 image “the_context_root/dam/no-image-aware-servlet?url=http%3A//localhost%3A80/file/MHIS044662&Rendition=164FixedWidth&noSaveAs=1” cannot be displayed because it contains errors.

我找不到我做错了什么。任何指针都会非常有帮助。

最佳答案

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

特别是如果出现问题(例如在其他服务器上找不到文件、IOException、非法 URL 等),请确保始终返回错误状态代码(2xx 除外),否则浏览器始终会收到 200 - 好的,但是没有数据!

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!
}

关于java - 从URL读取InputStream并通过Servlet写入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20149058/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com