项目开发中需要导入这样的数据,根据文件路径将文件上传到文件服务器,本来以为通过File来实现,的确在本地是可以实现的。然而项目发布后,需要导入报文件不存在。原因是服务器根本不会存在这样的文件。
后来想了一个办法,将文件打成zip文件。通过解读zip文件来找到相应的文件。如图
发现使用zip导入,读取会有问题,应该里面包含了中文,造成读取不了,网上百度都是修改字符集为“GBK”,不过这种办法治标不治本,apache已经提供了一个jar来解决这个问题
compile("org.apache.commons:commons-compress:1.8.1")
使用ZipArchiveInputStream来解决。
package net.parim.spark.unicom.provider.careercenter.service;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.commons.io.FileUtils;
import org.apache.poi.util.IOUtils;
public class UnzipFile {
public static void main(String[] args) throws Exception{
String path = "D:/data/content/upload/2018/05/14/jiaocai.zip";
unzipFile(path);
File file = new File("D:/data/content/upload/2018/05/14/jiaocai/1231.xlsx");
System.out.println(file.getName());
//
// String filePath = path.substring(0,path.lastIndexOf("/")+1);
// String filePath2 = unzip(new File(path), filePath);
// String filePath3 = filePath2.substring(0,filePath2.lastIndexOf("\\")+1);
// File file = new File(filePath3);
// File[] files = file.listFiles();
// System.out.println(files[0].getAbsolutePath());
// for(int i=0;i<files.length;i++){
// System.out.println(files[i].getAbsolutePath());
// }
//System.out.println(filePath2);
}
public static Map<String,Object> unzipFile(String path){
String filePath = "";
String filePath3 = "";
if(path.lastIndexOf("\\")!=-1){
filePath = path.substring(0,path.lastIndexOf("\\")+1);
String filePath2 = unzip(new File(path), filePath);
filePath3 = filePath2.substring(0,filePath2.lastIndexOf("\\")+1);
}else{
filePath = path.substring(0,path.lastIndexOf("/")+1);
String filePath2 = unzip(new File(path), filePath);
filePath3 = filePath2.substring(0,filePath2.lastIndexOf("/")+1);
}
File file = new File(filePath3);
File[] files = file.listFiles();
Map<String,Object> map = new HashMap<String,Object>();
map.put("file", files[0].getAbsolutePath());
map.put("directory", files[1].getAbsolutePath());
System.out.println(map);
return map;
}
public static String unzip(File zipFile, String descDir) {
File directory=null;
try (ZipArchiveInputStream inputStream = getZipFile(zipFile)) {
File pathFile = new File(descDir);
if (!pathFile.exists()) {
pathFile.mkdirs();
}
ZipArchiveEntry entry = null;
while ((entry = inputStream.getNextZipEntry()) != null) {
if (entry.isDirectory()) {
directory = new File(descDir, entry.getName());
directory.mkdirs();
} else {
OutputStream os = null;
try {
os = new BufferedOutputStream(new FileOutputStream(new File(descDir, entry.getName())));
//输出文件路径信息
IOUtils.copy(inputStream, os);
} finally {
IOUtils.closeQuietly(os);
}
}
}
final File[] files = pathFile.listFiles();
if (files != null && files.length == 1 && files[0].isDirectory()) {
// 说明只有一个文件夹
FileUtils.copyDirectory(files[0], pathFile);
}
} catch (Exception e) {
}
return directory.getAbsolutePath();
}
private static ZipArchiveInputStream getZipFile(File zipFile) throws Exception {
return new ZipArchiveInputStream(new BufferedInputStream(new FileInputStream(zipFile)));
}
}