You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

423 lines
16 KiB
Java

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.api.sfj.DA.util;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import java.io.*;
import java.nio.channels.FileChannel;
import java.util.UUID;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
/**
* ClassName: JY_action
* date: 2020/8/10 17:23
* author LiLei
* remake 文件处理类,获取ecology存储的附件转换为原文件类型要注意的是一般附件都会被重命名为UUID形式并被压缩为zip形式。
*/
public class JY_action {
/**
* zip文件解压
*
* @param inputFile 待解压文件夹/文件
* @param destDirPath 解压路径
*/
public static void zipUncompress(String inputFile, String destDirPath) throws Exception {
File srcFile = new File(inputFile);//获取当前压缩文件
// 判断源文件是否存在
if (!srcFile.exists()) {
throw new Exception(srcFile.getPath() + "所指文件不存在");
}
//开始解压
//构建解压输入流
ZipInputStream zIn = new ZipInputStream(new FileInputStream(srcFile));
ZipEntry entry = null;
File file = null;
while ((entry = zIn.getNextEntry()) != null) {
if (!entry.isDirectory()) {
file = new File(destDirPath, entry.getName());
if (!file.exists()) {
new File(file.getParent()).mkdirs();//创建此文件的上级目录
}
OutputStream out = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(out);
int len = -1;
byte[] buf = new byte[1024];
while ((len = zIn.read(buf)) != -1) {
bos.write(buf, 0, len);
}
// 关流顺序,先打开的后关闭
bos.close();
out.close();
}
}
}
/**
* 兼容linux的解压
*
* @param zipFilePath
* @param destDir
*/
public static void unzip(String zipFilePath, String destDir) {
System.setProperty("sun.zip.encoding", System.getProperty("sun.jnu.encoding")); //防止文件名中有中文时出错
//System.out.println(System.getProperty("sun.zip.encoding")); //ZIP编码方式
//System.out.println(System.getProperty("sun.jnu.encoding")); //当前文件编码方式
//System.out.println(System.getProperty("file.encoding")); //这个是当前文件内容编码方式
File dir = new File(destDir);
// create output directory if it doesn't exist
if (!dir.exists()) dir.mkdirs();
FileInputStream fis;
// buffer for read and write data to file
byte[] buffer = new byte[1024];
try {
fis = new FileInputStream(zipFilePath);
ZipInputStream zis = new ZipInputStream(fis);
ZipEntry ze = zis.getNextEntry();
while (ze != null) {
String fileName = ze.getName();
File newFile = new File(destDir + File.separator + fileName);
//System.out.println("Unzipping to " + newFile.getAbsolutePath());
// create directories for sub directories in zip
new File(newFile.getParent()).mkdirs();
FileOutputStream fos = new FileOutputStream(newFile);
int len;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
// close this ZipEntry
zis.closeEntry();
ze = zis.getNextEntry();
}
// close last ZipEntry
zis.closeEntry();
zis.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 删除文件
*/
public static boolean deleteFile(String path) {
BaseBean bs = new BaseBean();
boolean flag = true;
File file = new File(path);
if (file.isFile() && file.exists()) {
file.delete();
flag = true;
bs.writeLog("删除成功");
}
return flag;
}
/**
* 重命名文件
*/
public static void singleChange(String path, String oldName, String newName) {
File file = new File(path + oldName); //指定文件名及路径
BaseBean bs = new BaseBean();
if (file.renameTo(new File(path + newName))) {
bs.writeLog("修改成功!");
} else {
bs.writeLog("修改失败");
}
}
public static String getFileNameNoEx(String filename) {
if ((filename != null) && (filename.length() > 0)) {
int dot = filename.lastIndexOf('.');
if ((dot >-1) && (dot < (filename.length()))) {
return filename.substring(0, dot);
}
}
return filename;
}
/**
* 用缓冲区读写,来提升读写效率。
*/
private static void fileCopyRightWay(String oldPath,String newPath) {
BaseBean bs = new BaseBean();
bs.writeLog("oldPath-->"+oldPath);
bs.writeLog("newPath-->"+newPath);
FileWriter fw = null;
FileReader fr = null;
try {
fr = new FileReader(oldPath);//读
fw = new FileWriter(newPath);//写
char[] buf = new char[1024];//缓冲区
int len;
while ((len = fr.read(buf)) != -1) {
fw.write(buf, 0, len);//读几个写几个
}
} catch (IOException e) {
bs.writeLog(e.getMessage());
} finally {
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
bs.writeLog(e.getMessage());
}
}
if (fw != null) {
try {
fw.flush();
fw.close();
} catch (IOException e) {
bs.writeLog(e.getMessage());
}
}
}
}
private static void copyFileUsingFileChannels(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();
}
}
public static void fjFilename(String fj, String GDnewPath) {
String[] arr = fj.split(",");
BaseBean bs = new BaseBean();
bs.writeLog("进入附件方法---111"+GDnewPath);
for (int i = 0; i < arr.length; i++) {
RecordSet rs33 = new RecordSet();
String docdetail = "select top(1) im.filerealpath,do.imagefilename,im.iszip from imagefile im left join docimagefile do on im.imagefileid = do.imagefileid where docid=" + arr[i] + " order by versionid desc";
boolean su = rs33.execute(docdetail);
bs.writeLog("语句执行" + su);
bs.writeLog("===" + docdetail);
if (rs33.next()) {
String filerealpath = rs33.getString("filerealpath");
String imagefilename = rs33.getString("imagefilename");
int zip = rs33.getInt("iszip");
bs.writeLog("获取数据--11--44--" + filerealpath);
bs.writeLog("获取数据--11--44--" + imagefilename);
bs.writeLog("进入正文方法---111====判断是否压缩" + zip);
if (zip == 1) {
try {
bs.writeLog("进入正文方法--22====是压缩" + zip);
int index = filerealpath.lastIndexOf("/");
int index2 = imagefilename.lastIndexOf(".");
String zipPath = filerealpath.substring(index + 1); //压缩文件名
int index3 = zipPath.lastIndexOf(".");
String zipPath1 = zipPath.substring(0,index3);
String newname = imagefilename.substring(index2);
unzip(filerealpath, GDnewPath);
bs.writeLog(GDnewPath);
singleChange(GDnewPath+"/", zipPath1, zipPath1 + newname);
bs.writeLog("重命名"+zipPath1 + newname);
// bs.writeLog("重命名"+zipPath+ newname);
} catch (Exception e) {
e.printStackTrace();
}
} else {
bs.writeLog("进入正文方法---33==OFD==否压缩" + zip);
int index = filerealpath.lastIndexOf("/");
int index2 = imagefilename.lastIndexOf(".");
String zipPath = filerealpath.substring(index + 1); //未压缩文件名
String newname = imagefilename.substring(index2);
bs.writeLog("进入正文方法---344==="+zipPath+"===="+newname);
File f = new File(GDnewPath+"/"+zipPath+newname);
//复制文件到新文件夹下
if(! f.exists()) {
bs.writeLog("进入正文方法--55==OFD==否压缩" + zip);
InputStream instream = null;
try {
instream = new FileInputStream(new File(filerealpath));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
OutputStream out1 = null;
try {
out1 = new FileOutputStream(new File(GDnewPath+"/"+zipPath+newname));
bs.writeLog("进入正文方法--66==OFD==否压缩" );
int read = 0;
byte[] bytes = new byte[2048];
while((read = instream.read(bytes)) != -1) {
out1.write(bytes, 0, read);
}
out1.flush();
instream.close();
out1.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
}
public static String filename(String fj, String GDnewPath) {
String[] arr = fj.split(",");
BaseBean bs = new BaseBean();
bs.writeLog("进入附件方法---111"+GDnewPath);
UUID uuid = UUID.randomUUID();
for (int i = 0; i < arr.length; i++) {
RecordSet rs33 = new RecordSet();
String docdetail = "select top(1) im.filerealpath,do.imagefilename,im.iszip from imagefile im left join docimagefile do on im.imagefileid = do.imagefileid where docid=" + arr[i] + " order by versionid desc";
boolean su = rs33.execute(docdetail);
bs.writeLog("语句执行" + su);
bs.writeLog("===" + docdetail);
if (rs33.next()) {
String filerealpath = rs33.getString("filerealpath");
String imagefilename = rs33.getString("imagefilename");
int zip = rs33.getInt("iszip");
bs.writeLog("获取数据--11--44--" + filerealpath);
bs.writeLog("获取数据--11--44--" + imagefilename);
bs.writeLog("进入正文方法---111====判断是否压缩" + zip);
if (zip == 1) {
try {
bs.writeLog("进入正文方法--22====是压缩" + zip);
int index = filerealpath.lastIndexOf("/");
int index2 = imagefilename.lastIndexOf(".");
String zipPath = filerealpath.substring(index + 1); //压缩文件名
int index3 = zipPath.lastIndexOf(".");
String zipPath1 = zipPath.substring(0,index3);
String newname = imagefilename.substring(index2);
unzip(filerealpath, GDnewPath);
bs.writeLog(GDnewPath);
singleChange(GDnewPath+"/", zipPath1, zipPath1 + newname);
bs.writeLog("重命名"+zipPath1 + newname);
bs.writeLog("重命名"+zipPath+ newname);
String oldPath=GDnewPath+"/"+zipPath1+newname;
bs.writeLog("oldPath-->"+oldPath);
String newPath=GDnewPath+"/"+uuid.toString()+newname;
bs.writeLog("newPath-->"+newPath);
// fileCopyRightWay(oldPath,newPath);
try {
copyFileUsingFileChannels(new File(oldPath), new File(newPath));
}catch (Exception e){
e.printStackTrace();
}
bs.writeLog("执行完重命名-->");
} catch (Exception e) {
e.printStackTrace();
}
} else {
bs.writeLog("进入正文方法---33==OFD==否压缩" + zip);
int index = filerealpath.lastIndexOf("/");
int index2 = imagefilename.lastIndexOf(".");
String zipPath = filerealpath.substring(index + 1); //未压缩文件名
String newname = imagefilename.substring(index2);
bs.writeLog("进入正文方法---344==="+zipPath+"===="+newname);
File f = new File(GDnewPath+"/"+zipPath+newname);
//复制文件到新文件夹下
if(! f.exists()) {
bs.writeLog("进入正文方法--55==OFD==否压缩" + zip);
InputStream instream = null;
try {
instream = new FileInputStream(new File(filerealpath));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
OutputStream out1 = null;
try {
out1 = new FileOutputStream(new File(GDnewPath+"/"+zipPath+newname));
bs.writeLog("进入正文方法--66==OFD==否压缩" );
int read = 0;
byte[] bytes = new byte[2048];
while((read = instream.read(bytes)) != -1) {
out1.write(bytes, 0, read);
}
out1.flush();
instream.close();
out1.close();
bs.writeLog("uuid.toString2()-->"+uuid.toString());
String oldPath=GDnewPath+"/"+zipPath+newname;
bs.writeLog("oldPath2-->"+oldPath);
String newPath=GDnewPath+"/"+uuid.toString()+newname;
bs.writeLog("newPath2-->"+newPath);
// fileCopyRightWay(oldPath,newPath);
try {
copyFileUsingFileChannels(new File(oldPath), new File(newPath));
}catch (Exception e){
e.printStackTrace();
}
bs.writeLog("执行完重命名2-->");
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
return uuid.toString();
}
}