Java将多部分文件转换为大文件

utugiqy6  于 2022-12-25  发布在  Java
关注(0)|答案(3)|浏览(200)

我使用下面的方法来转换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;
}

这工作正常,但对于大文件,我得到了这个例外:
java.lang.OutOfMemoryError: Java堆空间
我添加了更多堆,但仍然出现错误。
那么有没有一种编程的方法来解决这个问题,比如在转换的时候把多部分文件分割成更小的块,但是我不知道如何编码。
任何帮助或建议将不胜感激。

dvtswwa3

dvtswwa31#

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;
  }
szqfcxe2

szqfcxe22#

下面的代码将帮助您将文件划分为块,然后您必须聚合

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);
            }

        }
56lgkhnf

56lgkhnf3#

如何使用transferTo:

public File convert(MultipartFile file) throws IOException 
 {
  File convFile = new File(file.getOriginalFilename());
  file.transferTo(convFile);
  return convFile;
}

相关问题