gpt4 book ai didi

java - 具有大型二进制文件的 Hessian

转载 作者:行者123 更新时间:2023-12-03 20:21:14 30 4
gpt4 key购买 nike

如何使用 Hessian 二进制协议(protocol)创建文件上传/下载 servlet?对于大于 1GB 的文件。我为 Hessian with large binary data 创建测试, 但如果文件超过可用 RAM 大小,则会抛出 java.lang.OutOfMemoryError 异常。

//客户端接口(interface)

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说:

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.

所以,你可以试试这个(我还没有测试过)

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();
//.....
}
}

关于java - 具有大型二进制文件的 Hessian,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5701359/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com