Java实现IPFS文件的上传和下载

一、导入依赖

Jar包方式

<dependency>
    <groupId>com.github.ipfs</groupId>
    <artifactId>java-ipfs-api</artifactId>
    <version>1.3.3</version>
</dependency>
<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>1.3.3</version>
</dependency>

Maven方式

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>
 
<dependencies>
    <dependency>
        <groupId>com.github.ipfs</groupId>
        <artifactId>java-ipfs-api</artifactId>
        <version>v1.3.3</version>
    </dependency>
</dependencies>

二、代码实例

@Component
public class IPFSUtil {

    public String upload(IPFS ipfs, 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 String upload(IPFS ipfs, byte[] data) throws IOException {
        NamedStreamable.ByteArrayWrapper file = new NamedStreamable.ByteArrayWrapper(data);
        MerkleNode addResult = ipfs.add(file).get(0);
        return addResult.hash.toString();
    }

    public byte[] download(IPFS ipfs, String hash) {
        byte[] data = null;
        try {
            data = ipfs.cat(Multihash.fromBase58(hash));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return data;
    }

    public void download(IPFS ipfs, 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();
                }

            }
        }
    }
}

/**
* IPFSTest
*/

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = {Application.class})
@Slf4j
public class IPFSTest {

    // ipfs的服务器地址和端口
    private IPFS ipfs = new IPFS("/ip4/127.0.0.1/tcp/5001");

    @Autowired
    private IPFSUtil ipfsUtil;

    @Test
    public void testIPFSUpload() throws IOException {
        //  filePath 指的是文件的上传路径+文件名,如D:/1.png  
        String filePath = "D:\\kyrie.png";

        String cid = ipfsUtil.upload(ipfs, filePath);

    }

    @Test
    public void testIPFSDownload() throws IOException {
        String cid = "BmxJxgEUoQ7avSXC7BbazTCSqMmySBrIPmSX7ipWCBcLVN1";
        String destFile = "D:\\irving.jpg";

        ipfsUtil.download(ipfs, cid, destFile);

    }

}

青年人的责任重大!努力吧...

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值