如何使用Hessian二进制协议创建文件Upload / Downlod servlet?适用于大于1GB的文件。我为Hessian with large binary data创建测试,但是如果文件大于可用的RAM大小,则会引发java.lang.OutOfMemoryError。

//客户端的api

public interface Upload {
  public void upload(String filename, InputStream data);
}


//实施

public class Upload extends HessianServlet implements Upload {
  @Override
  public void upload(String fileName, InputStream in) {
    int count = 0;
    try {
      while ((count = in.read()) != -1) {
        System.err.print("reading view in console=" + count);
      }
      in.close();
    } catch (IOException ex) {
        Logger.getLogger(Upload.class.getName()).log(Level.SEVERE, null, ex);
    }
    System.err.print("total buffers filled=" + count);
  }
}


//客户电话

    HessianProxyFactory proxy = new HessianProxyFactory();

    try {


//调用上传代理

      Upload x = (Upload) proxy.create(Upload.class, "http://localhost:8080/PathToServletFile/Upload");

      InputStream in;

      try {
        in = new FileInputStream("C:/bigFile.zip");
        x.upload("some file", in);
      } catch (FileNotFoundException ex) {
        Logger.getLogger(HessianFrame.class.getName()).log(Level.SEVERE, null, ex);
      }
    } catch (MalformedURLException ex) {
        Logger.getLogger(HessianFrame.class.getName()).log(Level.SEVERE, null, ex);
    }


//例外

Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:2786)
at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:94)
at sun.net.www.http.PosterOutputStream.write(PosterOutputStream.java:61)
at com.caucho.hessian.io.HessianOutput.writeByteBufferPart(HessianOutput.java:743)
at com.caucho.hessian.io.AbstractHessianOutput.writeByteStream(AbstractHessianOutput.java:407)
at com.caucho.hessian.io.InputStreamSerializer.writeObject(InputStreamSerializer.java:70)
at com.caucho.hessian.io.HessianOutput.writeObject(HessianOutput.java:324)
at com.caucho.hessian.io.HessianOutput.call(HessianOutput.java:132)
at com.caucho.hessian.client.HessianProxy.sendRequest(HessianProxy.java:280)
at com.caucho.hessian.client.HessianProxy.invoke(HessianProxy.java:170)
at $Proxy0.upload(Unknown Source)
at hessianlargefiletest.HessianFrame.btnDownloadActionPerformed(HessianFrame.java:108)
at hessianlargefiletest.HessianFrame.access$000(HessianFrame.java:32)
at hessianlargefiletest.HessianFrame$1.actionPerformed(HessianFrame.java:56)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6289)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6054)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4652)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4482)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4482)

最佳答案

您不应该使用代理发送大文件

正如hessian document所说:


客户端需要直接控制Hessian协议,因为代理接口需要在调用返回之前缓冲整个文件。


因此,您可以尝试执行此操作(我尚未对其进行测试)

public interface Download {
    public InputStream download(String filename, InputStream data){
          //create your http connection and get the outputstream
          OutputStream os = ....;

          Hessian2Output out = new Hessian2Output(os);

          out.writeObject(filename);
          byte[] buffer = new byte[1024];
          int c = -1;
          while( (c = data.read(buf)) != -1) {
             os.write(buf);
          }
          os.flush();
          //.....
    }
}

10-08 13:30