首页 > 解决方案 > 无法用 Java 上传 200mb PDF 文件

问题描述

我能够上传具有 7MB 数据的 PDF 文件,但我无法上传超过 7MB 的数据。谁能提供如何使用 SMB 协议上传 200MB 文件同时使用 Angular 代码处理 200MB PDF 文件的示例

服务代码:

SmbFileOutputStream sfos = new SmbFileOutputStream(efbiFile);
byte[] buf = new byte[1024 * 1024 * 200];
BufferedOutputStream bufos = new BufferedOutputStream(sfos,buf.length);
final ReadableByteChannel inputChannel = 
Channels.newChannel(fileObj.getInputStream());
final WritableByteChannel outputChannel = Channels.newChannel(bufos);                             
ChannelTools.fastChannelCopy(inputChannel,outputChannel);
inputChannel.close();
outputChannel.close();

标签: java

解决方案


您很可能不想分配 200 MB 缓冲区 ( new byte[1024 * 1024 * 200])。的目的BufferedOutputStream是提高许多小写入的整体性能,而不是将整个输入存储在 RAM 中。

您可以使用InputStream.transferTo(OutputStream )方法并从默认BufferedOutputStream大小 8196 开始:

try (InputStream in = fileObj.getInputStream();
     SmbFileOutputStream sfos = new SmbFileOutputStream(efbiFile);
     BufferedOutputStream out = new BufferedOutputStream(sfos)) {
  in.transferTo(out);
  out.flush(); // In case SmbFileOutputStream doesn't implement this correctly
}

推荐阅读