栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

ipfs问题汇总

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

ipfs问题汇总

java连接ipfs 1、使用Maven方式引入包:
    
        
            jitpack.io
            https://jitpack.io
        
    
        
            com.github.ipfs
            java-ipfs-api
            1.3.3
        
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”);
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/749503.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号