IPFS Java实现

本文档详细介绍了如何搭建IPFS私有网络,并提供了Java接口的实现,包括上传文件、下载文件等关键操作。通过引入java-ipfs-api和java-multihash依赖,实现了与IPFS节点的交互,方便在Java应用中集成IPFS功能。
摘要由CSDN通过智能技术生成

搭建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());
                }

            }
        }
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值