IPFS 不适用于 Javas GET - 如何使用 POST 来代替?

IPFS doesn't work with Javas GET - how to do it with POST instead?

我对 IPFS 和 Java 有疑问。 互联网上有很多示例,但它们不再有效,因为 IPFS 不再支持 GET。依赖项包含在 pom.xml 中。 所以这就是问题所在。我尝试使用 POST Rest,但仍然出现此错误。你知道如何用 Java 中的 POST 解决这个问题吗?

IPFS ipfs = new IPFS("/ip4/127.0.0.1/tcp/5001");

@POST
@Path("/file2IPFS")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response test(@FormDataParam("file") InputStream uploadInputStream) throws IOException {
    //create File from Inputstream is normally included but for Example with Hello.txt


    ipfs.refs.local();

    NamedStreamable.FileWrapper file = new NamedStreamable.FileWrapper(new File("hello.txt"));
    MerkleNode addResult = ipfs.add(file).get(0);
    
    
    return Response.status(200).entity("Passt").build();
}

这是代码片段。此方法是 Rest-Webservice 的一部分。

java.lang.RuntimeException: IOException contacting IPFS daemon.
Trailer: null 405 - Method Not Allowed
at io.ipfs.api.IPFS.get(IPFS.java:592)
at io.ipfs.api.IPFS.retrieve(IPFS.java:571)
at io.ipfs.api.IPFS.retrieveAndParse(IPFS.java:553)
at io.ipfs.api.IPFS.version(IPFS.java:501)
at io.ipfs.api.IPFS.<init>(IPFS.java:61)
at io.ipfs.api.IPFS.<init>(IPFS.java:52)
at io.ipfs.api.IPFS.<init>(IPFS.java:48)

您应该使用较旧的 IPFS 版本。

此问题是由 IPFS 守护程序的 HTTP 接口中的中断 change 引起的。自版本 0.5.0 起,所有端点仅接受 POST 个请求。

更新: java-ipfs-http-client 的 new version 已于 2020-08-03 发布。它支持 IPFS 0.5.x 和 0.6.x,因此下面的所有信息都变得无关紧要。我保留它只是为了历史目的。


您似乎在使用 IPFS 0.5.0 及更高版本的 java-ipfs-http-client library as a wrapper above the bare HTTP interface. Sadly, that library hasn't been updated in quite some time, and its latest release isn't compatible

似乎 author of java-ipfs-http-client has lost interest in maintaining it. However, he has incorporated the IPFS wrapper code into another project of his, Peergos. That version is actually compatible with the fresher versions of IPFS, and it also has a couple of the critical bugs fixed that are present 在独立库的依赖项中。

正如@hau-phvn 所建议的那样,解决该问题的快速简便方法是将您的 IPFS 守护程序降级为 version 0.4.23

如果您需要使用最新的 IPFS 服务器,请尝试使用 Peergos 作为依赖项(或尝试只删除相关部分,因为整个项目包含许多您可能不了解的其他功能需要)。您也可以尝试一些 recent forks.

的运气