Java实现IPFS文件的上传和下载
2022/1/25 20:04:27
本文主要是介绍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); } }
青年人的责任重大!努力吧...
这篇关于Java实现IPFS文件的上传和下载的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-05小米13T Pro系统合集:性能与摄影的极致融合,值得你升级的系统ROM
- 2024-10-01基于Python+Vue开发的医院门诊预约挂号系统
- 2024-10-01基于Python+Vue开发的旅游景区管理系统
- 2024-10-01RestfulAPI入门指南:打造简单易懂的API接口
- 2024-10-01初学者指南:了解和使用Server Action
- 2024-10-01Server Component入门指南:搭建与配置详解
- 2024-10-01React 中使用 useRequest 实现数据请求
- 2024-10-01使用 golang 将ETH账户的资产平均分散到其他账户
- 2024-10-01JWT用户校验课程:从入门到实践
- 2024-10-01Server Component课程入门指南