<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependency>
<groupId>com.github.ipfs</groupId>
<artifactId>java-ipfs-api</artifactId>
<version>1.3.3</version>
</dependency>
如果上述1.3.3版本的无法下载依赖包,可尝试1.2.2版本。
这个时候如果api包导入成功,但是使用的时候提示相关的依赖无法import,可以手动加入下面的依赖(都是IPFS的内部依赖包)
<dependency>
<groupId>com.github.multiformats</groupId>
<artifactId>java-multihash</artifactId>
<version>v1.3.0</version>
</dependency>
<dependency>
<groupId>com.github.multiformats</groupId>
<artifactId>java-multibase</artifactId>
<version>v1.1.0</version>
</dependency>
<dependency>
<groupId>com.github.multiformats</groupId>
<artifactId>java-multiaddr</artifactId>
<version>v1.4.1</version>
</dependency>
<dependency>
<groupId>com.github.ipld</groupId>
<artifactId>java-cid</artifactId>
<version>v1.3.1</version>
</dependency>
如果maven一直导入失败的话,可以尝试使用jar包的方式进行依赖。
下载链接:
java-ipfs-api.jar.
java-multiaddr.jar.
java-multibase.jar.
java-multihash.jar.
java-cid.jar.
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.FileOutputStream;
import java.io.IOException;
/**
* @Author: luowenxing
* @Date: 2021/5/31 21:30
*/
public class IPFSUtis {
/**
* ipfs的服务器地址和端口,替换成自己的ip,port
*/
private static IPFS IPFS = new IPFS("/ip4/ip/tcp/port");
public static String upload(String fileName) throws IOException {
NamedStreamable.FileWrapper file = new NamedStreamable.FileWrapper(new File(fileName));
MerkleNode addResult = IPFS.add(file).get(0);
return addResult.hash.toString();
}
public static String upload(byte[] data) throws IOException {
NamedStreamable.ByteArrayWrapper file = new NamedStreamable.ByteArrayWrapper(data);
MerkleNode addResult = IPFS.add(file).get(0);
return addResult.hash.toString();
}
public static byte[] download(String hash) {
byte[] data = null;
try {
data = IPFS.cat(Multihash.fromBase58(hash));
} catch (IOException e) {
e.printStackTrace();
}
return data;
}
public static void download(String hash, String destFile) {
byte[] data = null;
try {
data = IPFS.cat(Multihash.fromBase58(hash));
} catch (IOException e) {
e.printStackTrace();
}
if (data != null && data.length > 0) {
File file = new File(destFile);
if (file.exists()) {
file.delete();
}
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
fos.write(data);
fos.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}