• java大文件上传


    上次遇到这样一个问题,客户上传高清视频(1G以上)的时候上传失败。

    一开始以为是session过期或者文件大小受系统限制,导致的错误。
    查看了系统的配置文件没有看到文件大小限制,
    web.xml中seesiontimeout是30,我把它改成了120。
    但还是不行,有时候10分钟就崩了。

    同事说,可能是客户这里服务器网络波动导致网络连接断开,我觉得有点道理。
    但是我在本地测试的时候发觉上传也失败,网络原因排除。

    看了日志,错误为:
    java.lang.OutOfMemoryError Java heap space
    上传文件代码如下:

    1.     public static String uploadSingleFile(String path,MultipartFile file) {
    2.         
    3.         if (!file.isEmpty()) {
    4.             
    5.                 byte[] bytes;
    6.                 try {
    7.                     bytes = file.getBytes();
    8.                     
    9.                     // Create the file on server
    10.                     File serverFile = createServerFile(path,file.getOriginalFilename());
    11.                     BufferedOutputStream stream = new BufferedOutputStream(
    12.                             new FileOutputStream(serverFile));
    13.                     stream.write(bytes);
    14.                     stream.flush();
    15.                     stream.close();
    16.     
    17.                     logger.info("Server File Location="
    18.                             + serverFile.getAbsolutePath());
    19.  
    20.                     return getRelativePathFromUploadDir(serverFile).replaceAll("\\", "/");
    21.                 } catch (IOException e) {
    22.                     // TODO Auto-generated catch block
    23.                     e.printStackTrace();
    24.                     System.out.println(e.getMessage());
    25.                 }
    26.             
    27.         }else{
    28.             System.out.println("文件内容为空");
    29.         }
    30.         return null;    
    31.     }

    乍一看没什么大问题,我在 stream.write(bytes); 这句加了断点,发觉根本就没走到。
    而是在 bytes = file.getBytes(); 就报错了。

    原因应该是文件太大的话,字节数超过Integer(Bytes[]数组)的最大值,导致的问题。
    既然这样,把文件一点点的读进来即可。

    修改上传代码如下:

    1.    public static String uploadSingleFile(String path,MultipartFile file) {
    2.         
    3.         if (!file.isEmpty()) {
    4.             
    5.                 //byte[] bytes;
    6.                 try {
    7.                     //bytes = file.getBytes();
    8.                     
    9.                     // Create the file on server
    10.                     File serverFile = createServerFile(path,file.getOriginalFilename());
    11.                     BufferedOutputStream stream = new BufferedOutputStream(
    12.                             new FileOutputStream(serverFile));
    13.                     int length=0;
    14.                     byte[] buffer = new byte[1024];
    15.                     InputStream inputStream = file.getInputStream();
    16.                     while ((length = inputStream.read(buffer)) != -1) {
    17.                         stream.write(buffer, 0, length);
    18.                     }
    19.                     //stream.write(bytes);
    20.                     stream.flush();
    21.                     stream.close();
    22.     
    23.                     logger.info("Server File Location="
    24.                             + serverFile.getAbsolutePath());
    25.  
    26.                     return getRelativePathFromUploadDir(serverFile).replaceAll("\\", "/");
    27.                 } catch (IOException e) {
    28.                     // TODO Auto-generated catch block
    29.                     e.printStackTrace();
    30.                     System.out.println(e.getMessage());
    31.                 }
    32.             
    33.         }else{
    34.             System.out.println("文件内容为空");
    35.         }
    36.         return null;    
    37.     }

     效果截图展示:


     详细代码可以参考一下这篇文章:http://blog.ncmem.com/wordpress/2019/08/09/java%e5%a4%a7%e6%96%87%e4%bb%b6%e4%b8%8a%e4%bc%a0/

  • 相关阅读:
    java学习day28-jQuery(常用)
    在博客园上添加bilibili视频
    参数传递 实参
    linux 下office软件推荐
    桥后总结 二
    go基础笔记-包
    Linux:Day24(上)
    Linux:Day23(下) vsftpd
    Linux:Day23(上) MariaDB SQL语句
    Linux:Day22(下) php及mysql使用基础
  • 原文地址:https://www.cnblogs.com/songsu/p/11356111.html
Copyright © 2020-2023  润新知