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

时间:2015-08-24 17:24:49

标签: java php spring hessian

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

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

(是的,我知道有现代/标准的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>
  • PHP&#39; Hessian:2.0.3

1 个答案:

答案 0 :(得分:0)

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

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

对于对此主题感兴趣的任何人。