Java解析上傳的zip文件--包含Excel解析與圖片上傳
前言:今天遇到一個需求:上傳一個zip格式的壓縮文件,該zip中包含人員信息的excel以及excel中每行對應的人的圖片,現(xiàn)在需要將該zip壓縮包中所有內容解析導入到數(shù)據(jù)庫中,包括圖片,并將圖片與excel內容對應。
代碼演示:
/**
* 信息導入Controller
*/
@RestController
@RequestMapping("/import")
public class ImportController {
@AutoWired
private IExcelService excelService
/**
* 接收zip
* zip中包含了人員excel以及excel中每行對應的人員圖片--默認每個人員圖片的名稱為number號
*/
@PostMapping(value = "/zip")
@Transactional
public void sendRequest(@RequestParam("file") MultipartFile zipFile) throws IOException {
//通過zip名稱創(chuàng)建一個file文件-該文件無具體路徑
File file = new File(Objects.requireNonNull(zipFile.getOriginalFilename()));
//將zip寫入到file中
FileUtils.writeByteArrayToFile(file, zipFileFile.getBytes());
//設定字符集編碼--這一步必須,否則放到linux服務器中會有字符集報錯
Charset charset = Charset.forName("GBK");
ZipFile zipFile = new ZipFile(file, charset);
//開始讀取zip中文件
Enumeration<? extends ZipEntry> entries = zipFile.entries();
//創(chuàng)建excelZip存放zip中的excel文件內容
List<ZipEntry> excelZip = new ArrayList<ZipEntry>();
//創(chuàng)建imgZip存放zip中的圖片文件
List<ZipEntry> imgZip = new ArrayList<ZipEntry>();
//開始讀取zip文件
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
//通過文件名稱去過濾出excel
if (entry.getName().contains("/excel表格名稱.xlsx")) {
excelZip.add(entry);
}
//獲取包含圖片目錄下所有的圖片信息
if (!entry.isDirectory() && entry.getName().contains("圖片目錄的名稱")) {
imgZip.add(entry);
}
}
//創(chuàng)建文件輸入流,將excel變?yōu)榱? InputStream excelInputStream = zipFile.getInputStream(excelZip.get(0));
//創(chuàng)建EasyExcel中的監(jiān)聽器
ExcelListener excelListener = new ExcelListener();
//調用EasyExcel中的EasyExcelFactory方法去讀取輸入流
EasyExcelFactory.read(excelInputStream,Excel.class,excelListener).sheet().doRead();
//到此完成獲取壓縮包中excel的人員信息
List<Excel> excelList = excelListener.getDataList();
//遍歷讀取圖片
imgZip.stream().forEach(zip -> {
try {
//創(chuàng)建壓縮圖片的輸入流
InputStream imgInputStream = zipFile.getInputStream(zip);
//調用流轉文件的方法
File img = stream2file(imgInputStream);
for (Excel excel : excelList)
{
//將去除后綴的壓縮圖片名稱與excel中的人員號碼字段做比較
if (removeExtension(zip.getName()).contains(excel.getNum())) {
//調用文件上傳接口,上傳到服務器目錄下,并返回文件存儲路徑
//這邊FileUpload.upload為自己寫的一個上傳接口,此處省略。。。
//如果接受對象為MultipartFile,還需調用fileToMultipartFile轉換
String urlPath =FileUpload.upload(fileToMultipartFile(img));
//進行對象的保存
Excel e=new Excel();
//設置存儲的圖片地址
e.setPath(urlPath);
//調用存儲接口存儲圖片與excel信息
excelService.insert(e);
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}
//設置文件名前綴;必須至少三個字符
static final String PREFIX = "stream2file";
//設置定義文件的后綴擴展名;如果為null,則將使用后綴".tmp"
//這邊為圖片格式,所以默認給的后綴為jpg格式
static final String SUFFIX = ".jpg";
/**
* 輸入流轉文件
* 該方法為在內存中創(chuàng)建臨時文件,不進行磁盤存儲
*/
public static File stream2file(InputStream in) throws IOException {
//創(chuàng)建臨時文件
final File tempFile = File.createTempFile(PREFIX, SUFFIX);
tempFile.deleteOnExit();
try (FileOutputStream out = new FileOutputStream(tempFile)) {
IOUtils.copy(in, out);
}
return tempFile;
}
/**
* 截取文件名稱--去除后綴名(.jpg等)
*/
public static String removeExtension(String fileName) {
int lastDotIndex = fileName.lastIndexOf('.');
if (lastDotIndex == -1) {
// 如果文件名中沒有后綴,則返回原文件名
return fileName;
} else {
// 截取從開頭到最后一個`.`字符之前的部分
return fileName.substring(0, lastDotIndex);
}
}
/**
* file轉MultipartFile
*
* @param file file
* @return MultipartFile
*/
public static MultipartFile fileToMultipartFile(File file) {
MultipartFile result = null;
if (null != file) {
try (FileInputStream input = new FileInputStream(file)) {
result = new MockMultipartFile(file.getName().concat("temp"), file.getName(), "text/plain", input);
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
}
}
回顧:
博主解答思路為:文章來源:http://www.zghlxwxcb.cn/news/detail-746529.html
- 接收壓縮文件
- 解析壓縮文件,并區(qū)分excel與圖片文件
- 將excel與圖片進行匹配,將匹配成功的數(shù)據(jù)存儲到數(shù)據(jù)庫中
要點:文章來源地址http://www.zghlxwxcb.cn/news/detail-746529.html
- 本次讀取幾乎都是內存讀取,將數(shù)據(jù)讀取到內存中,也在磁盤中建立了臨時文件
- 解析時需要進行多次類型轉換
- new ZipFile(file, charset)這一步一定要設定字符集編碼,并查看window與linux字符集編碼的區(qū)別,否則會因為字符集編碼問題報錯而無法運行
到了這里,關于Java解析上傳的zip文件--包含Excel解析與圖片上傳的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!