Code Monkey home page Code Monkey logo

freeimg-tools's Introduction

FreeImg-Tools

关于FreeImg图床的调用SDK

开始使用

1. 引入 Maven 依赖

<dependency>
    <groupId>cn.iaimi</groupId>
    <artifactId>FreeImg-Tools</artifactId>
    <version>0.0.2</version>
</dependency>

2. 配置 application.yml

application.xml 中配置 Token 和 AlbumID(可选)

cloverapi:
  free-img:
    # 图床的调用 token
    free-img-token: Bearer 153|zENXxxxxaB1C6sjxxxxxC2fVaxxxdUPpX6xxxxxxY
    # 相册id,可选  (在相册访问url中可知)
    free-img-album-id: 102

3. 使用示例

这里使用的是 SpringBoot 的测试类作为使用 demo,如下:

@SpringBootTest
@ActiveProfiles({"work"})
public class FreeImgToolTest {

    @Resource
    private FreeImgCrudTool freeImgCrudTool;

    @Test
    void uploadImgByFileTest() throws IOException {

        UploadImgRes uploadImgRes = freeImgCrudTool.uploadImage(getFile());
        System.out.println(uploadImgRes);
        System.out.println(uploadImgRes.getData());
        System.out.println(uploadImgRes.getData().getLinks());
    }

    @Test
    void uploadImgByStreamTest() throws IOException {
        ResourceLoader loader = new DefaultResourceLoader();
        org.springframework.core.io.Resource resource = loader.getResource("classpath:/image.png");
        UploadImgRes uploadImgRes = freeImgCrudTool.uploadImage(resource.getInputStream());
        System.out.println(uploadImgRes);
        System.out.println(uploadImgRes.getData());
        System.out.println(uploadImgRes.getData().getLinks());
    }

    /**
     * 获取类路径下的图片文件
     * @return
     * @throws IOException
     */
    private static File getFile() throws IOException {
        ResourceLoader loader = new DefaultResourceLoader();
        org.springframework.core.io.Resource resource = loader.getResource("classpath:/image.png");
        return resource.getFile();
    }

    @Test
    void deleteTest() {
        // https://www.freeimg.cn/delete/uyq5wp/a1101ccdb1e9a066238b8ae972383f46
        boolean b = freeImgCrudTool
                .deleteImg("https://www.freeimg.cn/delete/uyq5wp/a1101ccdb1e9a066238b8ae972383f46");
        System.out.println(b);
    }
}

将 FreeImgCrudTool 依赖注入进来使用

@Resource
private FreeImgCrudTool freeImgCrudTool;

第二种使用方式-工厂模式

public class UploadImageToolTest {

    private final String FREEIMG_TOKEN = "Bearer 100|fvxxxxxxxxxxxxXP";
    private final Integer FREEIMG_ALBUM_ID = 102;


    @Test
    public void useFactoryTest() throws IOException {
        FreeImgCrudTool freeImgCrudTool = FreeImgToolsFactory.create()
                .setFreeImageToken(FREEIMG_TOKEN)  // "Bearer 100|fvxxxxxxxxxxxxXP"
                .setFreeImageAlbumId(FREEIMG_ALBUM_ID) // 相册id  102
                .build();
        // 上传照片
        cn.iaimi.freeimgtools.model.domain.Image image = freeImgCrudTool.upload(getFile());
        System.out.println(image);
        // 删除照片
        boolean isSuccess = freeImgCrudTool.delete(image);// 推荐使用delete 也可以使用 freeImgCrudTool.deleteImg(<传入deleleUrl>)
        if (isSuccess) {
            System.out.println("删除成功");
        } else {
            System.out.println("删除失败");
        }
    }

    private static File getFile() throws IOException {
        ResourceLoader loader = new DefaultResourceLoader();
        Resource resource = loader.getResource("classpath:/image.png");
        String absolutePath = resource.getFile().getAbsolutePath();
        System.out.println(absolutePath);
        return resource.getFile();
    }
}

freeimg-tools's People

Contributors

clov614 avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.