根据url下载图片和页面

 

需要将&tp=webp&wxfrom=5去掉,既可以在任何地方显示,也可以下载了

 

http://mmbiz.qpic.cn/mmbiz_jpg/bf8pC39RBhGFOH1ib9AcqC9k43d5yvcCJMspwk2Eam49QliaymWy1P590MLKgjPcYffXQNWhhTPicKk1unQIGQ2XQ/640?wx_fmt=jpeg&tp=webp&wxfrom=5&wx_lazy=1
http://mmbiz.qpic.cn/mmbiz_jpg/bf8pC39RBhGFOH1ib9AcqC9k43d5yvcCJMspwk2Eam49QliaymWy1P590MLKgjPcYffXQNWhhTPicKk1unQIGQ2XQ/640?wx_fmt=jpeg&wx_lazy=1

 

 

java 下载微信公众号里面的图片

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class GetWexinImage {

    /**
     * 下载微信公众号里的图片
     * @param request
     * @param response
     */
    @RequestMapping("/test/a")
    public void a(HttpServletRequest request, HttpServletResponse response)  {
        String url = "http://mmbiz.qpic.cn/mmbiz_jpg/bf8pC39RBhGFOH1ib9AcqC9k43d5yvcCJt44g7k8DDGyOlXI7Qyw2GGT6NWhWBjPzhWPLnp9zZJTDY3D167kdMA/640?wx_fmt=jpeg&tp=webp&wxfrom=5&wx_lazy=1";
        InputStream fis = null;
        OutputStream os = null;
        try {
            URL getUrl = new URL(url);
            URLConnection connection = getUrl.openConnection();
            connection.setRequestProperty("Referer", "");
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent","Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36");
            connection.setDoOutput(true);
            fis = connection.getInputStream();
            os = response.getOutputStream();
            int count = 0;
            byte[] buffer = new byte[1024 * 1024];
            while ((count = fis.read(buffer)) != -1) {
                os.write(buffer, 0, count);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null){
                try {
                    os.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        response.setContentType("image/*");
    }

}

 

 java 下载微信公众号里面的图片,保存到本地,需要将&tp=webp&wxfrom=5去掉,既可以在任何地方显示,也可以下载了

 /**
     * 下载微信公众号里的图片
     * @param request
     * @param response
     */
    @RequestMapping("/test/b")
    public void b(HttpServletRequest request, HttpServletResponse response)  {
        String url = "http://mmbiz.qpic.cn/mmbiz_jpg/bf8pC39RBhGFOH1ib9AcqC9k43d5yvcCJt44g7k8DDGyOlXI7Qyw2GGT6NWhWBjPzhWPLnp9zZJTDY3D167kdMA/640?wx_fmt=jpeg&wx_lazy=1";
        InputStream fis = null;
        FileOutputStream fos = null;
        try {
            URL getUrl = new URL(url);
            URLConnection connection = getUrl.openConnection();
            connection.setRequestProperty("Referer", "");
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent","Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36");
            connection.setDoOutput(true);
            fis = connection.getInputStream();
            File file = new File("D://test/a.jpg");  
            fos = new FileOutputStream(file);  
            int count = 0;
            byte[] buffer = new byte[1024 * 1024];
            while ((count = fis.read(buffer)) != -1) {
                fos.write(buffer, 0, count);
            }
            
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null){
                try {
                    fos.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null){
                try {
                    fos.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

 

 

java 根据url地址生成html页面文件

import java.io.File;
import java.io.FileOutputStream;  
public class GenerateIndexPage {  
      
    protected static String defaultToFile = "d://test.html";  
    protected static String defaultFromFile = "https://mp.weixin.qq.com/s/OYJzYiQRepc8yfrGReITDA";  
      
    public static void main(String[] args) throws Exception {
        genHtml(defaultFromFile);
    }
    
    public static String genHtml(String fromFile) throws Exception {  
        String result = "";  
        java.net.URL url = new java.net.URL(fromFile);  
        java.net.HttpURLConnection conn =(java.net.HttpURLConnection) url.openConnection();  
        try{  
            if (conn.getResponseCode() == 200) {  
              
                java.io.InputStream is = (java.io.InputStream) conn.getContent();  
                try{  
                    FileOutputStream baos = new FileOutputStream(new File(defaultToFile));  
                    int buffer = 1024;  
                    byte[] b = new byte[buffer];  
                    int n = 0;  
                    while ((n = is.read(b, 0, buffer)) > 0) {  
                        baos.write(b, 0, n);  
                    }  
                    //String s = new String(baos.toByteArray(), WEATHER_HTML_CHARSET);  
                    is.close();  
                    baos.close();  
                    result = "生成成功";  
                }catch(Exception e){  
                    result="写文件过程出错,取消生成。";  
                }  
            }else{  
                result="获得链接过程出错,取消生成。";              
            }  
        }catch(Exception e){  
                e.printStackTrace();  
                result="获得内容过程出错,取消生成。";  
        }  
        return result;  
    }  
      

 

java根据url地址,获取页面内容

public class GetHtml {
    
    public static void main(String[] args) throws Exception {
        getHtml();
    }
    public static String getHtml() throws Exception {  
        //System.setProperty("http.proxyHost", "isaserver");System.setProperty("http.proxyPort", "80");  
        java.net.URL url = new java.net.URL("https://mp.weixin.qq.com/s/OYJzYiQRepc8yfrGReITDA");  
        java.net.HttpURLConnection conn = (java.net.HttpURLConnection) url.openConnection();  
        if (conn.getResponseCode() == 200) {  
            java.io.InputStream is = (java.io.InputStream) conn.getContent();  
            java.io.ByteArrayOutputStream baos =   
                new java.io.ByteArrayOutputStream();  
              
            int buffer = 1024;  
            byte[] b = new byte[buffer];  
            int n = 0;  
            while ((n = is.read(b, 0, buffer)) > 0) {  
                baos.write(b, 0, n);  
            }  
            String s = new String(baos.toByteArray(), "UTF-8");  
            is.close();  
            baos.close();  
            System.out.println(s);
            return s;  
        }  
        return "";  
    }
}

 

转载于:https://www.cnblogs.com/shihaiming/p/9087979.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值