zoukankan      html  css  js  c++  java
  • InputStream转MultipartFile

    原文:https://blog.csdn.net/niugang0920/article/details/109337641

        //调用放
        MultipartFile multipartFile = getMultipartFile(inputStream, originalFilename);
    
     /**
         * 获取封装得MultipartFile
         *
         * @param inputStream inputStream
         * @param fileName    fileName
         * @return MultipartFile
         */
        public MultipartFile getMultipartFile(InputStream inputStream, String fileName) {
            FileItem fileItem = createFileItem(inputStream, fileName);
            //CommonsMultipartFile是feign对multipartFile的封装,但是要FileItem类对象
            return new CommonsMultipartFile(fileItem);
        }
    
    
        /**
         * FileItem类对象创建
         *
         * @param inputStream inputStream
         * @param fileName    fileName
         * @return FileItem
         */
        public FileItem createFileItem(InputStream inputStream, String fileName) {
            FileItemFactory factory = new DiskFileItemFactory(16, null);
            String textFieldName = "file";
            FileItem item = factory.createItem(textFieldName, MediaType.MULTIPART_FORM_DATA_VALUE, true, fileName);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            OutputStream os = null;
            //使用输出流输出输入流的字节
            try {
                os = item.getOutputStream();
                while ((bytesRead = inputStream.read(buffer, 0, 8192)) != -1) {
                    os.write(buffer, 0, bytesRead);
                }
                inputStream.close();
            } catch (IOException e) {
                log.error("Stream copy exception", e);
                throw new IllegalArgumentException("文件上传失败");
            } finally {
                if (os != null) {
                    try {
                        os.close();
                    } catch (IOException e) {
                        log.error("Stream close exception", e);
    
                    }
                }
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (IOException e) {
                        log.error("Stream close exception", e);
                    }
                }
            }
    
            return item;
        }
  • 相关阅读:
    dhcp服务
    lvm逻辑卷扩容报错解决
    xshell连接linux使用vim无法正常使用小键盘
    OracleXETNSListener无法启动或启动停止
    RF常用关键字
    pytest的初始化清除操作
    pytest特点与执行
    flask 简单示例
    python操作redis
    centos7安装redis
  • 原文地址:https://www.cnblogs.com/zagwk/p/15718447.html
Copyright © 2011-2022 走看看