栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java用multipartFile接口上传和读取图片及设置上传图片大小

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java用multipartFile接口上传和读取图片及设置上传图片大小

业务需求:

我们在用springBoot框架开发,有时候会上传图片和读取图片,那么我们,需要用multipartFile接口来上传图片,用multipartFile上传时会有限制,最大为1M因此我们需要对上传的大小进行设置,设置文件大小的逻辑在下面代码中

上传图片
@Override
    public ResponseModel updateConfig(String key, String value, MultipartFile file, String creator, int valueType) {
        try {
            if (valueType != 0) {
                OutputStream os = null;
                InputStream inputStream = null;
                try {
                    inputStream = file.getInputStream();
                    String fileName = file.getOriginalFilename();
                    String path = "F:\Image";
                    // 2、保存到临时文件
                    // 1K的数据缓冲
                    byte[] bs = new byte[1024];
                    // 读取到的数据长度
                    int len;
                    // 输出的文件流保存到本地文件
                    File tempFile = new File(path);
                    if (!tempFile.exists()) {
                        tempFile.mkdirs();
                    }
                    os = new FileOutputStream(tempFile.getPath() + File.separator + fileName);
                    // 开始读取
                    while ((len = inputStream.read(bs)) != -1) {
                        os.write(bs, 0, len);
                    }
                    //获取上传图片的路径
                    value = tempFile.getPath() + File.separator + fileName;
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    // 完毕,关闭所有链接
                    os.close();
                    inputStream.close();
                }
            }
            //把对应的数据存入数据库
            int i = tSpecialConfigMapper.updateConfig(value, creator, key);
            if (i > 0) {
                return ResponseModel.ok("修改成功");
            } else {
                return ResponseModel.ok("修改失败");
            }
        } catch (Exception e) {
            return ResponseModel.error(e.getMessage(), e);
        }
    }
读取图片
@Override
    public void readFile(HttpServletResponse response, String fileUrl) throws IOException {
        try {
            File file = new File(fileUrl);
            if (! file.exists()){
                response.sendError(404,"找不到文件");
            }else {
                FileInputStream in = new FileInputStream(file);
                byte[] buf = new byte[in.available()];
                in.read(buf);
                response.setContentType("image/jpg");
                OutputStream toClient = response.getOutputStream();
                toClient.write(buf);
                toClient.flush();
                toClient.close();
                in.close();
            }
        } catch (Exception e) {
            response.sendError(500,e.getMessage());
        }
    }
设置图片大小
@Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        //单个文件最大
        //factory.setMaxFileSize(DataSize.ofMegabytes(80)); //MB
        //factory.setMaxFileSize(DataSize.ofKilobytes(80)); //KB
        factory.setMaxFileSize(DataSize.ofGigabytes(5)); //Gb
        /// 设置总上传数据总大小
        //factory.setMaxRequestSize(DataSize.ofKilobytes(100));  //KB
        factory.setMaxRequestSize(DataSize.ofMegabytes(100));  //MB
        //factory.setMaxRequestSize(DataSize.ofGigabytes(100));  //Gb
        return factory.createMultipartConfig();
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/922322.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号