java-ipfs-api.jar的食用方法
阿新 • • 發佈:2018-11-07
引入java-ipfs-api.jar
從倉庫引入
在pom.xml中新增倉庫
<repositories>
<!-- 阿里雲映象,不需要的可以忽略 -->
<repository>
<id>public</id>
<name>aliyun nexus</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
</repository>
<!-- jitpack.io倉庫 -->
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</ repository>
</repositories>
<dependency>
<groupId>com.github.ipfs</groupId>
<artifactId>java-ipfs-api</artifactId>
<version>v1.2.2</version>
</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沒什麼卵用,上效果圖
ipfs hash為https://ipfs.io/ipfs/QmfGp2pRCs3VidFKEVh6NFdrJ3usfrRDr2KXW9k4t3EfUK