Java 将 MultipartFile 转换为大型文件的文件
Java Convert MultipartFile to File for large files
我正在使用以下方法将 MultipartFile 转换为文件:
public File convert(MultipartFile file) throws IOException {
File convFile = new File(file.getOriginalFilename());
convFile.createNewFile();
FileOutputStream fos = new FileOutputStream(convFile);
fos.write(file.getBytes());
fos.close();
return convFile;
}
Wich 工作正常,但对于大文件我遇到了这个异常:
java.lang.OutOfMemoryError: Java heap space
我添加了更多的堆但仍然有错误。
那么有没有一种编程方法可以解决这个问题,可能是在转换时将多部分文件拆分成更小的块,但我不确定如何编码。
如有任何帮助或建议,我们将不胜感激。
MultipartFile
是否属于 Spring 的包 org.springframework.web.multipart
?如果是这样,你可以做
public File convert(MultipartFile file) throws IOException {
File convFile = new File(file.getOriginalFilename());
convFile.createNewFile();
try(InputStream is = file.getInputStream()) {
Files.copy(is, convFile.toPath());
}
return convFile;
}
下面的代码将帮助您将文件分成块,然后您必须聚合
try
{
file = new File(filePath.toString());
messageDigest = MessageDigest.getInstance("MD5");
checksum = getFileCheckSum(messageDigest, file);
fileInputStream = new FileInputStream(file);
int fileSize = (int) file.length();
fileChannel = fileInputStream.getChannel();
int numberOfChunks = (int) Math.ceil(fileChannel.size() / (double) chunkSize);
int totalpacket = numberOfChunks;
int totalFileSize = fileSize;
int read = 0;
while (numberOfChunks > 0)
{
System.out.println("file is :" + file + "checksum is:" + checksum);
fileSize -= read;
if (numberOfChunks > 1)
{
ByteBuffer bytebuffer = ByteBuffer.allocate(chunkSize);
read = fileChannel.read(bytebuffer);
bytebuffer.flip();
String content = new String();
if (bytebuffer.hasRemaining())
{
content = new String(bytebuffer.array(), Charset.forName("UTF-8"));
}
bytebuffer.clear();
}
else
{
String chunkData = new String();
ByteBuffer byteBuffer = ByteBuffer.allocate(fileSize);
fileChannel.read(byteBuffer);
chunkData = new String(byteBuffer.array(), Charset.forName("UTF-8"));
byteBuffer.clear();
}
numberOfChunks--;
}
}
catch (IOException e)
{
log.info(e);
}
catch (NoSuchAlgorithmException nsae)
{
log.info(nsae);
}
finally
{
try
{
fileInputStream.close();
fileChannel.close();
}
catch (IOException ioe)
{
log.info(ioe);
}
}
如何使用 transferTo:
public File convert(MultipartFile file) throws IOException
{
File convFile = new File(file.getOriginalFilename());
file.transferTo(convFile);
return convFile;
}
我正在使用以下方法将 MultipartFile 转换为文件:
public File convert(MultipartFile file) throws IOException {
File convFile = new File(file.getOriginalFilename());
convFile.createNewFile();
FileOutputStream fos = new FileOutputStream(convFile);
fos.write(file.getBytes());
fos.close();
return convFile;
}
Wich 工作正常,但对于大文件我遇到了这个异常:
java.lang.OutOfMemoryError: Java heap space
我添加了更多的堆但仍然有错误。
那么有没有一种编程方法可以解决这个问题,可能是在转换时将多部分文件拆分成更小的块,但我不确定如何编码。
如有任何帮助或建议,我们将不胜感激。
MultipartFile
是否属于 Spring 的包 org.springframework.web.multipart
?如果是这样,你可以做
public File convert(MultipartFile file) throws IOException {
File convFile = new File(file.getOriginalFilename());
convFile.createNewFile();
try(InputStream is = file.getInputStream()) {
Files.copy(is, convFile.toPath());
}
return convFile;
}
下面的代码将帮助您将文件分成块,然后您必须聚合
try
{
file = new File(filePath.toString());
messageDigest = MessageDigest.getInstance("MD5");
checksum = getFileCheckSum(messageDigest, file);
fileInputStream = new FileInputStream(file);
int fileSize = (int) file.length();
fileChannel = fileInputStream.getChannel();
int numberOfChunks = (int) Math.ceil(fileChannel.size() / (double) chunkSize);
int totalpacket = numberOfChunks;
int totalFileSize = fileSize;
int read = 0;
while (numberOfChunks > 0)
{
System.out.println("file is :" + file + "checksum is:" + checksum);
fileSize -= read;
if (numberOfChunks > 1)
{
ByteBuffer bytebuffer = ByteBuffer.allocate(chunkSize);
read = fileChannel.read(bytebuffer);
bytebuffer.flip();
String content = new String();
if (bytebuffer.hasRemaining())
{
content = new String(bytebuffer.array(), Charset.forName("UTF-8"));
}
bytebuffer.clear();
}
else
{
String chunkData = new String();
ByteBuffer byteBuffer = ByteBuffer.allocate(fileSize);
fileChannel.read(byteBuffer);
chunkData = new String(byteBuffer.array(), Charset.forName("UTF-8"));
byteBuffer.clear();
}
numberOfChunks--;
}
}
catch (IOException e)
{
log.info(e);
}
catch (NoSuchAlgorithmException nsae)
{
log.info(nsae);
}
finally
{
try
{
fileInputStream.close();
fileChannel.close();
}
catch (IOException ioe)
{
log.info(ioe);
}
}
如何使用 transferTo:
public File convert(MultipartFile file) throws IOException
{
File convFile = new File(file.getOriginalFilename());
file.transferTo(convFile);
return convFile;
}