搭建Ipfs私有网络
IPFS搭建指引
java ipfs功能实现
依赖
<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>v1.2.2</version>
</dependency>
<dependency>
<groupId>com.github.multiformats</groupId>
<artifactId>java-multihash</artifactId>
<version>v1.1.0</version>
</dependency>
interface
package com.jkhl.infrastructure.common.ipfsServer;
import java.io.IOException;
public interface IIpfsService {
/**
* 指定path+文件名称,上传知ipfs
* @param filePath
* @return
* @throws IOException
*/
public String uploadToIpfs(String filePath) throws IOException;
/**
* 将byte格式的数据,上传至ipfs
* @param data
* @return
* @throws IOException
*/
public String uploadToIpfs(byte[] data) throws IOException;
/**
* 根据Hash值,从ipfs下载内容,返回byte数据格式
* @param hash
* @return
*/
public byte[] downFromIpfs(String hash);
/**
* 根据Hash值,从ipfs下载内容,并写入指定文件destFilePath
* @param hash
* @param destFilePath
*/
public void downFromIpfs(String hash, String destFilePath);
}
实现
package com.jkhl.infrastructure.common.ipfsServer;
import com.jkhl.infrastructure.config.CustomizeConfig;
import io.ipfs.api.IPFS;
import io.ipfs.api.MerkleNode;
import io.ipfs.api.NamedStreamable;
import io.ipfs.multihash.Multihash;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
@Log4j2
@Service
public class IpfsService implements IIpfsService {
@Autowired
private CustomizeConfig customizeConfig;
private static IPFS IPFSCONN = null;
public void initConnect() {
try {
if(IPFSCONN==null) {
String connect = customizeConfig.getIpfsConnect();
IPFS ipfsConnect= new IPFS(connect);
IPFSCONN = ipfsConnect;
}
} catch (Exception e) {
log.error(customizeConfig.getIpfsConnect() + "ipfs连接失败!原因: " + e.getMessage());
}
}
@Override
public String uploadToIpfs(String filePath) throws IOException {
this.initConnect();
NamedStreamable.FileWrapper file = new NamedStreamable.FileWrapper(new File(filePath));
MerkleNode addResult = IPFSCONN.add(file).get(0);
log.info(String.format("%s文件上传成功,Hash值为: %s",file.getName(), addResult.hash));
return addResult.hash.toString();
}
@Override
public String uploadToIpfs(byte[] data) throws IOException {
this.initConnect();
NamedStreamable.ByteArrayWrapper file = new NamedStreamable.ByteArrayWrapper(data);
MerkleNode addResult = IPFSCONN.add(file).get(0);
log.info(String.format("%s文件上传成功,Hash值为: %s",file.getName(), addResult.hash));
return addResult.hash.toString();
}
@Override
public byte[] downFromIpfs(String hash) {
this.initConnect();
byte[] data = null;
try {
data = IPFSCONN.cat(Multihash.fromBase58(hash));
} catch (IOException e) {
e.printStackTrace();
}
return data;
}
@Override
public void downFromIpfs(String hash, String destFile) {
byte[] data = null;
try {
this.initConnect();
data = IPFSCONN.cat(Multihash.fromBase58(hash));
} catch (IOException e) {
log.error(hash + "下载失败,原因:" + e.getMessage());
}
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) {
log.error(destFile + "目标文件写入失败;原因:" + e.getMessage());
} finally {
try {
fos.close();
} catch (IOException e) {
log.error(e.getMessage());
}
}
}
}
}