第一部分,入参两个,一个是为压缩包或文件,另一个是我自己的入参,可删除
@Override
public void batchImportFileNew(String type, File file1) throws IOException {
String checkFileName1 = file1.getName();
if(checkFileName1.indexOf("-")!=checkFileName1.lastIndexOf("-")) {
checkFileName1 = checkFileName1.substring(checkFileName1.indexOf("-")+1, checkFileName1.lastIndexOf("-"));
}
String suffix1 = checkFileName1.substring(checkFileName1.lastIndexOf(".") + 1);
//本地文件暂存路径
String destPath = "D:\\tmsImage\\";
List<File> fileListZip = new ArrayList<File>();
if ("zip".equals(suffix1.toLowerCase()) ) {
fileListZip = compresseZip(file1, destPath);
}else if("rar".equals(suffix1.toLowerCase())){
}else if("7z".equals(suffix1.toLowerCase())){
}else {
fileListZip.add(file1);
}
for (File file : fileListZip) {
try {
String checkFileName = file.getName();
String ecNo = checkFileName.substring(0,checkFileName.indexOf("_"));
String hqlLeg = "from TmsLeg t where t.ecNo=:ecNo";
TmsLeg leg = (TmsLeg)commonDao.findByQueryUniqueResult(hqlLeg, "ecNo", ecNo);
String hqlSign = "from TmsSign t where t.leg.id=:legId";
TmsSign sign = (TmsSign)commonDao.findByQueryUniqueResult(hqlSign, "legId", leg.getId());
String suffix = checkFileName.substring(checkFileName.lastIndexOf(".") + 1);
if ("jpg".equals(suffix.toLowerCase())||
"png".equals(suffix.toLowerCase())||
"gif".equals(suffix.toLowerCase())||
"jpeg".equals(suffix.toLowerCase())||
"pdf".equals(suffix.toLowerCase())) {
String filetype=suffix.toLowerCase();
this.batchImportFile(sign, type, file,filetype);
}else {
double width = 0;
double height = 0;
double scrWidth = 1024;//设置屏幕分辨率
if(width > scrWidth){
width = scrWidth;
}
TmsSignFile signFile = EntityFactory.getEntity(TmsSignFile.class);
if (file != null) {
//处理文件名
String oldFileName =file.getName();
String fileName=URL_Util.decoder(oldFileName);
if(oldFileName.indexOf("-")!=oldFileName.lastIndexOf("-")) {
fileName = fileName.substring(fileName.indexOf("-")+1, fileName.lastIndexOf("-"));
}
UploadUtils.createFile(file,fileName);
String filePathStr = UploadUtils.getHtmlCode(file,fileName);
signFile.setAttachmentName(fileName);
signFile.setAttachmentPath(filePathStr);
signFile.setType(type);
signFile.setLeg(sign.getLeg());
this.commonDao.store(signFile);
String fileUrl ="<img width="+'"'+width+'"'+"height="+'"'+height+'"'+" src=\"*.tmsSignServlet?id=" + signFile.getId() + "\" ";
fileUrl = fileUrl + " title=" + fileName + " />";
signFile.setFileUrl(fileUrl);
this.commonDao.store(signFile);
/*更新操作人信息节点毫无意义,store的时候session里面回自动重新获取*/
sign.setBeImportFile(true);
/*首次上传影像时赋值*/
if (sign.getSignFilesTime() == null) {
sign.setSignFilesTime(new Date());
}
TmsOrder order = commonDao.load(TmsOrder.class, leg.getOrder().getId());
if(TmsDispatchMethod.DOMEXPRESS.equals(order.getShipmentMethod())||TmsDispatchMethod.INTLEXPRESS.equals(order.getShipmentMethod())){
sign.setReturnTime(sign.getSignFilesTime());
}
this.commonDao.store(sign);
}
//是否回单及时
Calendar calendar = Calendar.getInstance();
Date time = null;
if(sign.getLeg().getArriveTime()!=null){
time=sign.getLeg().getArriveTime();
}else{
time=sign.getLeg().getShipment().getArriveTime();
}
if(time==null){
throw new BusinessException("运单及调度单实际到达时间均为空,无法计算是否回单及时");
}
calendar.setTime(time);
calendar.set(Calendar.HOUR_OF_DAY, 24);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.add(Calendar.DATE, +6);
Date arriveTime = calendar.getTime();
if(sign.getSignFilesTime().before(arriveTime)){
sign.setIsReceiptInTime(Boolean.TRUE);
}else{
sign.setIsReceiptInTime(Boolean.FALSE);
}
}
if(TmsSignStatus.RECEIVE.equals(sign.getStatus())){
sign.setSignCheckResult(TmsSignCheckStatus.A);
}
if(sign.getCheckResult()!=null&&"reject".equals(sign.getCheckResult())){
sign.setSignCheckResult(TmsSignCheckStatus.D);
}
commonDao.store(sign);
} catch (Exception e) {
logger.error("importFileNew error",e);
throw new BusinessException("上传失败,请联系系统管理员!");
}
}
}
然后是zip版本的解压
public static List<File> compresseZip(File srcFile,String destDirPath) throws IOException {
List<File> flieList = new ArrayList<File>();
long start = System.currentTimeMillis();
// 判断源文件是否存在
if (!srcFile.exists()) {
throw new RuntimeException(srcFile.getPath() + "所指文件不存在");
}
// 开始解压
ZipFile zipFile = null;
try {
zipFile = new ZipFile(srcFile);
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());
flieList.add(targetFile);
// 保证这个文件的父文件夹必须要存在
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 end = System.currentTimeMillis();
System.out.println("解压完成,耗时:" + (end - start) +" ms");
} catch (Exception e) {
throw new RuntimeException("unzip error from ZipUtils", e);
} finally {
if(zipFile != null){
try {
zipFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return flieList;
}
zip的解压中,调用了多个方法,只有一个是解压有关的,我放在下面。
public void batchImportFile(TmsSign sign,String type,File file,String filetype) {
StringBuffer stringBuffer = new StringBuffer();
double width = 0;
double height = 0;
try{
BufferedImage image = null;
if (!"pdf".equals(filetype)) {
image = ImageIO.read(new File(file.getAbsolutePath()));
width = image.getWidth();
height = image.getHeight();
}
}catch(IOException e){
throw new BusinessException("读取影像文件发生错误["+e.getMessage()+"],请确认上传的是有效的图片文件!");
}
if (!"pdf".equals(filetype)) {
double scrWidth = 1024;//设置屏幕分辨率
if(width > scrWidth){
width = scrWidth;
}
}
TmsSignFile signFile = EntityFactory.getEntity(TmsSignFile.class);
if (file != null) {
String fileName = batchGetImageName(sign, file);
createFile(file, sign, fileName);
getHtmlCode(stringBuffer, file, sign, fileName);
signFile.setAttachmentName(fileName);
signFile.setAttachmentPath(stringBuffer.toString());
signFile.setType(type);
signFile.setLeg(sign.getLeg());
this.commonDao.store(signFile);
String fileUrl = "";
if ("pdf".equals(filetype)) {
fileUrl ="<embed width='100%' height='100%' src=\"*.tmsSignPdfServlet?id=" + signFile.getId() + "\" ";
fileUrl = fileUrl + " title=" + fileName + " />";
}else {
fileUrl ="<img width="+'"'+width+'"'+"height="+'"'+height+'"'+" src=\"*.tmsSignServlet?id=" + signFile.getId() + "\" ";
fileUrl = fileUrl + " title=" + fileName + " />";
}
signFile.setFileUrl(fileUrl);
this.commonDao.store(signFile);
/*更新操作人信息节点毫无意义,store的时候session里面回自动重新获取*/
sign.setBeImportFile(true);
/*首次上传影像时赋值*/
if (sign.getSignFilesTime() == null) {
sign.setSignFilesTime(new Date());
}
TmsLeg leg = commonDao.load(TmsLeg.class, sign.getLeg().getId());
TmsOrder order = commonDao.load(TmsOrder.class, leg.getOrder().getId());
if(TmsDispatchMethod.DOMEXPRESS.equals(order.getShipmentMethod())||TmsDispatchMethod.INTLEXPRESS.equals(order.getShipmentMethod())){
sign.setReturnTime(sign.getSignFilesTime());
}
this.commonDao.store(sign);
}
//是否回单及时
Calendar calendar = Calendar.getInstance();
calendar.setTime(sign.getLeg().getShipment().getArriveTime());
calendar.set(Calendar.HOUR_OF_DAY, 24);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.add(Calendar.DATE, +6);
Date arriveTime = calendar.getTime();
if(sign.getSignFilesTime().before(arriveTime)){
sign.setIsReceiptInTime(Boolean.TRUE);
}else{
sign.setIsReceiptInTime(Boolean.FALSE);
}
}
上面这个方法里,有个文件名的乱码处理,方法是下面这个。
private String batchGetImageName(TmsSign sign, File file) {
String oldFileName = "";
String fileName = "";
try {
/*需要对文件名进行URL转码*/
oldFileName = URLDecoder.decode(file.getName(),"GBK");
fileName = oldFileName;
if (oldFileName.indexOf("-") != oldFileName.lastIndexOf("-")) {
fileName = oldFileName.substring(oldFileName.indexOf("-") + 1,
oldFileName.lastIndexOf("-"));
}
} catch (UnsupportedEncodingException e) {
logger.error("getImageName异常",e);
}
return fileName;
}
找到了好用的7z解压的代码,https://blog.youkuaiyun.com/qq_40955456/article/details/120361727
有机会再找rar的。