如何通过 Hessian 将文件从 PHP 发送到 Java?

How to send a file from PHP to Java via Hessian?

我正在评估 Hessian 做 RPC 从 PHP 到 Java。

Java 是后端服务器,它为 PHP 公开 hessian 服务(通过 Spring)。

(是的,我知道有 modern/standard REST 风格可以传输(POST)文件,但 Hessian 更高效,不需要解析,并且可以很容易地暴露Spring)

简单的消息传递就可以了。 一个问题是我不知道将文件从 PHP 上传到 Java 的正确方法。

java 方面很简单:

public String uploadIs(InputStream is) {
  // do something and return a String
}

在PHP侧(第一次尝试):

$testUrl = 'http://localhost:8000/hessian/TestService';
$testService = &new HessianClient($testUrl);

$file_stream = file_get_contents($filepath);
$h_stream = &new HessianStream($file_stream);
$h_stream->read();
print_r($testService->uploadIs($h_stream));

并且 java 服务器报告:

WARN  c.c.h.i.SerializerFactory - Hessian/Burlap: 'HessianStream' is an unknown class in java.net.URLClassLoader@1d798282:
java.lang.ClassNotFoundException: HessianStream

WARN  c.c.h.i.SerializerFactory - Hessian/Burlap: 'HessianStream' is an unknown class in java.net.URLClassLoader@1d798282:
java.lang.ClassNotFoundException: HessianStream

ERROR o.a.c.c.C.[.[.[.[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Hessian skeleton invocation failed; nested exception is java.lang.UnsupportedOperationException: com.caucho.hessian.io.InputStreamDeserializer@647a7c13] with root cause
java.lang.UnsupportedOperationException: com.caucho.hessian.io.InputStreamDeserializer@647a7c13 
at com.caucho.hessian.io.AbstractDeserializer.readObject(AbstractDeserializer.java:146) ~[hessian-4.0.38.jar:na]

如果我将 PHP 更改为(第二次尝试):

$file = fopen($filepath, 'r');
$contents = fread($file, filesize($filepath));
print_r($testService->uploadIs($contents));

java 方报告:

ERROR o.a.c.c.C.[.[.[.[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Hessian skeleton invocation failed; nested exception is 

com.caucho.hessian.io.HessianProtocolException: uploadIs: expected binary at 0x53 java.lang.String (... [a lot of binary file content] )

如何解决?谢谢!

环境:

<springboot.version>1.3.0.M2</springboot.version>    
<dependency>
  <groupId>com.caucho</groupId>
  <artifactId>hessian</artifactId>
  <version>4.0.38</version>
</dependency>

首先,感谢@Master_ex,他指出了正确的解决方案:

$file = fopen($filepath, 'rb');
print_r($testService->uploadIs($file));
fclose($file);

致所有对此主题感兴趣的人。