如今有個上傳文件功能,須要將文件上傳到oss上,可是文件有點多,因而使用接口進行上傳。可是須要上傳文件轉換爲MultipartFile類型文件進行上傳。spring
<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 "成功"; }