multipartfile转excel_读取本地文件转化成MultipartFile

介绍

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

主要代码

添加pom文件

org.springframework

spring-test

5.1.7.RELEASE

compile

org.apache.commons

commons-io

1.3.2

上传文件代码

@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 p

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,需要将OpenCV处理后的图片保存为Excel可以识别的格式,比如JPEG或PNG。 Java中可以使用ImageIO类读取图片并换格式,示例代码如下: ```java // 读取OpenCV处理后的图片 Mat mat = Imgcodecs.imread("path/to/image.jpg"); // 换为BufferedImage BufferedImage bufferedImage = new BufferedImage(mat.width(), mat.height(), BufferedImage.TYPE_3BYTE_BGR); mat.get(0, 0, ((DataBufferByte) bufferedImage.getRaster().getDataBuffer()).getData()); // 保存为PNG格式 ImageIO.write(bufferedImage, "png", new File("path/to/image.png")); ``` 接下来,可以使用Apache POI库创建Excel文件,并将换后的图片插入到Excel文件中,示例代码如下: ```java // 创建Excel工作簿 Workbook workbook = new XSSFWorkbook(); // 创建Excel工作表 Sheet sheet = workbook.createSheet("Sheet1"); // 读取换后的图片 File imageFile = new File("path/to/image.png"); // 将图片插入到Excel文件中 InputStream inputStream = new FileInputStream(imageFile); byte[] bytes = IOUtils.toByteArray(inputStream); int pictureIdx = workbook.addPicture(bytes, Workbook.PICTURE_TYPE_PNG); inputStream.close(); CreationHelper helper = workbook.getCreationHelper(); Drawing drawing = sheet.createDrawingPatriarch(); ClientAnchor anchor = helper.createClientAnchor(); anchor.setCol1(0); anchor.setRow1(0); Picture pict = drawing.createPicture(anchor, pictureIdx); pict.resize(); // 保存Excel文件 FileOutputStream fileOutputStream = new FileOutputStream("path/to/excel.xlsx"); workbook.write(fileOutputStream); fileOutputStream.close(); workbook.close(); ``` 最后,可以使用Spring框架提供的MultipartFile接口,将生成的Excel文件换成MultipartFile对象用于上传到服务器,示例代码如下: ```java // 读取生成的Excel文件 File excelFile = new File("path/to/excel.xlsx"); // 创建MultipartFile对象 MultipartFile multipartFile = new MockMultipartFile(excelFile.getName(), new FileInputStream(excelFile)); ``` 通过以上步骤,就可以将OpenCV处理后的图片格式换为Excel文件,并且将其换为MultipartFile对象用于上传。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值