gpt4 book ai didi

java - HTTPS 的代理 Servlet

转载 作者:行者123 更新时间:2023-11-29 09:30:56 24 4
gpt4 key购买 nike

我有一个安全站点,需要在某些页面上显示来自外部非 https URL 的图像。我想创建一个仅用作将图像数据传递到页面的代理的 servlet。一种方法是使用 Apache 的 HttpClient 下载图像数据,然后使用 IOUtils.copy 将数据复制到 servlet 的响应中。

有没有更简单的方法?

更新:这样做的原因是为了避免浏览器警告。

最佳答案

这是我最终使用的:

    protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
try {
String url = request.getParameter("url");
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
InputStream inputStream = httpEntity.getContent();
response.setContentType("image/jpeg");
IOUtils.copy(inputStream, response.getOutputStream());
} catch (Exception e) {
AppLogger.log(e);
}
}

如果有人有更好的方法来完成这个,请发布。

关于java - HTTPS 的代理 Servlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10867012/

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