Java和PHP两种方式实现上传图片到新浪微博的图床

这几天遇到一个需求,需要将图片上传到新浪微博的图传,研究了一下, 特此记录

1.模拟登陆,获取cookie

登录地址为:

https://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.15)&_=1403138799543
参数为:
entry=sso&gateway=1&from=null&savestate=30&useticket=0&pagerefer=&vsnf=1&su=" + base64username+"
&service=sso&sp=" + password + "&sr=1920*1080&encoding=UTF-8&cdult=3&domain=sina.com.cn&prelt=0&returntype=TEXT
然后使用post请求,获取cookie,可以从返回的cookie中找到(SUB:)开始的那段,为精简后的cookie
2.上传图片

地址为:

http://picupload.service.weibo.com/interface/pic_upload.php?mime=image%2Fjpeg&data=base64&url=0&markpos=1&logo=&nick=0&marks=1&app=miniblog&cb=http://weibo.com/aj/static/upimgback.html?_wv=5&callback=STK_ijax_" + new Date().getTime();

然后post提交,解析返回的json串,获取图片的pid

3.获取上传后的图片地址
public static String getImageUrl(String pid, int sizeidx, boolean https) {
        String[] sizeArr = new String[]{"large", "mw1024", "mw690", "bmiddle", "small", "thumb180", "thumbnail", "square"};
        pid = pid.trim();
        String size = sizeArr[sizeidx];
        // 传递 pid
        Pattern p = Pattern.compile("^[a-zA-Z0-9]{32}$");
        Matcher m = p.matcher(pid);

        if (m.matches()) {
            System.out.println("匹配了");
            CRC32 crc32 = new CRC32();
            crc32.update(pid.getBytes());
            return (https ? "https" : "http") + "://" + (https ? "ws" : "ww")
                    + ((crc32.getValue() & 3) + 1) + ".sinaimg.cn/" + size
                    + "/" + pid + "." + (pid.charAt(21) == 'g' ? "gif" : "jpg");
        }
//        // 传递 url
        String url = pid;
        Pattern p1 = Pattern.compile("^(https?://[a-z]{2}d.sinaimg.cn/)(large|bmiddle|mw1024|mw690|small|square|thumb180|thumbnail)(/[a-z0-9]{32}.(jpg|gif))$");
        Matcher m1 = p1.matcher(url);
        if (m1.find()) {
            return m.group(1) + size + m.group(3);
        }
        return null;
    }
详细代码见:

https://download.csdn.net/download/x5201314j/10322083




评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值