doufocheng6233
doufocheng6233
2015-08-24 17:24

如何通过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 dsgk40568 6年前

    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.

    点赞 评论 复制链接分享