ipfs问题汇总

java连接ipfs

1、使用Maven方式引入包:

    <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>

2、代码demo:

package com.chainmaker.nft.service;

import io.ipfs.api.IPFS;
import io.ipfs.api.MerkleNode;
import io.ipfs.api.NamedStreamable;
import io.ipfs.multihash.Multihash;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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

@Service
@Slf4j
public class ipfsService {

    private IPFS ipfs;

    public void IPFSOpe(String ip) {
        this.ipfs = new IPFS(ip);
    }

    public void upload(String upaddr) throws IOException {
        //保存上传文件
        NamedStreamable.FileWrapper savefile = new NamedStreamable.FileWrapper(new File(upaddr));
        MerkleNode result = ipfs.add(savefile).get(0);
        System.out.println(result.toString());


    }
    public void cat(String hash) throws IOException {
        //参数为文件 hash
        Multihash filePointer = Multihash.fromBase58(hash);
        byte[] fileContents = ipfs.cat(filePointer);
        System.out.println(new String(fileContents));
    }
    public void download(String hash, String downaddr) throws IOException {
        //参数为文件 hash
        Multihash filePointer = Multihash.fromBase58(hash);
        byte[] fileContents = ipfs.cat(filePointer);

        File downloadfile = new File(downaddr);
        if(!downloadfile.exists()) {
            downloadfile.createNewFile();
        }
        FileOutputStream fop = new FileOutputStream(downloadfile);
        fop.write(fileContents);
        fop.flush();
        fop.close();
    }

    public static void main(String[] args) throws IOException {
        String ip = "/ip4/127.0.0.1/tcp/5001";
        String upaddr = "/root/lyzdfintech.com/image/test.log";
//        String downaddr = "/root/lyzdfintech.com/image/test1.log";
//        String hash = "QmNkspXsCVxsjVBG3FCoBJVfcD5zT15K3Uzo9J14NnLQru"; //test.txt
        //String hash = "QmNkspXsCVxsjVBG3FCoBJVfcD5zT15K3Uzo9J14NnLQru"; //java.txt
        ipfsService test = new ipfsService();
        test.IPFSOpe(ip);
        test.upload(upaddr);
        //test.cat(hash);
//        test.download(hash, downaddr);

    }
}



异常汇总

1、java-sdk连接失败

 java.lang.RuntimeException: Server returned status: 404 with body: and Trailer header: null

解决方案:
将8080 Gateway 改为API 5001

IPFS ipfs = new IPFS(“/ip4/127.0.0.1/tcp/8080”);
修改为
IPFS ipfs = new IPFS(“/ip4/127.0.0.1/tcp/5001”);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
IPFS (InterPlanetary File System) 是一个开源的分布式文件系统,它通过使用分布式哈希表来连接全球计算机网络,提供了一个去中心化的存储和共享文件的方式。你可以使用 Python 来与 IPFS 进行交互和操作。 在 Python 中,你可以使用 ipfshttpclient 库来连接并与 IPFS 节点进行通信。首先,你需要安装该库,可以使用以下命令来安装: ``` pip install ipfshttpclient ``` 安装完成后,你可以按照以下示例代码来使用 IPFS 的基本功能: ```python import ipfshttpclient # 连接到 IPFS 节点 client = ipfshttpclient.connect() # 添加文件到 IPFS res = client.add('path/to/file.txt') file_hash = res['Hash'] print(f'File uploaded, IPFS hash: {file_hash}') # 获取文件内容 content = client.cat(file_hash) print(f'File content: {content}') # 获取文件的信息 file_info = client.stat(file_hash) print(f'File size: {file_info["Size"]}') # 下载文件到本地 client.get(file_hash) # 关闭连接 client.close() ``` 以上代码演示了如何连接到 IPFS 节点、上传文件、获取文件内容和信息、下载文件等基本操作。你可以根据自己的需求进行扩展和调整。 请注意,在实际使用中,你需要确保 IPFS 节点正常运行并具备访问权限。另外,IPFS 是一个分布式系统,因此文件上传后并不能立即在全球范围内被访问到,需要等待网络中的节点完成传播和同步。 希望这能帮助到你!如果你有任何其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值