gpt4 book ai didi

java - 无法使用spring将大文件上传到tomcat

转载 作者:行者123 更新时间:2023-11-28 22:18:27 25 4
gpt4 key购买 nike

我有一个将视频文件上传到我的服务器的应用程序,但上次尝试加载 4GB 的视频文件并上传该文件时总共只有 368Mb,共 4GB。

这是 Controller 的代码。

@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
public @ResponseBody List<String> handleFileUpload(@RequestParam("idClient") String idClient,
@RequestParam("idChannel") String idChannel, @RequestParam("idPlayList") String idPlayList,
MultipartHttpServletRequest request, HttpServletResponse response) {

StringBuffer sb = new StringBuffer();
List<String> response = new ArrayList<String>();
Iterator<String> itr = request.getFileNames();
MultipartFile file = null;

while (itr.hasNext()) {

file = request.getFile(itr.next());

if (!file.isEmpty()) {

sb.setLength(0);

sb.append(tmpFolder).append(idClient).append("_").append(idChannel).append("_").append(idPlayList)
.append("_").append(file.getOriginalFilename());

try {

byte[] bytes = file.getBytes();

BufferedOutputStream stream = new BufferedOutputStream(
new FileOutputStream(new File(sb.toString())));

stream.write(bytes);

stream.close();

response.add("File " + file.getOriginalFilename() + " uploading.");

} catch (IOException e) {

response.add("File " + file.getOriginalFilename() + " not upload.");

}

} else {

response.add("File no subido " + file.getOriginalFilename() + " file is empty ! .");

}

}

return response;
}

那会发生吗?

感谢您的帮助!

最佳答案

我解决了!谢谢问题是,当您加载超过 2GB 的文件时,超过了字节数组可以支持的最大大小,因此文件没有完全解决这个问题,这会创建一个缓冲区,将文件读入 block 或 block 。

public void copyPartsBufferFile(InputStream input, Integer bufferSize, String fullPathDetiny) {

BufferedOutputStream stream = null;

try {

stream = new BufferedOutputStream(new FileOutputStream(new File(fullPathDetiny)));

ReadableByteChannel rbc = Channels.newChannel(input);
ByteBuffer buffer = ByteBuffer.allocate(bufferSize);
byte[] bytes;

while (rbc.read(buffer) >= 0) {
buffer.flip();
bytes = buffer.array();
stream.write(bytes);
buffer.clear();
}

} catch (FileNotFoundException e) {

} catch (IOException e) {

} catch (Exception e) {

} finally {

}

}

我留下我的代码以防有人发生同样的情况!

问候。

关于java - 无法使用spring将大文件上传到tomcat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33528642/

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