开发OTA备案工具-导入导出

This commit is contained in:
高嵩
2023-03-28 02:46:21 +08:00
parent 899a50fd28
commit 42c9d90ad7
8 changed files with 271 additions and 73 deletions
@@ -215,7 +215,11 @@ public class OtaBaCxListController extends JeroController<OtaBaCxList, IOtaBaCxL
@GetMapping(value = "/exportData")
// @RequiresPermissions("otaBaCx:exportTemplate")
public void exportData(@RequestParam(value = "otaId", required = false) String otaId, HttpServletResponse response, HttpServletRequest request) throws Exception {
this.otaBaCxListService.exportData(otaId, response, request);
try {
this.otaBaCxListService.exportData(otaId, response, request);
} catch (Exception e) {
e.printStackTrace();
}
}
}
@@ -71,4 +71,6 @@ public interface IOtaBaCxJbxxService extends IService<OtaBaCxJbxx> {
OtaBaCxJbxx importData(MultipartFile file, String otaId, String cut) throws Exception;
OtaBaCxJbxx parseFileList(List<File> upLoadFiles, String otaId, String cut) throws Exception;
void exportData(File file, String otaId, String cut) throws Exception;
}
@@ -11,65 +11,65 @@ import java.util.List;
/**
* @Description: 车型及功能备案列表
* @Author: jero-boot
* @Date: 2023-03-17
* @Date: 2023-03-17
* @Version: V1.0
*/
public interface IOtaBaCxListService extends IService<OtaBaCxList> {
/**
* 保存
*
* @param otaBaCxList
* @return
*/
/**
* 保存
*
* @param otaBaCxList
* @return
*/
void add(OtaBaCxList otaBaCxList);
/**
* 更新
*
* @param otaBaCxList
* @return
*/
/**
* 更新
*
* @param otaBaCxList
* @return
*/
void editById(OtaBaCxList otaBaCxList);
/**
* 通过id删除
*
* @param id
* @return
*/
/**
* 通过id删除
*
* @param id
* @return
*/
void deleteById(String id);
/**
* 批量删除
*
* @param ids
* @return
*/
/**
* 批量删除
*
* @param ids
* @return
*/
void deleteByIds(List<String> ids);
/**
* 通过id查询
*
* @param id
* @return
*/
/**
* 通过id查询
*
* @param id
* @return
*/
OtaBaCxList queryById(String id);
/**
* 列表查询
*/
* 列表查询
*/
List<OtaBaCxList> queryList();
/**
* 备案维护
*/
void batchRecord(String ids, String bazt, String batjsj, String bz, String cut);
/**
* 备案维护
*/
void batchRecord(String ids, String bazt, String batjsj, String bz, String cut);
void exportTemplate(HttpServletResponse response, HttpServletRequest request);
void importData(MultipartFile file, String cut) throws Exception;
void exportData(String otaId, HttpServletResponse response, HttpServletRequest request);
void exportData(String otaId, HttpServletResponse response, HttpServletRequest request) throws Exception;
}
@@ -72,4 +72,6 @@ public interface IOtaBaCxZxsjyqService extends IService<OtaBaCxZxsjyq> {
OtaBaCxZxsjyq importData(MultipartFile file, String otaId, String cut) throws Exception;
OtaBaCxZxsjyq parseFileList(List<File> upLoadFiles, String otaId, String cut) throws Exception;
void exportData(File file, String otaId, String cut) throws Exception;
}
@@ -236,5 +236,48 @@ public class OtaBaCxJbxxServiceImpl extends ServiceImpl<OtaBaCxJbxxMapper, OtaBa
return jbxx;
}
@Override
public void exportData(File file, String otaId, String cut) throws Exception {
OtaBaCxJbxx jbxx = this.getByOtaId(otaId);
Workbook wb = ImportFileUtil.readExcel(file);
Sheet s = wb.getSheetAt(0);
Row row = s.getRow(1);
Cell cell = row.getCell(1);
cell.setCellValue(jbxx.getQymc());
row = s.getRow(2);
cell = row.getCell(1);
cell.setCellValue(jbxx.getDjbh());
row = s.getRow(3);
cell = row.getCell(1);
cell.setCellValue(jbxx.getCplb());
row = s.getRow(4);
cell = row.getCell(1);
cell.setCellValue(jbxx.getGgpc());
row = s.getRow(5);
cell = row.getCell(1);
cell.setCellValue(jbxx.getCpsb());
row = s.getRow(6);
cell = row.getCell(1);
cell.setCellValue(jbxx.getCpmc());
row = s.getRow(7);
cell = row.getCell(1);
cell.setCellValue(jbxx.getCpxh());
row = s.getRow(8);
cell = row.getCell(1);
cell.setCellValue(jbxx.getDllx1());
row = s.getRow(9);
cell = row.getCell(1);
cell.setCellValue(jbxx.getDllx2());
row = s.getRow(10);
cell = row.getCell(1);
cell.setCellValue(sysDictService.queryDictItemByValue("communication_terminal", jbxx.getTxzd(), cut));
row = s.getRow(11);
cell = row.getCell(1);
cell.setCellValue(jbxx.getCdotasj());
wb.write(new FileOutputStream(file));
wb.close();
}
}
@@ -10,6 +10,7 @@ import com.jero.modules.ota.mapper.OtaBaCxListMapper;
import com.jero.modules.ota.service.*;
import com.jero.modules.ota.utils.ImportFileUtil;
import com.jero.modules.system.util.StringUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
@@ -19,10 +20,7 @@ import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@@ -187,28 +185,23 @@ public class OtaBaCxListServiceImpl extends ServiceImpl<OtaBaCxListMapper, OtaBa
}
@Override
public void exportData(String otaId, HttpServletResponse response, HttpServletRequest request) {
log.error("otaId" + otaId);
File template = new File("D://opt//ota//车型及功能备案.zip");
BufferedOutputStream os = null;
InputStream in = null;
try {
in = new FileInputStream(template);
response.setHeader("Content-disposition", "attachment;filename=" + template.getName());
response.setContentType("application/octet-stream;charset=UTF-8");
os = new BufferedOutputStream(response.getOutputStream());
int len;
while ((len = in.read()) != -1) {
os.write(len);
os.flush();
}
in.close();
os.close();
} catch (Exception e) {
throw new JeroBootException("下载文件失败,请重试");
} finally {
IOUtils.closeQuietly(in);
IOUtils.closeQuietly(os);
public void exportData(String otaId, HttpServletResponse response, HttpServletRequest request) throws Exception {
String cut = "cn";
File template = new File(templatePath + "车型及功能备案.zip");
File exportFile = ImportFileUtil.copyZip(ImportFileUtil.createMfileByFile(template), cut, uploadPath);
File jbxxFile = ImportFileUtil.findFile(exportFile, "车型基本信息.xlsx");
if (ObjectUtils.isEmpty(jbxxFile)) {
throw new JeroBootException("没有找到 车型基本信息.xlsx");
}
otaBaCxJbxxService.exportData(jbxxFile, otaId, cut);
String outPath = uploadPath + "/车型及功能备案" + System.currentTimeMillis() + ".zip";
FileOutputStream fos1 = new FileOutputStream(outPath);
ImportFileUtil.toZip(exportFile.getCanonicalPath(), fos1, true);
ImportFileUtil.exportTemplate(response, outPath);
}
}
@@ -250,4 +250,9 @@ public class OtaBaCxZxsjyqServiceImpl extends ServiceImpl<OtaBaCxZxsjyqMapper, O
otaBaCxZxsjyq.setZmcl(obj.toString());
return otaBaCxZxsjyq;
}
@Override
public void exportData(File file, String otaId, String cut) throws Exception {
OtaBaCxZxsjyq otaBaCxZxsjyq = this.getByOtaId(otaId);
}
}
@@ -12,15 +12,22 @@ import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.entity.ContentType;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ImportFileUtil {
@@ -52,8 +59,7 @@ public class ImportFileUtil {
}
public static List<File> importZip(MultipartFile file, String cut, String uploadPath) throws Exception {
List<File> fileList = new ArrayList<>();
public static File copyZip(MultipartFile file, String cut, String uploadPath) throws Exception {
int pos = file.getOriginalFilename().lastIndexOf(".");
String str = file.getOriginalFilename().substring(pos + 1).toLowerCase();
String filenameorg = file.getOriginalFilename().substring(0, pos);
@@ -77,7 +83,12 @@ public class ImportFileUtil {
if (str.equals("zip")) {
zipEntryName = FileUnZip.unZipFiles(path + File.separator + file.getOriginalFilename(), path);
}
File fileNew = new File(path);
return new File(path);
}
public static List<File> importZip(MultipartFile file, String cut, String uploadPath) throws Exception {
List<File> fileList = new ArrayList<>();
File fileNew = ImportFileUtil.copyZip(file, cut, uploadPath);
for (File file1 : fileNew.listFiles()) {
if (file1.getName().contains(".xls") || file1.getName().contains(".xlsx") || file1.isDirectory()
|| file1.getName().contains(".docx") || file1.getName().contains(".doc")) {
@@ -112,7 +123,6 @@ public class ImportFileUtil {
}
public static Workbook readExcel(File file) {
Workbook wb = null;
if (file == null) {
return null;
}
@@ -121,9 +131,9 @@ public class ImportFileUtil {
try {
is = new FileInputStream(file);
if (".xls".equals(extString)) {
return wb = new HSSFWorkbook(is);
return new HSSFWorkbook(is);
} else if (".xlsx".equals(extString)) {
return wb = new XSSFWorkbook(is);
return new XSSFWorkbook(is);
}
is.close();
} catch (FileNotFoundException e) {
@@ -133,7 +143,7 @@ public class ImportFileUtil {
} finally {
IOUtils.closeQuietly(is);
}
return wb;
return null;
}
public static String getCellValue(Cell cell, Workbook workbook) throws Exception {
@@ -202,6 +212,19 @@ public class ImportFileUtil {
return null;
}
public static File findFile(List<File> file, String fileName) throws Exception {
for (File file1 : file) {// 遍历数组
// 判断当前的File对象是否为文件夹
if (!file1.isDirectory()) {
String name = file1.getName();
if (name.equals(fileName)) {
return file1;
}
}
}
return null;
}
public static MultipartFile createMfileByFile(File file) {
MultipartFile mFile = null;
try {
@@ -253,4 +276,130 @@ public class ImportFileUtil {
return res;
}
private static final int BUFFER_SIZE = 2 * 1024;
/**
* 压缩成ZIP 方法1
*
* @param srcDir 压缩文件夹路径
* @param out 压缩文件输出流
* @param KeepDirStructure 是否保留原来的目录结构,true:保留目录结构;
* false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败)
* @throws RuntimeException 压缩失败会抛出运行时异常
*/
public static void toZip(String srcDir, OutputStream out, boolean KeepDirStructure)
throws RuntimeException {
long start = System.currentTimeMillis();
ZipOutputStream zos = null;
try {
zos = new ZipOutputStream(out);
File sourceFile = new File(srcDir);
compress(sourceFile, zos, sourceFile.getName(), KeepDirStructure);
long end = System.currentTimeMillis();
System.out.println("压缩完成,耗时:" + (end - start) + " ms");
} catch (Exception e) {
throw new RuntimeException("zip error from ZipUtils", e);
} finally {
if (zos != null) {
try {
zos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 压缩成ZIP 方法2
*
* @param srcFiles 需要压缩的文件列表
* @param out 压缩文件输出流
* @throws RuntimeException 压缩失败会抛出运行时异常
*/
public static void toZip(List<File> srcFiles, OutputStream out) throws RuntimeException {
long start = System.currentTimeMillis();
ZipOutputStream zos = null;
try {
zos = new ZipOutputStream(out);
for (File srcFile : srcFiles) {
byte[] buf = new byte[BUFFER_SIZE];
zos.putNextEntry(new ZipEntry(srcFile.getName()));
int len;
FileInputStream in = new FileInputStream(srcFile);
while ((len = in.read(buf)) != -1) {
zos.write(buf, 0, len);
}
zos.closeEntry();
in.close();
}
long end = System.currentTimeMillis();
System.out.println("压缩完成,耗时:" + (end - start) + " ms");
} catch (Exception e) {
throw new RuntimeException("zip error from ZipUtils", e);
} finally {
if (zos != null) {
try {
zos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 递归压缩方法
*
* @param sourceFile 源文件
* @param zos zip输出流
* @param name 压缩后的名称
* @param KeepDirStructure 是否保留原来的目录结构,true:保留目录结构;
* false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败)
* @throws Exception
*/
private static void compress(File sourceFile, ZipOutputStream zos, String name,
boolean KeepDirStructure) throws Exception {
byte[] buf = new byte[BUFFER_SIZE];
if (sourceFile.isFile()) {
// 向zip输出流中添加一个zip实体,构造器中name为zip实体的文件的名字
zos.putNextEntry(new ZipEntry(name));
// copy文件到zip输出流中
int len;
FileInputStream in = new FileInputStream(sourceFile);
while ((len = in.read(buf)) != -1) {
zos.write(buf, 0, len);
}
// Complete the entry
zos.closeEntry();
in.close();
} else {
File[] listFiles = sourceFile.listFiles();
if (listFiles == null || listFiles.length == 0) {
// 需要保留原来的文件结构时,需要对空文件夹进行处理
if (KeepDirStructure) {
// 空文件夹的处理
zos.putNextEntry(new ZipEntry(name + "/"));
// 没有文件,不需要文件的copy
zos.closeEntry();
}
} else {
for (File file : listFiles) {
// 判断是否需要保留原来的文件结构
if (KeepDirStructure) {
// 注意:file.getName()前面需要带上父文件夹的名字加一斜杠,
// 不然最后压缩包中就不能保留原来的文件结构,即:所有文件都跑到压缩包根目录下了
compress(file, zos, name + "/" + file.getName(), KeepDirStructure);
} else {
compress(file, zos, file.getName(), KeepDirStructure);
}
}
}
}
}
}