如何使用 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)