url连接可以在浏览器直接看到图片但是通过后台代码保存到本地的图片显示图片错误

神歌圣音 2020-03-30 04:16:22
url连接可以在浏览器直接看到图片但是通过后台代码保存到本地的图片显示图片错误

这是后台代码

URL url = null;
try {
Authenticator.setDefault(new MyAuthenticator("admin","123456"));
//HttpClientUtil.getHttpClient();
url = new URL(urlList);
DataInputStream dataInputStream = new DataInputStream(url.openStream());
FileOutputStream fileOutputStream = new FileOutputStream(new File(savePath));
ByteArrayOutputStream output = new ByteArrayOutputStream();

byte[] buffer = new byte[1024];
int length;

while ((length = dataInputStream.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
byte[] context=output.toByteArray();
fileOutputStream.write(output.toByteArray());
dataInputStream.close();
fileOutputStream.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
...全文
450 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
神歌圣音 2020-03-30
  • 打赏
  • 举报
回复
引用 6 楼 marwi_study 的回复:
[quote=引用 5 楼 神歌圣音的回复:][quote=引用 4 楼 marwi_study 的回复:] 你可能是路径没找对,所以才会读不到那个图片,用绝对路径试一下
用的是url路径,在浏览器可以显示但是到了后台就不行了(url是一个get接口)[/quote] 我的意思是要加上applicationContextPath的那个路径,确定能拿到那个图片的路径[/quote] 可能是我的理解有误,我解释一下,现在的情况是我给一个接口传递一个相对路径,接口返回字节流,我将字节流在网页上用图片显示,之后我想通过另外的接口获取现在网页上显示的图片的字节流将其下载下来
marwi_study 2020-03-30
  • 打赏
  • 举报
回复
引用 5 楼 神歌圣音的回复:
[quote=引用 4 楼 marwi_study 的回复:] 你可能是路径没找对,所以才会读不到那个图片,用绝对路径试一下
用的是url路径,在浏览器可以显示但是到了后台就不行了(url是一个get接口)[/quote] 我的意思是要加上applicationContextPath的那个路径,确定能拿到那个图片的路径
神歌圣音 2020-03-30
  • 打赏
  • 举报
回复
引用 4 楼 marwi_study 的回复:
你可能是路径没找对,所以才会读不到那个图片,用绝对路径试一下
用的是url路径,在浏览器可以显示但是到了后台就不行了(url是一个get接口)
marwi_study 2020-03-30
  • 打赏
  • 举报
回复
你可能是路径没找对,所以才会读不到那个图片,用绝对路径试一下
神歌圣音 2020-03-30
  • 打赏
  • 举报
回复
想通过同一个接口将服务器中的图片显示到页面上,让后调用显示图片的接口将图片下载下来
神歌圣音 2020-03-30
  • 打赏
  • 举报
回复
引用 1 楼 marwi_study 的回复:
你把读到的字节流显示在console看看是不是没读到或者没读完整
没读到 <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <p> <img th:src="@{/image/${image_name}}" width="1080" height="900"/> </p> </body> </html> 这是html显示页面
marwi_study 2020-03-30
  • 打赏
  • 举报
回复
你把读到的字节流显示在console看看是不是没读到或者没读完整

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧