Commit baff08729fbd460849b624a88d6d810ff56e3c71

Authored by fangcheng
1 parent 4b4a37c731
Exists in master

1

Showing 2 changed files with 286 additions and 17 deletions

parent/core.sdk/src/main/java/com/lyms/util/FileUtils.java View file @ baff087
  1 +package com.lyms.util;
  2 +
  3 +import java.io.BufferedInputStream;
  4 +import java.io.BufferedOutputStream;
  5 +import java.io.File;
  6 +import java.io.FileInputStream;
  7 +import java.io.FileOutputStream;
  8 +import java.io.IOException;
  9 +import java.io.InputStream;
  10 +import java.io.OutputStream;
  11 +import java.util.zip.ZipEntry;
  12 +import java.util.zip.ZipOutputStream;
  13 +
  14 +public class FileUtils {
  15 +
  16 + public static void main(String[] args) throws IOException {
  17 + copyFile("E:\\upload\\create\\1436144988371_JL33041594.xml", "E:\\test\\upload");
  18 + // deleteFile("E:\\test\\upload\\");
  19 + }
  20 +
  21 + /**
  22 + * 移动 文件或者文件夹
  23 + * @param oldPath
  24 + * @param newPath
  25 + * @throws IOException
  26 + */
  27 + public static void moveTo(String oldPath, String newPath) throws IOException {
  28 + copyFile(oldPath, newPath);
  29 + deleteFile(oldPath);
  30 + }
  31 +
  32 + /**
  33 + * 删除 文件或者文件夹
  34 + * @param filePath
  35 + */
  36 + public static void deleteFile(String filePath) {
  37 + File file = new File(filePath);
  38 + if (!file.exists()) {
  39 + return;
  40 + }
  41 + if (file.isDirectory()) {
  42 + File[] list = file.listFiles();
  43 +
  44 + for (File f : list) {
  45 + deleteFile(f.getAbsolutePath());
  46 + }
  47 + }
  48 + file.delete();
  49 + }
  50 +
  51 + /**
  52 + * 复制 文件或者文件夹
  53 + * @param oldPath
  54 + * @param newPath
  55 + * @throws IOException
  56 + */
  57 + public static void copyFile(String oldPath, String newPath) throws IOException {
  58 + System.out.println("copy file from [" + oldPath + "] to [" + newPath + "]");
  59 +
  60 + File oldFile = new File(oldPath);
  61 + if (oldFile.exists()) {
  62 +
  63 + if (oldFile.isDirectory()) { // 如果是文件夹
  64 + File newPathDir = new File(newPath);
  65 + newPathDir.mkdirs();
  66 + File[] lists = oldFile.listFiles();
  67 + if (lists != null && lists.length > 0) {
  68 + for (File file : lists) {
  69 + copyFile(file.getAbsolutePath(), newPath.endsWith(File.separator) ? newPath + file.getName()
  70 + : newPath + File.separator + file.getName());
  71 + }
  72 + }
  73 + } else {
  74 + InputStream inStream = new FileInputStream(oldFile); //读入原文件
  75 + FileOutputStream fs = new FileOutputStream(newPath);
  76 + write2Out(inStream, fs);
  77 + inStream.close();
  78 + }
  79 + }
  80 + }
  81 +
  82 + /**
  83 + * 重命名文件
  84 + * @param file
  85 + * @param name
  86 + * @return
  87 + */
  88 + public static File renameFile(File file, String name) {
  89 + String fileName = file.getParent() + File.separator + name;
  90 + File dest = new File(fileName);
  91 + file.renameTo(dest);
  92 + return dest;
  93 + }
  94 +
  95 + /**
  96 + * 压缩多个文件。
  97 + * @param zipFileName 压缩输出文件名
  98 + * @param files 需要压缩的文件
  99 + * @return
  100 + * @throws Exception
  101 + */
  102 + public static File createZip(String zipFileName, File... files) throws Exception {
  103 + File outFile = new File(zipFileName);
  104 + ZipOutputStream out = null;
  105 + BufferedOutputStream bo = null;
  106 + try {
  107 + out = new ZipOutputStream(new FileOutputStream(outFile));
  108 + bo = new BufferedOutputStream(out);
  109 +
  110 + for (File file : files) {
  111 + zip(out, file, file.getName(), bo);
  112 + }
  113 + } catch (Exception e) {
  114 + e.printStackTrace();
  115 + } finally {
  116 + try {
  117 + bo.close();
  118 + } finally {
  119 + out.close(); // 输出流关闭
  120 + }
  121 + }
  122 + return outFile;
  123 + }
  124 +
  125 + /**
  126 + *
  127 + * @param zipFileName 压缩输出文件名
  128 + * @param inputFile 需要压缩的文件
  129 + * @return
  130 + * @throws Exception
  131 + */
  132 + public static File createZip(String zipFileName, File inputFile) throws Exception {
  133 + File outFile = new File(zipFileName);
  134 + ZipOutputStream out = null;
  135 + BufferedOutputStream bo = null;
  136 + try {
  137 + out = new ZipOutputStream(new FileOutputStream(outFile));
  138 + bo = new BufferedOutputStream(out);
  139 + zip(out, inputFile, inputFile.getName(), bo);
  140 + } catch (Exception e) {
  141 + e.printStackTrace();
  142 + } finally {
  143 + try {
  144 + bo.close();
  145 + } finally {
  146 + out.close(); // 输出流关闭
  147 + }
  148 + }
  149 + return outFile;
  150 + }
  151 +
  152 + private static void zip(ZipOutputStream out, File f, String base, BufferedOutputStream bo) throws Exception { // 方法重载
  153 + if (f.isDirectory()) {
  154 + File[] fl = f.listFiles();
  155 + if (fl == null || fl.length == 0) {
  156 + out.putNextEntry(new ZipEntry(base + "/")); // 创建创建一个空的文件夹
  157 + } else {
  158 + for (int i = 0; i < fl.length; i++) {
  159 + zip(out, fl[i], base + "/" + fl[i].getName(), bo); // 递归遍历子文件夹
  160 + }
  161 + }
  162 +
  163 + } else {
  164 + out.putNextEntry(new ZipEntry(base)); // 创建zip压缩进入 base 文件
  165 + System.out.println(base);
  166 + BufferedInputStream bi = new BufferedInputStream(new FileInputStream(f));
  167 +
  168 + try {
  169 + write2Out(bi, out);
  170 + } catch (IOException e) {
  171 + //Ignore
  172 + } finally {
  173 + bi.close();// 输入流关闭
  174 + }
  175 + }
  176 + }
  177 +
  178 + private static void write2Out(InputStream input, OutputStream out) throws IOException {
  179 + byte[] b = new byte[1024];
  180 + int c = 0;
  181 + while ((c = input.read(b)) != -1) {
  182 + out.write(b, 0, c);
  183 + out.flush();
  184 + }
  185 + out.flush();
  186 + }
  187 +}
parent/core.sdk/src/main/java/com/lyms/util/JsonUtils.java View file @ baff087
1 1 package com.lyms.util;
2 2  
  3 +import java.util.List;
3 4 import java.util.Map;
4 5  
5 6 import com.alibaba.fastjson.JSON;
6 7 import com.alibaba.fastjson.JSONObject;
  8 +import com.alibaba.fastjson.serializer.JSONLibDataFormatSerializer;
  9 +import com.alibaba.fastjson.serializer.SerializeConfig;
  10 +import com.alibaba.fastjson.serializer.SerializerFeature;
7 11  
8 12 /**
9 13 * <li>@ClassName: JsonUtils
10 14  
11 15  
12 16  
13 17  
... ... @@ -14,27 +18,105 @@
14 18 */
15 19 public class JsonUtils {
16 20  
17   - public static <T> String beanToJson(T t) {
18   - return JSON.toJSONString(t);
19   - }
  21 + private static final SerializeConfig config;
20 22  
21   - /*
22   - * @SuppressWarnings("unchecked") public static <T> Map<String, Object>
23   - * beanToMap(T t) { System.out.println(beanToJson(t)); return (Map<String,
24   - * Object>) JSON.parse(beanToJson(t)); }
25   - */
  23 + static {
  24 + config = new SerializeConfig();
  25 + config.put(java.util.Date.class, new JSONLibDataFormatSerializer()); // 使用和json-lib兼容的日期输出格式
  26 + config.put(java.sql.Date.class, new JSONLibDataFormatSerializer()); // 使用和json-lib兼容的日期输出格式
  27 + }
26 28  
27   - public static <T> JSONObject beanToMap(T t) {
28   - return JSON.parseObject(beanToJson(t));
29   - }
  29 + private static final SerializerFeature[] features = { SerializerFeature.WriteMapNullValue, // 输出空置字段
  30 + SerializerFeature.WriteNullListAsEmpty, // list字段如果为null,输出为[],而不是null
  31 + SerializerFeature.WriteNullNumberAsZero, // 数值字段如果为null,输出为0,而不是null
  32 + SerializerFeature.WriteNullBooleanAsFalse, // Boolean字段如果为null,输出为false,而不是null
  33 + SerializerFeature.WriteNullStringAsEmpty // 字符类型字段如果为null,输出为"",而不是null
  34 + };
30 35  
31   - public static <T> T jsonToBean(String json, Class<T> clazz) {
32   - return JSON.parseObject(json, clazz);
33   - }
  36 + public static String toJSONString(Object object) {
  37 + return JSON.toJSONString(object, config, features);
  38 + }
34 39  
35   - public static <T> T mapToBean(Map<String, Object> map, Class<T> clazz) {
36   - return JSON.parseObject(JSON.toJSONString(map), clazz);
37   - }
  40 + public static String toJSONNoFeatures(Object object) {
  41 + return JSON.toJSONString(object, config);
  42 + }
  43 +
  44 + public static Object toBean(String text) {
  45 + return JSON.parse(text);
  46 + }
  47 +
  48 + public static <T> T toBean(String text, Class<T> clazz) {
  49 + return JSON.parseObject(text, clazz);
  50 + }
  51 +
  52 + // 转换为数组
  53 + public static <T> Object[] toArray(String text) {
  54 + return toArray(text, null);
  55 + }
  56 +
  57 + // 转换为数组
  58 + public static <T> Object[] toArray(String text, Class<T> clazz) {
  59 + return JSON.parseArray(text, clazz).toArray();
  60 + }
  61 +
  62 + // 转换为List
  63 + public static <T> List<T> toList(String text, Class<T> clazz) {
  64 + return JSON.parseArray(text, clazz);
  65 + }
  66 +
  67 + /**
  68 + * 将string转化为序列化的json字符串
  69 + * @param keyvalue
  70 + * @return
  71 + */
  72 + public static Object textToJson(String text) {
  73 + Object objectJson = JSON.parse(text);
  74 + return objectJson;
  75 + }
  76 +
  77 + /**
  78 + * json字符串转化为map
  79 + * @param s
  80 + * @return
  81 + */
  82 + @SuppressWarnings("rawtypes")
  83 + public static Map stringToCollect(String s) {
  84 + Map m = JSONObject.parseObject(s);
  85 + return m;
  86 + }
  87 +
  88 + /**
  89 + * 将map转化为string
  90 + * @param m
  91 + * @return
  92 + */
  93 + @SuppressWarnings("rawtypes")
  94 + public static String collectToString( Map m) {
  95 + String s = JSONObject.toJSONString(m);
  96 + return s;
  97 + }
  98 +
  99 + public static <T> String beanToJson(T t) {
  100 + return JSON.toJSONString(t);
  101 + }
  102 +
  103 + /*
  104 + * @SuppressWarnings("unchecked") public static <T> Map<String, Object>
  105 + * beanToMap(T t) { System.out.println(beanToJson(t)); return (Map<String,
  106 + * Object>) JSON.parse(beanToJson(t)); }
  107 + */
  108 +
  109 + public static <T> JSONObject beanToMap(T t) {
  110 + return JSON.parseObject(beanToJson(t));
  111 + }
  112 +
  113 + public static <T> T jsonToBean(String json, Class<T> clazz) {
  114 + return JSON.parseObject(json, clazz);
  115 + }
  116 +
  117 + public static <T> T mapToBean(Map<String, Object> map, Class<T> clazz) {
  118 + return JSON.parseObject(JSON.toJSONString(map), clazz);
  119 + }
38 120  
39 121 }