zoukankan      html  css  js  c++  java
  • 文件上传下载

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

    一开始以为是session过期或者文件大小受系统限制,导致的错误。

    查看了系统的配置文件没有看到文件大小限制,

    web.xml中seesiontimeout是30,我把它改成了120。

    但还是不行,有时候10分钟就崩了。

    同事说,可能是客户这里服务器网络波动导致网络连接断开,我觉得有点道理。

    但是我在本地测试的时候发觉上传也失败,网络原因排除。

    看了日志,错误为:

    java.lang.OutOfMemoryError Java heap space

    上传文件代码如下:

        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("文件内容为空");

            }

            return null;    

        }

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

    而是在 bytes = file.getBytes(); 就报错了。

    原因应该是文件太大的话,字节数超过Integer(Bytes[]数组)的最大值,导致的问题。

    既然这样,把文件一点点的读进来即可。

    修改上传代码如下:


       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("文件内容为空");

            }

            return null;    

        }


    效果展示:


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

  • 相关阅读:
    利用ChromeCROSS可以在chrome浏览器上调试跨域代码
    js导出Excel表格
    css文本超出隐藏显示省略号
    JS中判断null、undefined与NaN的方法
    js实现获取当前时间是本月第几周和年的第几周的方法
    从一个点子到一个社区APP,是如何通过.NET实现的?——“文林物业系统”APP介绍及采访记录
    如何使用.net开发一款小而美的O2O移动应用? ——“家庭小秘”APP介绍及采访记录
    .NET移动开发,关于发布IOS的方法(本人亲身经历折腾很久终于成功)
    4.6预告先导篇——你们关心的几个问题:关于页、文档、内网推送等
    .Net移动开发平台 ,基于VisualStudio的可视化开发——Smobiler平台入门教程
  • 原文地址:https://www.cnblogs.com/songsu/p/11302842.html
Copyright © 2011-2022 走看看