Java large file upload details and example code

  • 2020-06-03 06:40:46
  • OfStack

Java large file upload details

Preface:

Last week, I encountered such a problem that the client failed to upload hd video (above 1G).

At first, I thought session was out of date or the file size was limited by the system. When I looked at the configuration file of the system, I did not see the file size limit. In ES10en.xml, seesiontimeout was 30. I changed it to 120. But it still doesn't work. Sometimes it breaks in 10 minutes.

Colleague said, it may be the client here server network fluctuation caused the network connection disconnect, I think a bit of truth. However, When I tested it locally, I found that the upload also failed and the network reason was ruled out.

After reading the log, the error is:


java.lang.OutOfMemoryError Java heap space

The upload code is as follows:


public static String uploadSingleFile(String path,MultipartFile file) {
   
  if (!file.isEmpty()) {
     
      byte[] bytes;
      try {
        bytes = file.getBytes();
         
        // Create the file on server
        File serverFile = createServerFile(path,file.getOriginalFilename());
        BufferedOutputStream stream = new BufferedOutputStream(
            new FileOutputStream(serverFile));
        stream.write(bytes);
        stream.flush();
        stream.close();
 
        logger.info("Server File Location="
            + serverFile.getAbsolutePath());
 
        return getRelativePathFromUploadDir(serverFile).replaceAll("\\\\", "/");
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        System.out.println(e.getMessage());
      }
     
  }else{
    System.out.println(" The file content is empty ");
  }
  return null;  
}

At first glance it doesn't look like a big deal. I'm in ES24en. write(bytes); I didn't get there at all. But in bytes = file.getBytes (); I got an error.

The problem is that the file is too large and the number of bytes exceeds the maximum value of Integer (Bytes[] array).

In this case, just read file 1 dot in.

Modify the upload code as follows:


public static String uploadSingleFile(String path,MultipartFile file) {
   
  if (!file.isEmpty()) {
     
      //byte[] bytes;
      try {
        //bytes = file.getBytes();
         
        // Create the file on server
        File serverFile = createServerFile(path,file.getOriginalFilename());
        BufferedOutputStream stream = new BufferedOutputStream(
            new FileOutputStream(serverFile));
        int length=0;
        byte[] buffer = new byte[1024];
        InputStream inputStream = file.getInputStream();
        while ((length = inputStream.read(buffer)) != -1) {
          stream.write(buffer, 0, length);
        }
        //stream.write(bytes);
        stream.flush();
        stream.close();
 
        logger.info("Server File Location="
            + serverFile.getAbsolutePath());
 
        return getRelativePathFromUploadDir(serverFile).replaceAll("\\\\", "/");
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        System.out.println(e.getMessage());
      }
     
  }else{
    System.out.println(" The file content is empty ");
  }
  return null;  
}

Thank you for reading, I hope to help you, thank you for your support to this site!


Related articles: