具有大型二进制文件的 Hessian

发布于 2024-11-02 01:23:35 字数 3894 浏览 7 评论 0原文

如何使用 Hessian 二进制协议创建文件上传/下载 servlet?对于大于 1GB 的文件。我为 具有大型二进制数据的 Hessian 创建测试,但如果文件大于可用 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)

how to create file Upload/Downlod servlet using Hessian binary protocol? for files larger than 1GB. I create test for Hessian with large binary data, but if file is more than available RAM size exception is thrown java.lang.OutOfMemoryError.

//api for client

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

//implementation

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

//client call

    HessianProxyFactory proxy = new HessianProxyFactory();

    try {

//call proxy for Upload

      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

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自由如风 2024-11-09 01:23:35

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

,如 hessian 文档所述:

客户端需要直接控制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();
          //.....
    }
}

you should not use Proxy to send large files

as the hessian document says:

the client needs to take control of the Hessian protocol directly, because the proxy interface would require buffering the entire file before the call returns.

so , you could try this (i haven't test it)

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();
          //.....
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文