微博图床Java API

2018-11-11 12:08:05

wbp4j

微博图床Java API
https://github.com/echisan/wbp4j

简介

这是个使用了微博图床接口的上传图片的api

  • 使用方便简单
  • cookie过期自动登录

Maven

添加依赖

  1. <dependency>
  2. <groupId>com.github.echisan</groupId>
  3. <artifactId>wpb4j</artifactId>
  4. <version>2.0</version>
  5. </dependency>

quickstart

  1. UploadRequest uploadRequest = new UploadRequestBuilder()
  2. .setAcount("weibo username/email", "password")
  3. .build();
  4. UploadResponse response = uploadRequest.upload(new File("path"));
  5. System.out.println(response.getResult());
  6. System.out.println(response.getMessage());
  7. System.out.println(response.getImageInfo());

返回结果

  1. SUCCESS
  2. upload success!
  3. ImageInfo{
  4. pid='7fa15162gy1fx0rsux7bcj20dn07e0t7',
  5. width=491, height=266, size=27707,
  6. large='//ws3.sinaimg.cn/large/7fa15162gy1fx0rsux7bcj20dn07e0t7.jpg',
  7. middle='//ws3.sinaimg.cn/mw690/7fa15162gy1fx0rsux7bcj20dn07e0t7.jpg',
  8. small='//ws3.sinaimg.cn/thumbnail/7fa15162gy1fx0rsux7bcj20dn07e0t7.jpg'
  9. }

使用

目前可以设置的参数,除了setAcount是必须设置之外,其他都可以不配置

  1. UploadRequest uploadRequest = new UploadRequestBuilder()
  2. .setAcount("", "")
  3. .setTryLoginTime(重新登陆的时间间隔,默认2分钟,会根据重登的次数*间隔网上涨,直到登陆成功)
  4. .setCookieCacheName(登陆成功会对cookie缓存到本地,可以自定义缓存的名称)
  5. .setCookieCacheFilePath(自定义缓存的路径)
  6. .build();

Spring中使用

  1. @Bean
  2. public UploadRequest wbpUploadRequest(){
  3. return new UploadRequestBuilder()
  4. .setAcount("", "")
  5. .build();
  6. }
  7. @PostMapping("/wbp")
  8. public ImageInfo wbp(@RequestParam("image") MultipartFile file) throws IOException, Wbp4jException {
  9. // 需要将MultipartFile转化成file对象
  10. File f = new File(file.getName());
  11. FileOutputStream fos = new FileOutputStream(f);
  12. fos.write(file.getBytes());
  13. fos.close;
  14. UploadResponse upload = uploadRequest.upload(f);
  15. return upload.getImageInfo();
  16. }

更新日志

重构了代码,减少第三方依赖,目前只依赖logging,fastjson,将包上传至官方仓库使用更方便————2018.11.08


1
2
0

添加评论

正在回复:
取消
5
1
2
0