doufocheng6233 2015-08-24 17:24
浏览 89
已采纳

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

I am evaluating Hessian to do RPC from PHP to Java.

Java is the backend server , which exposes hessian service (via Spring) for PHP .

(Yes , I know there are modern/standard REST-style to transfer(POST) file , but Hessian is more efficient , doesn't need to parse , and can be easily exposed by Spring )

Simple message passings are OK. One problem is I don't know the correct way to upload a file from PHP to Java.

The java side is simple :

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

in PHP side (1st try) :

$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));

And the java server reports :

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]

If I change my PHP to (2nd try) :

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

The java side reports :

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] )

How to solve it ? Thanks !

environments :

<springboot.version>1.3.0.M2</springboot.version>    
<dependency>
  <groupId>com.caucho</groupId>
  <artifactId>hessian</artifactId>
  <version>4.0.38</version>
</dependency>
  • PHP's Hessian : 2.0.3
  • 写回答

1条回答 默认 最新

  • dsgk40568 2015-08-26 03:42
    关注

    First , Thanks @Master_ex , he points out the correct solution :

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

    For anyone who is interested in this topic.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容