Java 上传解压zip文件,并且解析文件里面的excel和图片
需求:上传一个zip文件,zip文件里面包含一个excel和很多图片,需要把excel里面的信息解析出来保存到表中,同时图片也转化成base64保存到数据库表中。
PS:为了方便不同水平的开发人员阅读,我把代码全部写到Controller里面。这个demo的file入参的类型是MultipartFile,很多网上的例子是File类型。这两个类型在解析文件的时候还是有点区别的。
第①个方法:
/**
* 这个deomo入参的类型是MultipartFile,很多网上的例子是File类型
* @param file (zip)
* @param request
* @param response
* @return
* @throws Exception
*/
@PostMapping("/addPersonsFileOfZip")
public String addPersonsFileOfZip(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws Exception {
String createdId = request.getParameter(KEY_CREATED_ID);
//正常上这里需要检查一下createdId是否为空 //原则上这个uploadZipFilesAndParse方法需要写到service和serviceImpl中
String result =uploadZipFilesAndParse(file,createdId);
return result;
}
第②个方法:
/**
*返回的是批次号
*同时我另外开了线程处理zip文件里面的图片和excel,
*/
@Override
public String uploadZipFilesAndParse(MultipartFile file, String createdId) throws Exception {
String filename = file.getOriginalFilename();
String fileType = filename.substring(filename.lastIndexOf(".") + 1).toLowerCase(Locale.US);
String uuid = UUID.randomUUID().toString();
//判断文件是不是zip类型
if(fileType.equals("zip")){ //FileConfig.localtion是配置文件和config类生产的,我会在评论区附上这些代码,测试demo的时候大家可以直接把FileConfig.localtion替换成D:/test
//String desPath = FileConfig.localtion + File.separator + uuid.replaceAll("-", "");
String desPath = "D:/test" + File.separator + uuid.replaceAll("-", ""); //下面这三行的代码就是把上传文件copy到服务器,一定不要遗漏了。
//遗漏了这个代码,在本地测试环境不会出问题,在服务器上一定会报没有找到文件的错误
String savePath = FileConfig.localtion + File.separator;
File savefile = new File(savePath+filename);
file.transferTo(savefile); FileUtil fileUtil = new FileUtil();
//解压zip文件,我是写在公共类里面,FileUtil类代码评论区见
FileUtil.unZip(file, desPath,savePath);
new Thread(new Runnable() {
@Override
public void run() {
List<File> fileList = new ArrayList<>();
fileList = fileUtil.getSubFiles(desPath,fileList);
for (File oneFile : fileList){
if (oneFile.getName().toLowerCase().endsWith(".xls") || oneFile.getName().toLowerCase().endsWith(".xlsx") ) {
try {
//解析处理excel文件
parseExcelFile(oneFile,createdId,uuid);
} catch (Exception e) {
LogUtils.error(e.getMessage());
}
}else if(oneFile.getName().toLowerCase().endsWith(".jpg")) {
try {
//解析处理图片文件
parseImageFile(oneFile,createdId,uuid);
} catch (Exception e) {
LogUtils.error(e.getMessage());
}
}
} //最后要删除文件,删除文件的方法见评论区FileUtil类
FileUtil.clearFiles(desPath); }
}).start(); }
return uuid;
}
第③个方法:解压zip文件的unzip方法
public static void unZip(MultipartFile srcFile, String destDirPath,String savePath) throws RuntimeException, IOException {
long startTime = System.currentTimeMillis();
File file = null;
InputStream ins = srcFile.getInputStream();
file=new File(savePath+srcFile.getOriginalFilename());
LogUtils.info("MultipartFile transform to File,MultipartFile name:"+srcFile.getOriginalFilename());
inputStreamToFile(ins, file);
if (!file.exists()) {
throw new RuntimeException(file.getPath() + ",file is not found");
}
ZipFile zipFile = null;
try {
zipFile = new ZipFile(file);
Enumeration<?> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry) entries.nextElement();
LogUtils.info("zipFile context name:"+entry.getName());
if (entry.isDirectory()) {
String dirPath = destDirPath + File.separator+ entry.getName();
File dir = new File(dirPath);
dir.mkdirs();
}else {
File targetFile = new File(destDirPath + File.separator + entry.getName());
targetFile.setExecutable(true);
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[1024];
while ((len = is.read(buf)) != -1) {
fos.write(buf, 0, len);
}
fos.close();
is.close();
}
}
long endTime = System.currentTimeMillis();
LogUtils.info("unZip time-->" + (endTime - startTime) + " ms");
}catch(Exception e) {
throw new RuntimeException("unzip error from FileUtil", e);
} finally {
if(zipFile != null){
try {
zipFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//MultipartFile change to file may create a temp file in the project root folder(delete the temp file)
File del = new File(file.toURI());
del.delete();
}
}
第④个方法:unzip方法中的inputStreamToFile方法,这个方法的目的是把MultipartFile转成File类型,但是会在项目根目录下生成一个临时文件,切记要删除
private static void inputStreamToFile(InputStream ins, File file) {
try {
OutputStream os = new FileOutputStream(file);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
ins.close();
LogUtils.info("MultipartFile transform to File completed!");
}catch(Exception e) {
e.printStackTrace();
}
}
第⑤个方法:parseExcelFile方法是解析excel的方法,里面包括我自己项目的逻辑处理,大家可以删除这些代码,只保留解析excel的代码就好
private void parseExcelFile(File file,String createdId,String uuid) throws Exception {
LogUtils.info("file name:"+file.getName());
FileInputStream is = new FileInputStream(file);
Workbook workbook = WorkbookFactory.create(is);
Sheet sheet = workbook.getSheetAt(0);
int firstRowIndex = sheet.getFirstRowNum() + 1;
int lastRowIndex = sheet.getLastRowNum();
List<VapBatchPersonInfo> batchPersonList = new ArrayList<>();
for (int rIndex = firstRowIndex; rIndex <= lastRowIndex; rIndex++) {
VapBatchPersonInfo vapBatchPersonInfo = new VapBatchPersonInfo();
Row row = sheet.getRow(rIndex);
if (row != null) {
int firstCellIndex = row.getFirstCellNum();
int lastCellIndex = row.getLastCellNum();
JSONObject jsonObject = new JSONObject();
jsonObject.put(KEY_CREATED_ID, createdId);
Cell resultCell = row.createCell(lastCellIndex);
Cell msgCell = row.createCell(lastCellIndex + 1);
Boolean flag = false;
for (int cIndex = firstCellIndex; cIndex < lastCellIndex; cIndex++) {
Cell cell = row.getCell(cIndex);
String titleName = sheet.getRow(0).getCell(cIndex).toString();
String checkTitleName = checkTitleName(cIndex, titleName);
if (!"SUCCESS".equals(checkTitleName)) {
msgCell.setCellValue(checkTitleName);
resultCell.setCellValue("Failed");
flag = true;
break;
}
if (cell != null) {
cell.setCellType(CellType.STRING);
jsonObject.put(titleName, cell.toString());
}
}
if (flag) {
rIndex = 0;
lastRowIndex = 0;
} else {
vapBatchPersonInfo.setBatchNo(uuid);
vapBatchPersonInfo.setName(jsonObject.getString("fullName"));
vapBatchPersonInfo.setImageName(jsonObject.getString("imageName"));
vapBatchPersonInfo.setConfidenceThreshold(jsonObject.getString("confidenceThreshold"));
vapBatchPersonInfo.setCreatedId(jsonObject.getString("createdId"));
vapBatchPersonInfo.setIdentityNo(jsonObject.getString("identityNo"));
vapBatchPersonInfo.setCreatedDate(new Date());
vapBatchPersonInfo.setLastUpdatedId(jsonObject.getString("createdId"));
vapBatchPersonInfo.setLastUpdatedDate(new Date());
vapBatchPersonInfo.setStatus(TaskStatus.RUNNING);
batchPersonList.add(vapBatchPersonInfo);
}
}
}
batchPersonInfoRepository.saveAll(batchPersonList);
}
第⑥个方法:parseImageFile方法是解析图片的方法
private void parseImageFile(File file, String createdId, String uuid) throws Exception {
String imgStr ="";
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[(int) file.length()];
int offset = 0;
int numRead = 0;
while (offset < buffer.length && (numRead = fis.read(buffer, offset, buffer.length - offset)) >= 0) {
offset += numRead;
}
if (offset != buffer.length) {
throw new IOException("Could not completely read file " + file.getName());
}
fis.close();
Base64 encoder = new Base64();
imgStr = Base64.encodeBase64String(buffer);
imgStr.length();
LogUtils.info("file name:"+file.getName());
// LogUtils.info("file imgStr:"+imgStr);
// LogUtils.info("file imgStr.length:"+imgStr.length());
}
最后附上FileConfig和FileUtil的代码
FileConfig代码:
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component; /**
*Jun 12, 2019
*
* FileConfig.java
*/
@ConfigurationProperties(prefix = "upload")
@Component
@Order
public class FileConfig { public static String localtion;
public static String maxFileSize;
public static String maxRequestSize; /**
* @param localtion the localtion to set
*/
public void setLocaltion(String localtion) {
FileConfig.localtion = localtion;
} /**
* @param maxFileSize the maxFileSize to set
*/
public void setMaxFileSize(String maxFileSize) {
FileConfig.maxFileSize = maxFileSize;
} /**
* @param maxRequestSize the maxRequestSize to set
*/
public void setMaxRequestSize(String maxRequestSize) {
FileConfig.maxRequestSize = maxRequestSize;
} }
FileConfig类里面读取的配置文件信息:
配置文件类型是yml,大家也可以自己改成properties文件格式
upload:
#localtion: ${UPLOAD_DIR:/home/data/test}
localtion: ${UPLOAD_DIR:D:/test}
maxFileSize: 10240KB
maxRequestSize: 102400KB
FileUtil类的代码:
import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.PosixFilePermission;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile; import org.springframework.web.multipart.MultipartFile; import sg.com.mha.ummi.common.util.LogUtils; public class FileUtil { public static void clearFiles(String workspaceRootPath) {
File file = new File(workspaceRootPath);
deleteFile(file);
} public static void deleteFile(File file) {
if (file.exists()) {
if (file.isDirectory()) {
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
deleteFile(files[i]);
}
}
}
file.delete();
} public static void fileWrite(String str, String fileNamePath) throws IOException {
FileWriter writer = null;
try {
File file = new File(fileNamePath);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
file.createNewFile();
}
writer = new FileWriter(file, true);
writer.write(str + System.getProperty("line.separator")); } catch (IOException e) {
LogUtils.error(e.getMessage());
} finally {
if (writer != null) {
writer.close();
}
}
} public static void changePermission(File dirFile, int mode) throws IOException {
char[] modes = Integer.toOctalString(mode).toCharArray();
if (modes.length != 3) {
return;
}
Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>();
switch (modes[0]) {
case '1':
perms.add(PosixFilePermission.OWNER_EXECUTE);
break;
case '2':
perms.add(PosixFilePermission.OWNER_WRITE);
break;
case '4':
perms.add(PosixFilePermission.OWNER_READ);
break;
case '5':
perms.add(PosixFilePermission.OWNER_READ);
perms.add(PosixFilePermission.OWNER_EXECUTE);
break;
case '6':
perms.add(PosixFilePermission.OWNER_READ);
perms.add(PosixFilePermission.OWNER_WRITE);
break;
case '7':
perms.add(PosixFilePermission.OWNER_READ);
perms.add(PosixFilePermission.OWNER_WRITE);
perms.add(PosixFilePermission.OWNER_EXECUTE);
break; default:
break;
}
switch (modes[1]) {
case '1':
perms.add(PosixFilePermission.GROUP_EXECUTE);
break;
case '2':
perms.add(PosixFilePermission.GROUP_WRITE);
break;
case '4':
perms.add(PosixFilePermission.GROUP_READ);
break;
case '5':
perms.add(PosixFilePermission.GROUP_READ);
perms.add(PosixFilePermission.GROUP_EXECUTE);
break;
case '6':
perms.add(PosixFilePermission.GROUP_READ);
perms.add(PosixFilePermission.GROUP_WRITE);
break;
case '7':
perms.add(PosixFilePermission.GROUP_READ);
perms.add(PosixFilePermission.GROUP_WRITE);
perms.add(PosixFilePermission.GROUP_EXECUTE);
break;
default:
break;
}
switch (modes[2]) {
case '1':
perms.add(PosixFilePermission.OTHERS_EXECUTE);
break;
case '2':
perms.add(PosixFilePermission.OTHERS_WRITE);
break;
case '4':
perms.add(PosixFilePermission.OTHERS_READ);
break;
case '5':
perms.add(PosixFilePermission.OTHERS_EXECUTE);
perms.add(PosixFilePermission.OTHERS_READ);
break;
case '6':
perms.add(PosixFilePermission.OTHERS_READ);
perms.add(PosixFilePermission.OTHERS_WRITE);
break;
case '7':
perms.add(PosixFilePermission.OTHERS_EXECUTE);
perms.add(PosixFilePermission.OTHERS_READ);
perms.add(PosixFilePermission.OTHERS_WRITE);
break;
default:
break;
} try {
Path path = Paths.get(dirFile.getAbsolutePath());
Files.setPosixFilePermissions(path, perms);
} catch (Exception e) {
e.printStackTrace();
}
} public static File mkFile(String fileName) {
File f = new File(fileName);
try {
if (f.exists()) {
f.delete();
}
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
return f;
} public static void copyDirAndFile(String oldPath, String newPath) throws IOException {
if (!(new File(newPath)).exists()) {
(new File(newPath)).mkdir();
}
File file = new File(oldPath);
//file name list
String[] filePaths = file.list();
for (String filePath : filePaths) {
String oldFullPath = oldPath + file.separator + filePath;
String newFullPath = newPath + file.separator + filePath;
File oldFile = new File(oldFullPath);
File newFile = new File(newFullPath);
if (oldFile.isDirectory()) {
copyDirAndFile(oldFullPath, newFullPath);
} else if (oldFile.isFile()) {
copyFile(oldFile, newFile);
}
}
} public static void copyFile(File source, File dest) throws IOException {
FileChannel inputChannel = null;
FileChannel outputChannel = null;
try {
inputChannel = new FileInputStream(source).getChannel();
outputChannel = new FileOutputStream(dest).getChannel();
outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
} finally {
inputChannel.close();
outputChannel.close();
}
} /**
* @author panchaoyuan
* @param srcFile Unzipped file
* @param destDirPath Unzipped destination folder
* @throws RuntimeException
* @throws IOException
*/
public static void unZip(MultipartFile srcFile, String destDirPath,String savePath) throws RuntimeException, IOException {
long startTime = System.currentTimeMillis(); File file = null;
InputStream ins = srcFile.getInputStream();
file=new File(savePath+srcFile.getOriginalFilename());
LogUtils.info("MultipartFile transform to File,MultipartFile name:"+srcFile.getOriginalFilename());
inputStreamToFile(ins, file); if (!file.exists()) {
throw new RuntimeException(file.getPath() + ",file is not found");
}
ZipFile zipFile = null;
try {
zipFile = new ZipFile(file);
Enumeration<?> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry) entries.nextElement();
LogUtils.info("zipFile context name:"+entry.getName());
if (entry.isDirectory()) {
String dirPath = destDirPath + File.separator+ entry.getName();
File dir = new File(dirPath);
dir.mkdirs();
}else {
File targetFile = new File(destDirPath + File.separator + entry.getName());
targetFile.setExecutable(true);
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[1024];
while ((len = is.read(buf)) != -1) {
fos.write(buf, 0, len);
}
fos.close();
is.close();
}
}
long endTime = System.currentTimeMillis();
LogUtils.info("unZip time-->" + (endTime - startTime) + " ms");
}catch(Exception e) {
throw new RuntimeException("unzip error from FileUtil", e);
} finally {
if(zipFile != null){
try {
zipFile.close();
} catch (IOException e) {
e.printStackTrace();
}
} //MultipartFile change to file may create a temp file in the project root folder(delete the temp file)
File del = new File(file.toURI());
del.delete();
}
} /**
* MultipartFile changed to File
* @author panchaoyuan
* @return
*/
private static void inputStreamToFile(InputStream ins, File file) {
try {
OutputStream os = new FileOutputStream(file);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
ins.close();
LogUtils.info("MultipartFile transform to File completed!");
}catch(Exception e) {
e.printStackTrace();
}
} /**
* @author panchaoyuan
*/
public List<File> getSubFiles(String desFile,List<File> fileList) {
File file = new File(desFile);
File[] files = file.listFiles();
for (File fileIndex : files) {
if (!fileIndex.exists()) {
throw new NullPointerException("Cannot find " + fileIndex);
} else if (fileIndex.isFile()) {
fileList.add(fileIndex);
} else {
if (fileIndex.isDirectory()) {
getSubFiles(fileIndex.getAbsolutePath(),fileList);
}
}
}
return fileList;
} }
水平有限,可能写得不是很完整,大家copy这些代码的时候有可能因为引入包的不同,不一定走得成功,如有疑问,在评论区联系本人,写得不好的地方也欢迎指正。
Java 上传解压zip文件,并且解析文件里面的excel和图片的更多相关文章
- mac通过自带的ssh连接Linux服务器并上传解压文件
需求: 1:mac连接linux服务器 2:将mac上的文件上传到linux服务器指定位置 3:解压文件 mac上使用命令,推荐使用 iterm2 .当然,也可以使用mac自带的终端工具. 操作过程: ...
- apache ant解压zip。支持多级文件夹解压
package cn.liuc.util; import java.io.File; import java.io.FileOutputStream; import java.io.IOExcepti ...
- Android 解压zip文件你知道多少?
对于Android常用的压缩格式ZIP,你了解多少? Android的有两种解压ZIP的方法,你知道吗? ZipFile和ZipInputStream的解压效率,你对比过吗? 带着以上问题,现在就开始 ...
- 使用Python解压zip、rar文件
解压 zip 文件 基本解压操作 import zipfile ''' 基本格式:zipfile.ZipFile(filename[,mode[,compression[,allowZip64]]]) ...
- java实现解压zip文件,(亲测可用)!!!!!!
项目结构: Util.java内容: package com.cfets.demo; import java.io.File; import java.io.FileOutputStream; imp ...
- Java 解压 zip 文件
代码如下 package test_java; import java.io.File; import java.io.FileOutputStream; import java.io.IOExcep ...
- edtftpj让Java上传FTP文件支持断点续传
在用Java实现FTP上传文件功能时,特别是上传大文件的时候,可以需要这样的功能:程序在上传的过程中意外终止了,文件传了一大半,想从断掉了地方继续传:或者想做类似迅雷下载类似的功能,文件太大,今天传一 ...
- java 解压 zip 包并删除
需求是这样的, 在服务器上有 运营上传的zip 包,内容是用户的照片,我需要做的是 获取这些照片上传,并保存到 数据库. 这里面的 上传照片,保存数据库都不难,主要问题是解压zip包,和删除zip ...
- Java 解压zip压缩包
因为最近项目需要批量上传文件,而这里的批量就是将文件压缩在了一个zip包里,然后读取文件进行解析文件里的内容. 因此需要先对上传的zip包进行解压.以下直接提供代码供参考: 1.第一个方法是用于解压z ...
随机推荐
- 四种为HttpClient添加默认请求报头的解决方案
HttpClient在Web调用中具有广泛的应用,而为它添加默认请求头是我们经常遇到的需求,本文介绍4种为HttpClient添加默认请求头的方式. 第一种方式 直接在创建的HttpClient对象的 ...
- 证书pfx转jks
keytool -importkeystore -srckeystore 2756649_order.hanels-home.com.pfx -srcstoretype pkcs12 -destke ...
- 深入理解static关键字
class A{ public int i = 10; public void show(){ System.out.printf("%d",i); } } class M{ pu ...
- 分层图 单调决策性DP
easy 写法. #include<bits/stdc++.h> using namespace std; #define Fopen freopen("_in.txt" ...
- bzoj2049 Cave 洞穴勘测 lct
这里比上次多了几个操作. 1. make_root(u) 换根节点, 先access(u), 再splay(u),将u移动到splay树的最顶上, 现在这棵splay对于root来说 只有左子树上有东 ...
- codeforces 701 D. As Fast As Possible(数学题)
题目链接:http://codeforces.com/problemset/problem/701/D 题意:给你n个人,每个人走路的速度v1,有一辆车速度为v2,每次可以载k个人,总路程为l,每个人 ...
- yzoj1891 最优配对问题 题解
题意 有n个点,且2|n,要求将其分为n/2对点对使得所有点对中距离之和尽量小 输出保留两位小数 考虑数据范围先想到的是搜索,然而搜索超时,我们发现在搜索的时候有重复搜索的情况,那么考虑记忆化,看到数 ...
- TLS加密远程连接Docker
<Docker远程连接设置>一文讲述了开启Docker远程连接的方法,但那种方法不安全,因为任何客户端都可以通过Docker服务的IP地址连接上去,今天我们就来学习Docker官方推荐的安 ...
- springboot打包jar包后运行
我们知道,spring boot内嵌tomcat,打包成jar包以后,直接就可以运行. 我们也可以使用启动项里面的mian入口来运行程序. 运行jar包时,我们一般是java -jar xxx.jar ...
- 使用openlivewriter编写cnblogs博客
下载OpenLiveWriter 下载地址:http://openlivewriter.org/ 安装OpenLiveWriter 1.账号配置 2.常规操作,省略- 安装高亮插件 1.下载插件:ht ...