前端jsp代码:
<form name="Form" action="/upload" method="post" enctype="multipart/form-data">
<h1 style="text-align: center">数据导入</h1>
<input type="file" name="multipartFile" style="background: #E3EFFB;">
<input type="submit" value="上传" style="padding: 2px 10px";/>
</form>
后台controller接收文件
package ****.dataimport.controller;
import ****.dataimport.DataImportService;
import ****.dataimport.ZipUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Map;
@Controller
@Scope("prototype")
@RequestMapping("upload")
public class AqDataImportController {
@Autowired
DataImportService dataImportService;
/**
* 上传压缩文件
*/
@RequestMapping("/uploadPushContent")
public String uploadPushContent(MultipartFile multipartFile, HttpServletRequest request) {
try {
// 1.上传附件到项目根目录
String path = request.getSession().getServletContext().getRealPath("/");
File newFile = new File(path,"upload.zip");
multipartFile.transferTo(newFile);
//解压文件
Map<String, File> map = ZipUtils.unZip(newFile,path);
循环遍历根据解压文件名来判断调用相应的导入方法
File file = null;
for(Map.Entry<String, File> entry : map.entrySet()){
if(entry.getKey().equals("A.xlsx")){
file= (File) entry.getValue();
dataImportService.importAData(file);
//完成后将文件删除
file.delete();
}
}
//删除上传的压缩文件
newFile.delete();
return "/success";
} catch (Exception e) {
e.printStackTrace();
return "/false";
}
}
}
解压文件工具类
package ****.dataimport;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
/**
* zip解压
*
* @param srcFile
* zip源文件
* @param destDirPath
* 解压后的目标文件夹
* @throws RuntimeException
* 解压失败会抛出运行时异常
*/
public class ZipUtils
{
public static int BUFFER_SIZE = 1024;
public static Map<String,File> unZip(File srcFile, String destDirPath)
throws RuntimeException
{
Map<String,File> map = new HashMap<String,File>();
// 判断源文件是否存在
if (!srcFile.exists())
{
throw new RuntimeException(srcFile.getPath() + "所指文件不存在");
}
// 开始解压
ZipFile zipFile = null;
try
{
Charset gbk = Charset.forName("gbk");
zipFile = new ZipFile(srcFile,gbk);
Enumeration<?> entries = zipFile.entries();
while (entries.hasMoreElements())
{
ZipEntry entry = (ZipEntry)entries.nextElement();
System.out.println("解压" + entry.getName());
// 如果是文件夹,就创建个文件夹
if (entry.isDirectory())
{
String dirPath = destDirPath + "/" + entry.getName();
File dir = new File(dirPath);
dir.mkdirs();
}
else
{
// 如果是文件,就先创建一个文件,然后用io流把内容copy过去
File targetFile = new File(destDirPath + "/" + entry.getName());
// 保证这个文件的父文件夹必须要存在
if (!targetFile.getParentFile().exists())
{
targetFile.getParentFile().mkdirs();
}
targetFile.createNewFile();
// 将压缩文件内容写入到这个文件中
InputStream is = zipFile.getInputStream(entry);
FileOutputStream fos = new FileOutputStream(targetFile);
int len;
byte[] buf = new byte[BUFFER_SIZE];
while ((len = is.read(buf)) != -1)
{
fos.write(buf, 0, len);
}
map.put(entry.getName(),targetFile);
// 关流顺序,先打开的后关闭
fos.close();
is.close();
}
}
}
catch (Exception e)
{
throw new RuntimeException("unzip error from ZipUtils", e);
}
finally
{
if (zipFile != null)
{
try
{
zipFile.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
return map;
}
}
excel文件导入
package ****.dataimport;
import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
public class DataImportService {
public void importAccidentData(File file) throws Exception {
Workbook workbook = WorkbookFactory.create(new FileInputStream(file));
Sheet sheet = workbook.getSheetAt(0);
for (int i = 0; i < sheet.getLastRowNum(); i++) {
Row row = sheet.getRow(i);
//新建实体类赋值并保存数据库
}
}
}