Skip to content

Instantly share code, notes, and snippets.

@devender-yadav
Created July 27, 2018 13:14
Show Gist options
  • Save devender-yadav/301ef6b95c4f7b5a6b2d22faa63a71f1 to your computer and use it in GitHub Desktop.
Save devender-yadav/301ef6b95c4f7b5a6b2d22faa63a71f1 to your computer and use it in GitHub Desktop.
IPFS sample program to add and get file.
package com.dev.ipfs.sample;
import java.io.IOException;
import io.ipfs.api.IPFS;
import io.ipfs.api.MerkleNode;
import io.ipfs.api.NamedStreamable;
import io.ipfs.multihash.Multihash;
public class IPFSSample {
public static void main(String[] args) throws IOException {
IPFS ipfs = new IPFS("/ip4/127.0.0.1/tcp/5001");
// ipfs.refs.local();
NamedStreamable.ByteArrayWrapper file = new NamedStreamable.ByteArrayWrapper(
"hello.txt", "my first string".getBytes());
MerkleNode addResult = ipfs.add(file).get(0);
// System.out.println(addResult.hash.toBase58());
Multihash filePointer = Multihash
.fromBase58("QmXCsyADdT78FYqyjdzRNmVHBJS1CmuSYxxD3vnmR5U8ob");
byte[] fileContents = ipfs.cat(filePointer);
String str = new String(fileContents);
System.out.println(str);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment