package cn.com.util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Component;
import com.zhuozhengsoft.pageoffice.PDFCtrl;
import com.zhuozhengsoft.pageoffice.PageOfficeCtrl;
import cn.com.szhtkj.controller.xtgl.BaseController;
@Component
public class CommonUtils extends BaseController{
public static void fileMake(String filePath){
File fileMake = new File(filePath);
if (!fileMake.exists()) {
fileMake.mkdirs();
}
}
public static List<String> getKjkm(String kjkm[]){
Arrays.sort(kjkm);
List<String> kjkmNewList = new ArrayList<String>();
for(int i = 0; i < kjkm.length; i++) {
String kjkmTemp = kjkm[i].toString();
boolean flg = true;
for(int j = 0; j < kjkm.length; j++) {
if(kjkmTemp.indexOf(kjkm[j]) >= 0 && kjkmTemp.length() != kjkm[j].length()) {
flg = false;
break;
}
}
if(flg) {
kjkmNewList.add(kjkm[i]);
}
}
return kjkmNewList;
}
public static String getRelativeYM(String year_month,int al_month){
long year = Long.parseLong(year_month.substring(0, 4));
long month = Long.parseLong(year_month.substring(4, 6));
long month_all = year * Constants.LONG_TWELVE + month + Constants.INT_ONE;
month_all += al_month;
year = Math.abs(month_all / Constants.LONG_TWELVE);
month = month_all - year * Constants.LONG_TWELVE;
if(month == Constants.INT_ZERO) {
month = Constants.LONG_TWELVE;
year--;
}
return Long.toString(year) + String.format("%02d",month);
}
public static void compress(String srcFilePath, String destFilePath) {
File src = new File(srcFilePath);
if (!src.exists()) {
throw new RuntimeException(srcFilePath + "不存在");
}
File zipFile = new File(destFilePath);
try {
FileOutputStream fos = new FileOutputStream(zipFile);
ZipOutputStream zos = new ZipOutputStream(fos);
String baseDir = "";
compressbyType(src, zos, baseDir);
zos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private static void compressbyType(File src, ZipOutputStream zos,String baseDir) {
if (!src.exists())
return;
System.out.println("压缩路径" + baseDir + src.getName());
if (src.isFile()) {
compressFile(src, zos, baseDir);
} else if (src.isDirectory()) {
compressDir(src, zos, baseDir);
}
}
private static void compressFile(File file, ZipOutputStream zos,String baseDir) {
if (!file.exists())
return;
try {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
ZipEntry entry = new ZipEntry(baseDir + file.getName());
zos.putNextEntry(entry);
int count;
byte[] buf = new byte[1024];
while ((count = bis.read(buf)) != -1) {
zos.write(buf, 0, count);
}
bis.close();
} catch (Exception e) {
}
}
private static void compressDir(File dir, ZipOutputStream zos,String baseDir) {
if (!dir.exists())
return;
File[] files = dir.listFiles();
if(files.length == 0){
try {
zos.putNextEntry(new ZipEntry(baseDir + dir.getName()+File.separator));
} catch (IOException e) {
e.printStackTrace();
}
}
for (File file : files) {
compressbyType(file, zos, baseDir + dir.getName() + File.separator);
}
}
public static void copyDirectiory(String sourceDir, String targetDir) throws IOException {
(new File(targetDir)).mkdirs();
File[] file = (new File(sourceDir)).listFiles();
for (int i = 0; i < file.length; i++) {
if (file[i].isFile()) {
File sourceFile = file[i];
File targetFile = new File(new File(targetDir).getAbsolutePath() + File.separator + file[i].getName());
copyFile(sourceFile,targetFile);
}
if (file[i].isDirectory()) {
String dir1 = sourceDir + "/" + file[i].getName();
String dir2 = targetDir + "/" + file[i].getName();
copyDirectiory(dir1, dir2);
}
}
}
public static void copyFile(File sourceFile,File targetFile) throws IOException{
FileInputStream input = new FileInputStream(sourceFile);
BufferedInputStream inBuff=new BufferedInputStream(input);
FileOutputStream output = new FileOutputStream(targetFile);
BufferedOutputStream outBuff=new BufferedOutputStream(output);
byte[] b = new byte[1024 * 5];
int len;
while ((len =inBuff.read(b)) != -1) {
outBuff.write(b, 0, len);
}
outBuff.flush();
inBuff.close();
outBuff.close();
output.close();
input.close();
}
public static PageOfficeCtrl openFile(HttpServletRequest request) {
PageOfficeCtrl poCtrl=new PageOfficeCtrl(request);
poCtrl.setServerPage(request.getContextPath() + "/poserver.zz");
poCtrl.setTitlebar(false);
poCtrl.setMenubar(false);
poCtrl.setOfficeToolbars(false);
poCtrl.setCustomToolbar(false);
return poCtrl;
}
public static PDFCtrl openPdfFile(HttpServletRequest request) {
PDFCtrl pdfCtrl1 = new PDFCtrl(request);
pdfCtrl1.setServerPage(request.getContextPath()+"/poserver.zz");
return pdfCtrl1;
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】凌霞软件回馈社区,携手博客园推出1Panel与Halo联合会员
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从零实现富文本编辑器#3-基于Delta的线性数据结构模型
· 记一次 .NET某旅行社酒店管理系统 卡死分析
· 长文讲解 MCP 和案例实战
· Hangfire Redis 实现秒级定时任务,使用 CQRS 实现动态执行代码
· Android编译时动态插入代码原理与实践
· 使用TypeScript开发微信小程序(云开发)-入门篇
· 没几个人需要了解的JDK知识,我却花了3天时间研究
· C#高性能开发之类型系统:从 C# 7.0 到 C# 14 的类型系统演进全景
· 在SqlSugar的开发框架中增加对低代码EAV模型(实体-属性-值)的WebAPI实现支持
· 管理100个小程序-很难吗