Springmvc文件上传例子,上传带图片的Excel,并利用poi解析。

直奔主题,第一步:上传一个带图片的Excel。第二步:解析该Excel文件,得到Excel数据和图片。
1.pom.xml

		<!-- 文件上传 -->
		<dependency>
			<groupId>commons-fileupload</groupId>
			<artifactId>commons-fileupload</artifactId>
			<version>1.3.2</version>
		</dependency>
		<!-- 解析excel -->
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-ooxml</artifactId>
			<version>3.11</version>
		</dependency>

2.spring-mvc.xml

	<!-- 文件上传解析器 -->
	<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<!-- 简单配置一个编码格式 -->
		<property name="defaultEncoding" value="UTF-8"></property>
		<!-- 设定文件上传的最大值为5MB,5*1024*1024
		<property name="maxUploadSize" value="5242880"></property> -->
		<!-- 设定文件上传时写入内存的最大值,如果小于这个参数不会生成临时文件,默认为10240 
		<property name="maxInMemorySize" value="40960"></property>-->
		<!-- 上传文件的临时路径 
		<property name="uploadTempDir" value="//"></property>-->
		<!-- 延迟文件解析 
		<property name="resolveLazily" value="true" />-->
	</bean>

3.简单超low jsp页面 ,一个上传form表单

	<form action="file/importExcel" method="post"
		enctype="multipart/form-data">
		<input name="file" type="file"> <input type="submit"
			value="upload">
	</form>

4.FileAction控制器

@RequestMapping("/file")
@RestController
public class FileAction {

	@RequestMapping(value = "/importExcel", method = RequestMethod.POST)
	public String importExcel(@RequestParam(value = "file", required = false) MultipartFile uploadFile)
			throws IOException {
		File target = UploadUtil.upLoadFile(uploadFile);// 文件上传
		UploadUtil.analysisExcel(target);//如果只要文件上传,执行完上一步就文件上传成功了。这一步解析Excel文件
		return "upload success";
	}
}

5.UploadUtils工具类,处理文件上传和解析excel文件

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFPicture;
import org.apache.poi.hssf.usermodel.HSSFPictureData;
import org.apache.poi.hssf.usermodel.HSSFShape;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

import sun.misc.BASE64Decoder;

public class UploadUtils{

	private static final Logger logger = LoggerFactory.getLogger(UploadUtil.class);

	/**
	 * 文件上传
	 * 
	 * @param uploadFile
	 * @throws IOException
	 */
	public static File upLoadFile(MultipartFile uploadFile) throws IOException {
		String uploadPath = "E://";
		File uploadFlie = new File(uploadPath);
		if (!uploadFlie.exists()) {
			uploadFlie.mkdir();
		}
		MultipartFile file = uploadFile;
		String uploadFileName = file.getOriginalFilename();
		InputStream isRef = file.getInputStream();
		File targetFile = new File(uploadPath, uploadFileName);
		FileOutputStream fosRef = new FileOutputStream(targetFile);
		IOUtils.copy(isRef, fosRef);
		return targetFile;
	}

	/**
	 * 解析excel
	 * 
	 * @param target
	 * @throws IOException
	 */
	@SuppressWarnings("resource")
	public static void analysisExcel(File target) throws IOException {
		try {
			InputStream excelFile = new FileInputStream(target);// 输入流
			HSSFWorkbook wb = new HSSFWorkbook(excelFile);// 得到Excel工作簿对象
			final Sheet sheet = wb.getSheetAt(0);// 得到Excel工作表对象
			final HSSFSheet hssfSheet = wb.getSheetAt(0);
			ExecutorService executorService = Executors.newFixedThreadPool(5);
			executorService.execute(new Runnable() {

				@Override
				public void run() {
					try {
						readPictures(hssfSheet);
					} catch (IOException e) {
						logger.error("e", e);
					}
				}
			});
			executorService.execute(new Runnable() {

				@Override
				public void run() {
					try {
						readData(sheet);
					} catch (Exception e) {
						logger.error("e", e);
					}
				}
			});
		} catch (Exception e) {
			logger.error("e", e);
		}
	}

	/**
	 * 读取excel内容
	 * 
	 * @param sheet
	 */
	public static void readData(Sheet sheet) {
		try {
			String name = "", phone = "";
			int rowNum = sheet.getLastRowNum() + 1;
			for (int i = 1; i < rowNum; i++) {// 从第二行开始读取数据,第一行是表头
				try {
					Row row = sheet.getRow(i);// 得到Excel工作表的行
					int cellNum = row.getLastCellNum();// 取得一行的有效单元格个数
					for (int j = 0; j < cellNum; j++) {
						Cell cell = row.getCell(j);// 得到Excel工作表指定行的单元格
						String cellValue = null;
						if (cell != null && cell.getCellType() != HSSFCell.CELL_TYPE_BLANK) {
							/*
							 * CELL_TYPE_NUMERIC 数值型 0 CELL_TYPE_STRING 字符串型 1
							 * CELL_TYPE_FORMULA 公式型 2 CELL_TYPE_BLANK 空值 3
							 * CELL_TYPE_BOOLEAN 布尔型 4 CELL_TYPE_ERROR 错误 5
							 */
							switch (cell.getCellType()) { // 判断excel单元格内容的格式,并对其进行转换,以便插入数据库
							case 0:
								cellValue = String.valueOf((long) cell.getNumericCellValue());
								break;
							case 1:
								cellValue = cell.getStringCellValue();
								break;
							case 2:
								cellValue = String.valueOf((int) cell.getNumericCellValue());
								break;
							case 3:
								cellValue = cell.getStringCellValue();
								break;
							case 4:
								cellValue = cell.getStringCellValue();
								break;
							}

							switch (j) {// 通过列数来判断对应插如的字段
							case 0:
								name = cellValue;
								break;
							case 1:
								phone = cellValue;
								break;
							}
						}
					}
					logger.info("第{}行,姓名:{},手机号:{}", i, name, phone);
				} catch (Exception e) {
					logger.error("e", e);
				}
			}
		} catch (Exception e) {
			logger.error("e", e);
		}
	}

	/**
	 * 获得excel中所有图片
	 * 
	 * @param sheet
	 * @throws IOException
	 */
	public static void readPictures(HSSFSheet sheet) throws IOException {
		try {
			List<HSSFShape> list = sheet.getDrawingPatriarch().getChildren();
			for (HSSFShape shape : list) {
				if (shape instanceof HSSFPicture) {
					HSSFPicture picture = (HSSFPicture) shape;
					HSSFPictureData pdata = picture.getPictureData();
					String extension = pdata.suggestFileExtension();
					GenerateImage(byteToBase64String(pdata.getData()), ((HSSFPicture) shape).getFileName(), extension);
				}
			}
		} catch (Exception e) {
			logger.error("e", e);
		}
	}

	/**
	 * 二进制数据转化为BASE64字符串
	 * 
	 * @param b
	 * @return
	 */
	public static String byteToBase64String(byte[] b) {
		return new String(Base64.encodeBase64(b));
	}

	/**
	 * 生成图片
	 * 
	 * @param imgStr
	 * @param name
	 *            图片名
	 * @param extension
	 *            后缀、扩展名
	 * @return
	 */
	public static boolean GenerateImage(String imgStr, String name, String extension) {
		try {
			if (imgStr == null)
				return false;
			BASE64Decoder decoder = new BASE64Decoder();
			try {
				// Base64解码
				byte[] b = decoder.decodeBuffer(imgStr);
				for (int i = 0; i < b.length; ++i) {
					if (b[i] < 0) {// 调整异常数据
						b[i] += 256;
					}
				}
				String path = "E://";
				String imgFilePath = path + name + "." + extension;// 新生成的图片
				logger.info("生成的图片:{}", imgFilePath);
				OutputStream out = new FileOutputStream(imgFilePath);
				out.write(b);
				out.flush();
				out.close();
				return true;
			} catch (Exception e) {
				return false;
			}
		} catch (Exception e) {
			logger.error("e", e);
		}
		return false;
	}
}

6.运行项目,输入地址http://localhost:8080/ssm-example/upload.jsp,出现一个很简陋的文件上传页面
在这里插入图片描述
上传Excel文件,Excel里面的数据为
在这里插入图片描述
在这里插入图片描述
点击upload上传文件,控制台打印出运行结果
在这里插入图片描述
再去E盘查看文件,可以看到刚才上传的文档和导出Excel文档里面的图片,图片的名字就是图片拉入Excel前的名字。
在这里插入图片描述

讲到这里,本篇文章到此也就结束了,中间可能存在一些问题,有一些不够严谨完善的地方,希望大家体谅体谅。有问题的话,欢迎大家留意,交流交流。

最后附上本项目github的地址https://github.com/Albert-WuBinBin/ssm-example,可以clone下来,运行上面的访问地址,直接访问就可以了。

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值