java-ipfs-api.jar的食用方法

引入java-ipfs-api.jar

从仓库引入

在pom.xml中添加仓库

	<repositories>
		
        <repository>
            <id>publicid>
            <name>aliyun nexusname>
            <url>http://maven.aliyun.com/nexus/content/groups/public/url>
            <releases>
                <enabled>trueenabled>
            releases>
        repository>
        
        <repository>
            <id>jitpack.ioid>
            <url>https://jitpack.iourl>
        repository>
    repositories>
		<dependency>
            <groupId>com.github.ipfsgroupId>
            <artifactId>java-ipfs-apiartifactId>
            <version>v1.2.2version>
        dependency>

源码打包(推荐)

git clone https://github.com/ipfs/java-ipfs-api.git
mvn clean install -Dmaven.test.skip=true

启动IPFS

还没写,后补

食用方法(v1.2.2)

IPFSUtil.java

import io.ipfs.api.IPFS;
import io.ipfs.api.MerkleNode;
import io.ipfs.api.NamedStreamable;
import io.ipfs.multihash.Multihash;

import java.io.File;
import java.io.IOException;

public class IPFSUtil {
   private static IPFS ipfs = new IPFS("/ip4/127.0.0.1/tcp/5001");

   public static String add() throws IOException {
       NamedStreamable.FileWrapper file = new NamedStreamable.FileWrapper(new File("C:\\Users\\Administrator\\Desktop", "test.png"));
       MerkleNode addResult = ipfs.add(file).get(0);
       return addResult.hash.toString();
   }

   public static String cat(String hash) throws IOException {
        byte[] data = ipfs.cat(Multihash.fromBase58(hash));
        return new String(data);
   }
}

IPFSUtilTest.java

import org.junit.Test;
import java.io.IOException;

public class IPFSUtilTest {

    @Test
    public void testAdd() {
        try {
            String hash = IPFSUtil.add();
            System.out.println(hash);
            // QmfGp2pRCs3VidFKEVh6NFdrJ3usfrRDr2KXW9k4t3EfUK
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Test
    public void testCat() {
        try {
            String data = IPFSUtil.cat("QmfGp2pRCs3VidFKEVh6NFdrJ3usfrRDr2KXW9k4t3EfUK");
            System.out.println(data);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

只写了两个简单的测试,其他api后补。add的对象是一个图片,所以cat没什么卵用,上效果图

java-ipfs-api.jar的食用方法_第1张图片

ipfs hash为https://ipfs.io/ipfs/QmfGp2pRCs3VidFKEVh6NFdrJ3usfrRDr2KXW9k4t3EfUK

你可能感兴趣的:(备忘)