具有大型二进制文件的 Hessian
如何使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不应该使用代理发送大文件
,如 hessian 文档所述:
所以,你可以尝试这个(我没有测试过)
you should not use Proxy to send large files
as the hessian document says:
so , you could try this (i haven't test it)