正在加载中,请稍后
author 首页 关于 笔记

按回车搜索更多

java实现网络图片传给浏览器直接下载
2018-07-14 阅读 {{counts.readCount}} 评论 {{counts.commentCount}}

例如七牛云的跨域的图片URL传给浏览器,浏览器直接下载。



例如七牛云的跨域的图片URL传给浏览器,浏览器直接下载。

 

我试了下,直接扔到前端,会被浏览器直接打开图片。百度上有一个例子是在a标签加download标签,可惜实测下来也只支持同域名下的图片。URL凡是跨域一律直接打开图片不下载。

如果要后端下载,网上的一堆例子都是非跨域的,图片要么在tomcat下,要么在服务器某个盘内。均不支持跨域。

 

最终实现的思路也是个笨办法,就是还是假装客户端去请求图片,获取输入流,再转成输出流返回给客户端。

这个方案首先容易被一些网站给403 405驳回,然后速度也不稳定,慢起来1~2KB每秒

但是这是我目前成功的唯一的办法。

另外servlet的和jsp的都实现了一下。jsp的更加方便使用

 

servlet 写法

  1. package servlet;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.OutputStream;
  6. import java.net.HttpURLConnection;
  7. import java.net.URL;
  8.  
  9. import javax.servlet.ServletException;
  10. import javax.servlet.annotation.WebServlet;
  11. import javax.servlet.http.HttpServlet;
  12. import javax.servlet.http.HttpServletRequest;
  13. import javax.servlet.http.HttpServletResponse;
  14.  
  15. @WebServlet("/download")
  16. public class download extends HttpServlet {
  17. private static final long serialVersionUID = 1L;
  18.  
  19. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  20. try {
  21. String url = "https://wallpapers.wallhaven.cc/wallpapers/full/wallhaven-664722.jpg";
  22. String filename = url.substring(url.lastIndexOf("-")+1, url.length());
  23. //读取文件流
  24. URL imageUrl = new URL(url);         
  25. HttpURLConnection httpURLConnection = (HttpURLConnection) imageUrl.openConnection();
  26. httpURLConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1");
  27. httpURLConnection.setConnectTimeout(5 * 1000);
  28. int filesize = httpURLConnection.getContentLength(); // 取数据长度
  29. InputStream in = httpURLConnection.getInputStream();
  30. OutputStream out = response.getOutputStream();
  31. //启动下载
  32. response.setHeader("content-disposition", "attachment;filename="+filename);
  33. response.addHeader("Content-Length", "" + filesize);
  34. response.setHeader("content-type","https://cdn1.zzzmh.cn/blog/image/jpeg");
  35. int len = 1;
  36. byte[] b = new byte[1024];
  37. while ((len=in.read(b))!=-1){
  38. out.write(b,0,len);
  39. }
  40. out.close();
  41. in.close();
  42. } catch (IOException e) {
  43. e.printStackTrace();
  44. }
  45. }
  46.  
  47. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  48. // TODO Auto-generated method stub
  49. doGet(request, response);
  50. }
  51.  
  52.  
  53. }

 

jsp写法

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <%@ page import="java.io.*" %>
  4. <%@ page import="java.net.*" %>
  5. <%
  6. try {
  7. String url = "https://wallpapers.wallhaven.cc/wallpapers/full/wallhaven-664722.jpg";
  8. String filename = "wallhaven-664722.jpg";
  9. //读取文件流
  10. URL imageUrl = new URL(url);         
  11. HttpURLConnection httpURLConnection = (HttpURLConnection) imageUrl.openConnection();
  12. httpURLConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1");
  13. httpURLConnection.setConnectTimeout(15 * 1000);
  14. int filesize = httpURLConnection.getContentLength(); // 取数据长度
  15. InputStream is = httpURLConnection.getInputStream();
  16. //启动下载
  17. response.setHeader("content-disposition", "attachment;filename="+filename);
  18. response.addHeader("Content-Length", "" + filesize);
  19. response.setHeader("content-type","https://cdn1.zzzmh.cn/blog/image/jpeg");
  20. BufferedOutputStream os = new BufferedOutputStream(response.getOutputStream());
  21.  
  22. int len = 1;
  23. byte[] b = new byte[1024];
  24. while ((len = is.read(b)) > 0){
  25. os.write(b,0,len);
  26. }
  27. is.close();
  28. os.close();
  29. } catch (IOException e) {
  30. e.printStackTrace();
  31. }
  32. %>
  33. <!-- <!DOCTYPE html>
  34. <html>
  35. <head>
  36. <meta charset="UTF-8">
  37. <title>下载</title>
  38. </head>
  39. <body>
  40. </body>
  41. </html> -->

 

提交
评论区空空如也,赶紧添加一条评论吧 评论 {{counts.commentCount}}
{{comment.name}} {{comment.os}} {{comment.browser}}
{{dateFormatter(comment.createTime)}}

{{comment.message}}

{{comment.reply.name}} {{comment.reply.os}} {{comment.reply.browser}}
{{dateFormatter(comment.reply.createTime)}}

{{comment.reply.message}}

zzzmh
关于我 留言板

网址导航

{{alert.message}}
留言板 * 站长不经常查看信箱 若有重要事宜联系邮箱 admin@zzzmh.cn 取消 发送