zoukankan      html  css  js  c++  java
  • 读取本地文件转化成MultipartFile

    介绍

    现在有个上传文件功能,需要将文件上传到oss上,但是文件有点多,于是使用接口进行上传。但是需要上传文件转换为MultipartFile类型文件进行上传。

    主要代码

    添加pom文件

    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-test</artifactId>
    			<version>5.1.7.RELEASE</version>
    			<scope>compile</scope>
    		</dependency>
    		<dependency>
    			<groupId>org.apache.commons</groupId>
    			<artifactId>commons-io</artifactId>
    			<version>1.3.2</version>
    		</dependency>
    

    上传文件代码

     @RequestMapping(value = "up")
        public String upFile() {
            ExecutorService fixedThreadPool = Executors.newFixedThreadPool(10);
            String filepath = "D:\work\files";//D盘下的file文件夹的目录
            File file = new File(filepath);//File类型可以是文件也可以是文件夹
            File[] fileList = file.listFiles();//将该目录下的所有文件放置在一个File类型的数组中
            for (int j = 0; j < fileList.length; j++) {
    
    
                final int i = j;
                fixedThreadPool.execute(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            File pdf = fileList[i];
                     
                            FileInputStream fileInputStream = null;
                            fileInputStream = new FileInputStream(pdf);
    
                            MultipartFile multipartFile = new MockMultipartFile(pdf.getName(), pdf.getName(),
                                    ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
                            String url = ossFileUtils.upload(multipartFile.getOriginalFilename(), multipartFile);
                        } catch (Exception e) {
                            System.out.println(e.getMessage());
    
                        }
                    }
                });
    
    
    
    
            }
    
            return "成功";
        }
    
  • 相关阅读:
    P2522 [HAOI2011]Problem b(容斥)
    P3455 [POI2007]ZAP-Queries
    P2519 [HAOI2011]problem a(线段树优化dp+思维)
    P2516 [HAOI2010]最长公共子序列 (lcs+容斥)
    [HAOI2010]软件安装(缩点+树形dp)
    P2508 [HAOI2008]圆上的整点(神仙题)
    [SDOI2011]消防(树的直径+二分||单调队列)
    QLabel设置字体颜色
    Qt绘制不规则串口
    C++继承关系
  • 原文地址:https://www.cnblogs.com/zhenghengbin/p/11096860.html
Copyright © 2011-2022 走看看