java实现网络图片传给浏览器直接下载
                                    
                                        2018-07-14
                                        阅读 {{counts.readCount}}
                                        评论 {{counts.commentCount}}
                                    
                                    例如七牛云的跨域的图片URL传给浏览器,浏览器直接下载。
例如七牛云的跨域的图片URL传给浏览器,浏览器直接下载。
我试了下,直接扔到前端,会被浏览器直接打开图片。百度上有一个例子是在a标签加download标签,可惜实测下来也只支持同域名下的图片。URL凡是跨域一律直接打开图片不下载。
如果要后端下载,网上的一堆例子都是非跨域的,图片要么在tomcat下,要么在服务器某个盘内。均不支持跨域。
最终实现的思路也是个笨办法,就是还是假装客户端去请求图片,获取输入流,再转成输出流返回给客户端。
这个方案首先容易被一些网站给403 405驳回,然后速度也不稳定,慢起来1~2KB每秒
但是这是我目前成功的唯一的办法。
另外servlet的和jsp的都实现了一下。jsp的更加方便使用
servlet 写法
- package servlet;
 - import java.io.IOException;
 - import java.io.InputStream;
 - import java.io.OutputStream;
 - import java.net.HttpURLConnection;
 - import java.net.URL;
 - import javax.servlet.ServletException;
 - import javax.servlet.annotation.WebServlet;
 - import javax.servlet.http.HttpServlet;
 - import javax.servlet.http.HttpServletRequest;
 - import javax.servlet.http.HttpServletResponse;
 - @WebServlet("/download")
 - public class download extends HttpServlet {
 - private static final long serialVersionUID = 1L;
 - protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 - try {
 - String url = "https://wallpapers.wallhaven.cc/wallpapers/full/wallhaven-664722.jpg";
 - String filename = url.substring(url.lastIndexOf("-")+1, url.length());
 - //读取文件流
 - URL imageUrl = new URL(url);
 - HttpURLConnection httpURLConnection = (HttpURLConnection) imageUrl.openConnection();
 - httpURLConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1");
 - httpURLConnection.setConnectTimeout(5 * 1000);
 - int filesize = httpURLConnection.getContentLength(); // 取数据长度
 - InputStream in = httpURLConnection.getInputStream();
 - OutputStream out = response.getOutputStream();
 - //启动下载
 - response.setHeader("content-disposition", "attachment;filename="+filename);
 - response.addHeader("Content-Length", "" + filesize);
 - response.setHeader("content-type","https://cdn1.zzzmh.cn/blog/image/jpeg");
 - int len = 1;
 - byte[] b = new byte[1024];
 - while ((len=in.read(b))!=-1){
 - out.write(b,0,len);
 - }
 - out.close();
 - in.close();
 - } catch (IOException e) {
 - e.printStackTrace();
 - }
 - }
 - protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 - // TODO Auto-generated method stub
 - doGet(request, response);
 - }
 - }
 
 
jsp写法
- <%@ page language="java" contentType="text/html; charset=UTF-8"
 - pageEncoding="UTF-8"%>
 - <%@ page import="java.io.*" %>
 - <%@ page import="java.net.*" %>
 - <%
 - try {
 - String url = "https://wallpapers.wallhaven.cc/wallpapers/full/wallhaven-664722.jpg";
 - String filename = "wallhaven-664722.jpg";
 - //读取文件流
 - URL imageUrl = new URL(url);
 - HttpURLConnection httpURLConnection = (HttpURLConnection) imageUrl.openConnection();
 - httpURLConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1");
 - httpURLConnection.setConnectTimeout(15 * 1000);
 - int filesize = httpURLConnection.getContentLength(); // 取数据长度
 - InputStream is = httpURLConnection.getInputStream();
 - //启动下载
 - response.setHeader("content-disposition", "attachment;filename="+filename);
 - response.addHeader("Content-Length", "" + filesize);
 - response.setHeader("content-type","https://cdn1.zzzmh.cn/blog/image/jpeg");
 - BufferedOutputStream os = new BufferedOutputStream(response.getOutputStream());
 - int len = 1;
 - byte[] b = new byte[1024];
 - while ((len = is.read(b)) > 0){
 - os.write(b,0,len);
 - }
 - is.close();
 - os.close();
 - } catch (IOException e) {
 - e.printStackTrace();
 - }
 - %>
 - <!-- <!DOCTYPE html>
 - <html>
 - <head>
 - <meta charset="UTF-8">
 - <title>下载</title>
 - </head>
 - <body>
 - </body>
 - </html> -->
 
 
                                        评论区空空如也,赶紧添加一条评论吧
                                        评论 {{counts.commentCount}}
                                    
                                    
                                            
                                                    {{comment.name}}
                                                    {{comment.os}}
                                                    {{comment.browser}}
                                                
                                                
                                                
                                                
                                                        
                                                                {{comment.reply.name}}
                                                                {{comment.reply.os}}
                                                                {{comment.reply.browser}}