From baff08729fbd460849b624a88d6d810ff56e3c71 Mon Sep 17 00:00:00 2001 From: fangcheng Date: Fri, 19 May 2017 22:19:57 +0800 Subject: [PATCH] 1 --- .../src/main/java/com/lyms/util/FileUtils.java | 187 +++++++++++++++++++++ .../src/main/java/com/lyms/util/JsonUtils.java | 116 +++++++++++-- 2 files changed, 286 insertions(+), 17 deletions(-) create mode 100644 parent/core.sdk/src/main/java/com/lyms/util/FileUtils.java diff --git a/parent/core.sdk/src/main/java/com/lyms/util/FileUtils.java b/parent/core.sdk/src/main/java/com/lyms/util/FileUtils.java new file mode 100644 index 0000000..19fc61b --- /dev/null +++ b/parent/core.sdk/src/main/java/com/lyms/util/FileUtils.java @@ -0,0 +1,187 @@ +package com.lyms.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.io.InputStream; +import java.io.OutputStream; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; + +public class FileUtils { + + public static void main(String[] args) throws IOException { + copyFile("E:\\upload\\create\\1436144988371_JL33041594.xml", "E:\\test\\upload"); + // deleteFile("E:\\test\\upload\\"); + } + + /** + * 移动 文件或者文件夹 + * @param oldPath + * @param newPath + * @throws IOException + */ + public static void moveTo(String oldPath, String newPath) throws IOException { + copyFile(oldPath, newPath); + deleteFile(oldPath); + } + + /** + * 删除 文件或者文件夹 + * @param filePath + */ + public static void deleteFile(String filePath) { + File file = new File(filePath); + if (!file.exists()) { + return; + } + if (file.isDirectory()) { + File[] list = file.listFiles(); + + for (File f : list) { + deleteFile(f.getAbsolutePath()); + } + } + file.delete(); + } + + /** + * 复制 文件或者文件夹 + * @param oldPath + * @param newPath + * @throws IOException + */ + public static void copyFile(String oldPath, String newPath) throws IOException { + System.out.println("copy file from [" + oldPath + "] to [" + newPath + "]"); + + File oldFile = new File(oldPath); + if (oldFile.exists()) { + + if (oldFile.isDirectory()) { // 如果是文件夹 + File newPathDir = new File(newPath); + newPathDir.mkdirs(); + File[] lists = oldFile.listFiles(); + if (lists != null && lists.length > 0) { + for (File file : lists) { + copyFile(file.getAbsolutePath(), newPath.endsWith(File.separator) ? newPath + file.getName() + : newPath + File.separator + file.getName()); + } + } + } else { + InputStream inStream = new FileInputStream(oldFile); //读入原文件 + FileOutputStream fs = new FileOutputStream(newPath); + write2Out(inStream, fs); + inStream.close(); + } + } + } + + /** + * 重命名文件 + * @param file + * @param name + * @return + */ + public static File renameFile(File file, String name) { + String fileName = file.getParent() + File.separator + name; + File dest = new File(fileName); + file.renameTo(dest); + return dest; + } + + /** + * 压缩多个文件。 + * @param zipFileName 压缩输出文件名 + * @param files 需要压缩的文件 + * @return + * @throws Exception + */ + public static File createZip(String zipFileName, File... files) throws Exception { + File outFile = new File(zipFileName); + ZipOutputStream out = null; + BufferedOutputStream bo = null; + try { + out = new ZipOutputStream(new FileOutputStream(outFile)); + bo = new BufferedOutputStream(out); + + for (File file : files) { + zip(out, file, file.getName(), bo); + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + bo.close(); + } finally { + out.close(); // 输出流关闭 + } + } + return outFile; + } + + /** + * + * @param zipFileName 压缩输出文件名 + * @param inputFile 需要压缩的文件 + * @return + * @throws Exception + */ + public static File createZip(String zipFileName, File inputFile) throws Exception { + File outFile = new File(zipFileName); + ZipOutputStream out = null; + BufferedOutputStream bo = null; + try { + out = new ZipOutputStream(new FileOutputStream(outFile)); + bo = new BufferedOutputStream(out); + zip(out, inputFile, inputFile.getName(), bo); + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + bo.close(); + } finally { + out.close(); // 输出流关闭 + } + } + return outFile; + } + + private static void zip(ZipOutputStream out, File f, String base, BufferedOutputStream bo) throws Exception { // 方法重载 + if (f.isDirectory()) { + File[] fl = f.listFiles(); + if (fl == null || fl.length == 0) { + out.putNextEntry(new ZipEntry(base + "/")); // 创建创建一个空的文件夹 + } else { + for (int i = 0; i < fl.length; i++) { + zip(out, fl[i], base + "/" + fl[i].getName(), bo); // 递归遍历子文件夹 + } + } + + } else { + out.putNextEntry(new ZipEntry(base)); // 创建zip压缩进入 base 文件 + System.out.println(base); + BufferedInputStream bi = new BufferedInputStream(new FileInputStream(f)); + + try { + write2Out(bi, out); + } catch (IOException e) { + //Ignore + } finally { + bi.close();// 输入流关闭 + } + } + } + + private static void write2Out(InputStream input, OutputStream out) throws IOException { + byte[] b = new byte[1024]; + int c = 0; + while ((c = input.read(b)) != -1) { + out.write(b, 0, c); + out.flush(); + } + out.flush(); + } +} diff --git a/parent/core.sdk/src/main/java/com/lyms/util/JsonUtils.java b/parent/core.sdk/src/main/java/com/lyms/util/JsonUtils.java index 67cf088..b43a6d2 100644 --- a/parent/core.sdk/src/main/java/com/lyms/util/JsonUtils.java +++ b/parent/core.sdk/src/main/java/com/lyms/util/JsonUtils.java @@ -1,9 +1,13 @@ package com.lyms.util; +import java.util.List; import java.util.Map; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; +import com.alibaba.fastjson.serializer.JSONLibDataFormatSerializer; +import com.alibaba.fastjson.serializer.SerializeConfig; +import com.alibaba.fastjson.serializer.SerializerFeature; /** *
  • @ClassName: JsonUtils @@ -14,26 +18,104 @@ import com.alibaba.fastjson.JSONObject; */ public class JsonUtils { - public static String beanToJson(T t) { - return JSON.toJSONString(t); - } + private static final SerializeConfig config; - /* - * @SuppressWarnings("unchecked") public static Map - * beanToMap(T t) { System.out.println(beanToJson(t)); return (Map) JSON.parse(beanToJson(t)); } - */ + static { + config = new SerializeConfig(); + config.put(java.util.Date.class, new JSONLibDataFormatSerializer()); // 使用和json-lib兼容的日期输出格式 + config.put(java.sql.Date.class, new JSONLibDataFormatSerializer()); // 使用和json-lib兼容的日期输出格式 + } - public static JSONObject beanToMap(T t) { - return JSON.parseObject(beanToJson(t)); - } + private static final SerializerFeature[] features = { SerializerFeature.WriteMapNullValue, // 输出空置字段 + SerializerFeature.WriteNullListAsEmpty, // list字段如果为null,输出为[],而不是null + SerializerFeature.WriteNullNumberAsZero, // 数值字段如果为null,输出为0,而不是null + SerializerFeature.WriteNullBooleanAsFalse, // Boolean字段如果为null,输出为false,而不是null + SerializerFeature.WriteNullStringAsEmpty // 字符类型字段如果为null,输出为"",而不是null + }; - public static T jsonToBean(String json, Class clazz) { - return JSON.parseObject(json, clazz); - } + public static String toJSONString(Object object) { + return JSON.toJSONString(object, config, features); + } - public static T mapToBean(Map map, Class clazz) { - return JSON.parseObject(JSON.toJSONString(map), clazz); - } + public static String toJSONNoFeatures(Object object) { + return JSON.toJSONString(object, config); + } + + public static Object toBean(String text) { + return JSON.parse(text); + } + + public static T toBean(String text, Class clazz) { + return JSON.parseObject(text, clazz); + } + + // 转换为数组 + public static Object[] toArray(String text) { + return toArray(text, null); + } + + // 转换为数组 + public static Object[] toArray(String text, Class clazz) { + return JSON.parseArray(text, clazz).toArray(); + } + + // 转换为List + public static List toList(String text, Class clazz) { + return JSON.parseArray(text, clazz); + } + + /** + * 将string转化为序列化的json字符串 + * @param keyvalue + * @return + */ + public static Object textToJson(String text) { + Object objectJson = JSON.parse(text); + return objectJson; + } + + /** + * json字符串转化为map + * @param s + * @return + */ + @SuppressWarnings("rawtypes") + public static Map stringToCollect(String s) { + Map m = JSONObject.parseObject(s); + return m; + } + + /** + * 将map转化为string + * @param m + * @return + */ + @SuppressWarnings("rawtypes") + public static String collectToString( Map m) { + String s = JSONObject.toJSONString(m); + return s; + } + + public static String beanToJson(T t) { + return JSON.toJSONString(t); + } + + /* + * @SuppressWarnings("unchecked") public static Map + * beanToMap(T t) { System.out.println(beanToJson(t)); return (Map) JSON.parse(beanToJson(t)); } + */ + + public static JSONObject beanToMap(T t) { + return JSON.parseObject(beanToJson(t)); + } + + public static T jsonToBean(String json, Class clazz) { + return JSON.parseObject(json, clazz); + } + + public static T mapToBean(Map map, Class clazz) { + return JSON.parseObject(JSON.toJSONString(map), clazz); + } } -- 1.8.3.1