我有一个Java程序,可以将文件从本地上传到Minio浏览器。文件大小约为900 MB。当我执行Java程序时,我得到-
Java.lang.OutOfMemoryError - Java heap Size
我尝试在eclipse.ini中和Run-->Configurations-->Project to -Xms4096M -Xmx8192M.
下都增加堆大小
在执行我收到的程序时增加堆大小后-
java.lang.OutOfMemoryError: Requested array size exceeds VM limit
如何使用Java将大型文件上传到Minio?
这是我的Java程序的样子-
StringBuilder stringBuilder = new StringBuilder();
File[] files = new File(path).listFiles();
showFiles(files);
System.out.println(pathList);
ListIterator<String> itr=pathList.listIterator();
while(itr.hasNext()){
String relativePath=itr.next();
if(relativePath!=null) {
String absolutePath=path+(relativePath).replaceFirst("minio_files", "");
System.out.println(absolutePath);
System.out.println(relativePath);
File f =new File(absolutePath);
BufferedReader reader = new BufferedReader(new FileReader(f));
String line = null;
String ls = System.getProperty("line.separator");
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
stringBuilder.append(ls);
}
if(stringBuilder.length()!=0) {
// delete the last new line separator
stringBuilder.deleteCharAt(stringBuilder.length() - 1);
}
reader.close();
// Create a InputStream for object upload.
ByteArrayInputStream bais = new ByteArrayInputStream(stringBuilder.toString().getBytes("UTF-8"));
答案 0 :(得分:1)
您绝对需要从文本文件中删除尾行分隔符吗?
如果这不是绝对必要的,则可以让minio客户端库透明地处理上载:
String absolutePath=path+(relativePath).replaceFirst("minio_files", "");
File f =new File(absolutePath);
minio.putObject("bucketName", f.getName(), absolutePath);
根据minio文档,这允许上传最多5 GB。与任何其他解决方案相比,它更易于实现且速度更快。
如果绝对需要删除尾行分隔符,则至少应预先设置StringBuilder
的大小(并使用正确的代码删除尾行分隔符):
File f = new File(absolutePath);
stringBuilder.ensureCapacity((int) f.length()+2);
try (BufferedReader reader = new BufferedReader(new FileReader(f))) {
String line;
String ls = System.getProperty("line.separator");
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
stringBuilder.append(ls);
}
if (stringBuilder.length() != 0) {
// delete the last new line separator
stringBuilder.setLength(stringBuilder.length() - ls.length());
}
}
请注意,此代码永远不能上传大于2GB的文件:
Integer.MAX_VALUE-5
StringBuilder
不能用于创建包含超过Integer.MAX_VALUE-5
个字符的字符串UTF-8
编码的字节数组不能产生超过Integer.MAX_VALUE-5
个字节的字节数组Integer.MAX_VALUE-5
个字符的字符串转换为字节数组