From cd7767932dddeaf6d9c73a83d4a9b38f0341b77f Mon Sep 17 00:00:00 2001 From: admin <weikou2014> Date: 星期五, 28 一月 2022 12:46:17 +0800 Subject: [PATCH] bug修复 --- src/main/java/com/yeshi/buwan/util/FileUtil.java | 702 +++++++++++++++++++++++++++++----------------------------- 1 files changed, 352 insertions(+), 350 deletions(-) diff --git a/src/main/java/com/yeshi/buwan/util/FileUtil.java b/src/main/java/com/yeshi/buwan/util/FileUtil.java index 5d4597d..6576970 100644 --- a/src/main/java/com/yeshi/buwan/util/FileUtil.java +++ b/src/main/java/com/yeshi/buwan/util/FileUtil.java @@ -13,10 +13,7 @@ import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import javax.persistence.Entity; import javax.persistence.GeneratedValue; @@ -34,403 +31,408 @@ //鏂囦欢澶勭悊 @Entity public class FileUtil { - public final static int TYPE_PICTURE_CLASS = 1;// 鍒嗙被鍥炬爣鏂囦欢澶� - public final static int TYPE_PICTURE_HOME_AD = 2;// 骞垮憡鍥剧墖 - public final static int TYPE_PICTURE_VIDEO = 3;// 瑙嗛鍥剧墖鏂囦欢 - public final static int TYPE_PICTURE_STAR = 4;// 鏄庢槦澶村儚 - @Id - @GeneratedValue - public final static int TYPE_VIDEO = 5;// 瑙嗛鏂囦欢 - public final static String PROJECT_NAME = "BuWan"; + public final static int TYPE_PICTURE_CLASS = 1;// 鍒嗙被鍥炬爣鏂囦欢澶� + public final static int TYPE_PICTURE_HOME_AD = 2;// 骞垮憡鍥剧墖 + public final static int TYPE_PICTURE_VIDEO = 3;// 瑙嗛鍥剧墖鏂囦欢 + public final static int TYPE_PICTURE_STAR = 4;// 鏄庢槦澶村儚 + @Id + @GeneratedValue + public final static int TYPE_VIDEO = 5;// 瑙嗛鏂囦欢 + public final static String PROJECT_NAME = "BuWan"; - /** - * 涓婁紶澶村儚 - * - * @param type - * @param request - * @return - */ - public List<String> getFile(int type, HttpServletRequest request) throws Exception { - List<String> resltList = new ArrayList<String>(); - DiskFileItemFactory factory = new DiskFileItemFactory(); - // 鑾峰彇鏂囦欢闇�瑕佷笂浼犲埌鐨勮矾寰� - String path = request.getRealPath("/upload"); + /** + * 涓婁紶澶村儚 + * + * @param type + * @param request + * @return + */ + public List<String> getFile(int type, HttpServletRequest request) throws Exception { + List<String> resltList = new ArrayList<String>(); + DiskFileItemFactory factory = new DiskFileItemFactory(); + // 鑾峰彇鏂囦欢闇�瑕佷笂浼犲埌鐨勮矾寰� + String path = request.getRealPath("/upload"); - // 濡傛灉娌′互涓嬩袱琛岃缃殑璇濓紝涓婁紶澶х殑 鏂囦欢 浼氬崰鐢� 寰堝鍐呭瓨锛� - // 璁剧疆鏆傛椂瀛樻斁鐨� 瀛樺偍瀹� , 杩欎釜瀛樺偍瀹わ紝鍙互鍜� 鏈�缁堝瓨鍌ㄦ枃浠� 鐨勭洰褰曚笉鍚� - /** - * 鍘熺悊 瀹冩槸鍏堝瓨鍒� 鏆傛椂瀛樺偍瀹わ紝鐒跺悗鍦ㄧ湡姝e啓鍒� 瀵瑰簲鐩綍鐨勭‖鐩樹笂锛� 鎸夌悊鏉ヨ 褰撲笂浼犱竴涓枃浠舵椂锛屽叾瀹炴槸涓婁紶浜嗕袱浠斤紝绗竴涓槸浠� .tem - * 鏍煎紡鐨� 鐒跺悗鍐嶅皢鍏剁湡姝e啓鍒� 瀵瑰簲鐩綍鐨勭‖鐩樹笂 - */ - factory.setRepository(new File(path)); - // 璁剧疆 缂撳瓨鐨勫ぇ灏忥紝褰撲笂浼犳枃浠剁殑瀹归噺瓒呰繃璇ョ紦瀛樻椂锛岀洿鎺ユ斁鍒� 鏆傛椂瀛樺偍瀹� - factory.setSizeThreshold(1024 * 1024); + // 濡傛灉娌′互涓嬩袱琛岃缃殑璇濓紝涓婁紶澶х殑 鏂囦欢 浼氬崰鐢� 寰堝鍐呭瓨锛� + // 璁剧疆鏆傛椂瀛樻斁鐨� 瀛樺偍瀹� , 杩欎釜瀛樺偍瀹わ紝鍙互鍜� 鏈�缁堝瓨鍌ㄦ枃浠� 鐨勭洰褰曚笉鍚� + /** + * 鍘熺悊 瀹冩槸鍏堝瓨鍒� 鏆傛椂瀛樺偍瀹わ紝鐒跺悗鍦ㄧ湡姝e啓鍒� 瀵瑰簲鐩綍鐨勭‖鐩樹笂锛� 鎸夌悊鏉ヨ 褰撲笂浼犱竴涓枃浠舵椂锛屽叾瀹炴槸涓婁紶浜嗕袱浠斤紝绗竴涓槸浠� .tem + * 鏍煎紡鐨� 鐒跺悗鍐嶅皢鍏剁湡姝e啓鍒� 瀵瑰簲鐩綍鐨勭‖鐩樹笂 + */ + factory.setRepository(new File(path)); + // 璁剧疆 缂撳瓨鐨勫ぇ灏忥紝褰撲笂浼犳枃浠剁殑瀹归噺瓒呰繃璇ョ紦瀛樻椂锛岀洿鎺ユ斁鍒� 鏆傛椂瀛樺偍瀹� + factory.setSizeThreshold(1024 * 1024); - // 楂樻按骞崇殑API鏂囦欢涓婁紶澶勭悊 - ServletFileUpload upload = new ServletFileUpload(factory); + // 楂樻按骞崇殑API鏂囦欢涓婁紶澶勭悊 + ServletFileUpload upload = new ServletFileUpload(factory); - try { - // 鍙互涓婁紶澶氫釜鏂囦欢 - List<FileItem> list = (List<FileItem>) upload.parseRequest(request); + try { + // 鍙互涓婁紶澶氫釜鏂囦欢 + List<FileItem> list = (List<FileItem>) upload.parseRequest(request); - for (FileItem item : list) { - // 鑾峰彇琛ㄥ崟鐨勫睘鎬у悕瀛� - String name = item.getFieldName(); + for (FileItem item : list) { + // 鑾峰彇琛ㄥ崟鐨勫睘鎬у悕瀛� + String name = item.getFieldName(); - // 濡傛灉鑾峰彇鐨� 琛ㄥ崟淇℃伅鏄櫘閫氱殑 鏂囨湰 淇℃伅 - if (item.isFormField()) { + // 濡傛灉鑾峰彇鐨� 琛ㄥ崟淇℃伅鏄櫘閫氱殑 鏂囨湰 淇℃伅 + if (item.isFormField()) { - } - // 瀵逛紶鍏ョ殑闈� 绠�鍗曠殑瀛楃涓茶繘琛屽鐞� 锛屾瘮濡傝浜岃繘鍒剁殑 鍥剧墖锛岀數褰辫繖浜� - else { - /** - * 浠ヤ笅涓夋锛屼富瑕佽幏鍙� 涓婁紶鏂囦欢鐨勫悕瀛� - */ - // 鑾峰彇璺緞鍚� - String value = item.getName(); - // 绱㈠紩鍒版渶鍚庝竴涓弽鏂滄潬 - int start = value.lastIndexOf("\\"); - // 鎴彇 涓婁紶鏂囦欢鐨� 瀛楃涓插悕瀛楋紝鍔�1鏄� 鍘绘帀鍙嶆枩鏉狅紝 - String filename = value.substring(start + 1); + } + // 瀵逛紶鍏ョ殑闈� 绠�鍗曠殑瀛楃涓茶繘琛屽鐞� 锛屾瘮濡傝浜岃繘鍒剁殑 鍥剧墖锛岀數褰辫繖浜� + else { + /** + * 浠ヤ笅涓夋锛屼富瑕佽幏鍙� 涓婁紶鏂囦欢鐨勫悕瀛� + */ + // 鑾峰彇璺緞鍚� + String value = item.getName(); + // 绱㈠紩鍒版渶鍚庝竴涓弽鏂滄潬 + int start = value.lastIndexOf("\\"); + // 鎴彇 涓婁紶鏂囦欢鐨� 瀛楃涓插悕瀛楋紝鍔�1鏄� 鍘绘帀鍙嶆枩鏉狅紝 + String filename = value.substring(start + 1); - request.setAttribute(name, filename); + request.setAttribute(name, filename); - File file = new File(path); + File file = new File(path); - if (!file.exists()) - file.mkdirs(); + if (!file.exists()) + file.mkdirs(); - File f = new File(createFile(type, path), filename); + File f = new File(createFile(type, path), filename); - resltList.add(f.getPath().split("webapps")[1]); + resltList.add(f.getPath().split("webapps")[1]); - OutputStream out = new FileOutputStream(f); + OutputStream out = new FileOutputStream(f); - InputStream in = item.getInputStream(); + InputStream in = item.getInputStream(); - int length = 0; - byte[] buf = new byte[1024]; + int length = 0; + byte[] buf = new byte[1024]; - LogUtil.i("鑾峰彇涓婁紶鏂囦欢鐨勬�诲叡鐨勫閲忥細" + item.getSize()); + LogUtil.i("鑾峰彇涓婁紶鏂囦欢鐨勬�诲叡鐨勫閲忥細" + item.getSize()); - // in.read(buf) 姣忔璇诲埌鐨勬暟鎹瓨鏀惧湪 buf 鏁扮粍涓� - while ((length = in.read(buf)) != -1) { - // 鍦� buf 鏁扮粍涓� 鍙栧嚭鏁版嵁 鍐欏埌 锛堣緭鍑烘祦锛夌鐩樹笂 - out.write(buf, 0, length); - } + // in.read(buf) 姣忔璇诲埌鐨勬暟鎹瓨鏀惧湪 buf 鏁扮粍涓� + while ((length = in.read(buf)) != -1) { + // 鍦� buf 鏁扮粍涓� 鍙栧嚭鏁版嵁 鍐欏埌 锛堣緭鍑烘祦锛夌鐩樹笂 + out.write(buf, 0, length); + } - in.close(); - out.close(); - } - } + in.close(); + out.close(); + } + } - } catch (FileUploadException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - return resltList; - } + } catch (FileUploadException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return resltList; + } - public Map<String, String> getTextAndFile(int type, HttpServletRequest request) throws Exception { - Map<String, String> map = new HashMap<String, String>(); - DiskFileItemFactory factory = new DiskFileItemFactory(); + public Map<String, String> getTextAndFile(int type, HttpServletRequest request) throws Exception { + Map<String, String> map = new HashMap<String, String>(); + DiskFileItemFactory factory = new DiskFileItemFactory(); - // 鑾峰彇鏂囦欢闇�瑕佷笂浼犲埌鐨勮矾寰� - String path = request.getRealPath("/upload"); + // 鑾峰彇鏂囦欢闇�瑕佷笂浼犲埌鐨勮矾寰� + String path = request.getRealPath("/upload"); - // 濡傛灉娌′互涓嬩袱琛岃缃殑璇濓紝涓婁紶澶х殑 鏂囦欢 浼氬崰鐢� 寰堝鍐呭瓨锛� - // 璁剧疆鏆傛椂瀛樻斁鐨� 瀛樺偍瀹� , 杩欎釜瀛樺偍瀹わ紝鍙互鍜� 鏈�缁堝瓨鍌ㄦ枃浠� 鐨勭洰褰曚笉鍚� - /** - * 鍘熺悊 瀹冩槸鍏堝瓨鍒� 鏆傛椂瀛樺偍瀹わ紝鐒跺悗鍦ㄧ湡姝e啓鍒� 瀵瑰簲鐩綍鐨勭‖鐩樹笂锛� 鎸夌悊鏉ヨ 褰撲笂浼犱竴涓枃浠舵椂锛屽叾瀹炴槸涓婁紶浜嗕袱浠斤紝绗竴涓槸浠� .tem - * 鏍煎紡鐨� 鐒跺悗鍐嶅皢鍏剁湡姝e啓鍒� 瀵瑰簲鐩綍鐨勭‖鐩樹笂 - */ - factory.setRepository(new File(path)); - // 璁剧疆 缂撳瓨鐨勫ぇ灏忥紝褰撲笂浼犳枃浠剁殑瀹归噺瓒呰繃璇ョ紦瀛樻椂锛岀洿鎺ユ斁鍒� 鏆傛椂瀛樺偍瀹� - factory.setSizeThreshold(1024 * 1024); + // 濡傛灉娌′互涓嬩袱琛岃缃殑璇濓紝涓婁紶澶х殑 鏂囦欢 浼氬崰鐢� 寰堝鍐呭瓨锛� + // 璁剧疆鏆傛椂瀛樻斁鐨� 瀛樺偍瀹� , 杩欎釜瀛樺偍瀹わ紝鍙互鍜� 鏈�缁堝瓨鍌ㄦ枃浠� 鐨勭洰褰曚笉鍚� + /** + * 鍘熺悊 瀹冩槸鍏堝瓨鍒� 鏆傛椂瀛樺偍瀹わ紝鐒跺悗鍦ㄧ湡姝e啓鍒� 瀵瑰簲鐩綍鐨勭‖鐩樹笂锛� 鎸夌悊鏉ヨ 褰撲笂浼犱竴涓枃浠舵椂锛屽叾瀹炴槸涓婁紶浜嗕袱浠斤紝绗竴涓槸浠� .tem + * 鏍煎紡鐨� 鐒跺悗鍐嶅皢鍏剁湡姝e啓鍒� 瀵瑰簲鐩綍鐨勭‖鐩樹笂 + */ + factory.setRepository(new File(path)); + // 璁剧疆 缂撳瓨鐨勫ぇ灏忥紝褰撲笂浼犳枃浠剁殑瀹归噺瓒呰繃璇ョ紦瀛樻椂锛岀洿鎺ユ斁鍒� 鏆傛椂瀛樺偍瀹� + factory.setSizeThreshold(1024 * 1024); - // 楂樻按骞崇殑API鏂囦欢涓婁紶澶勭悊 - ServletFileUpload upload = new ServletFileUpload(factory); - upload.setHeaderEncoding("UTF-8"); + // 楂樻按骞崇殑API鏂囦欢涓婁紶澶勭悊 + ServletFileUpload upload = new ServletFileUpload(factory); + upload.setHeaderEncoding("UTF-8"); - // 鍙互涓婁紶澶氫釜鏂囦欢 - List<FileItem> list = (List<FileItem>) upload.parseRequest(request); - for (FileItem item : list) { - // 鑾峰彇琛ㄥ崟鐨勫睘鎬у悕瀛� - String name = item.getFieldName(); + // 鍙互涓婁紶澶氫釜鏂囦欢 + List<FileItem> list = (List<FileItem>) upload.parseRequest(request); + for (FileItem item : list) { + // 鑾峰彇琛ㄥ崟鐨勫睘鎬у悕瀛� + String name = item.getFieldName(); - // 濡傛灉鑾峰彇鐨� 琛ㄥ崟淇℃伅鏄櫘閫氱殑 鏂囨湰 淇℃伅 - if (item.isFormField()) { - // String value = new String(item.getString().getBytes(), - // "UTF-8"); - map.put(name, item.getString("UTF-8")); - } - // 瀵逛紶鍏ョ殑闈� 绠�鍗曠殑瀛楃涓茶繘琛屽鐞� 锛屾瘮濡傝浜岃繘鍒剁殑 鍥剧墖锛岀數褰辫繖浜� - else { - /** - * 浠ヤ笅涓夋锛屼富瑕佽幏鍙� 涓婁紶鏂囦欢鐨勫悕瀛� - */ - // 鑾峰彇璺緞鍚� - String value = item.getName(); - if (StringUtil.isNullOrEmpty(value)) - continue; - // 绱㈠紩鍒版渶鍚庝竴涓弽鏂滄潬 - int start = value.lastIndexOf("\\"); - // .闇�瑕佽浆涔� - String end = value.split("\\.")[1]; - // 鎴彇 涓婁紶鏂囦欢鐨� 瀛楃涓插悕瀛楋紝鍔�1鏄� 鍘绘帀鍙嶆枩鏉狅紝 - String filename = ""; - if (value.contains(".") && value.length() > 2) { - filename = System.currentTimeMillis() + (int) (Math.random() * 10000) + "." + end; - request.setAttribute(name, filename); - } - File file = new File(path); + // 濡傛灉鑾峰彇鐨� 琛ㄥ崟淇℃伅鏄櫘閫氱殑 鏂囨湰 淇℃伅 + if (item.isFormField()) { + // String value = new String(item.getString().getBytes(), + // "UTF-8"); + map.put(name, item.getString("UTF-8")); + } + // 瀵逛紶鍏ョ殑闈� 绠�鍗曠殑瀛楃涓茶繘琛屽鐞� 锛屾瘮濡傝浜岃繘鍒剁殑 鍥剧墖锛岀數褰辫繖浜� + else { + /** + * 浠ヤ笅涓夋锛屼富瑕佽幏鍙� 涓婁紶鏂囦欢鐨勫悕瀛� + */ + // 鑾峰彇璺緞鍚� + String value = item.getName(); + if (StringUtil.isNullOrEmpty(value)) + continue; + // 绱㈠紩鍒版渶鍚庝竴涓弽鏂滄潬 + int start = value.lastIndexOf("\\"); + // .闇�瑕佽浆涔� + String end = value.split("\\.")[1]; + // 鎴彇 涓婁紶鏂囦欢鐨� 瀛楃涓插悕瀛楋紝鍔�1鏄� 鍘绘帀鍙嶆枩鏉狅紝 + String filename = ""; + if (value.contains(".") && value.length() > 2) { + filename = System.currentTimeMillis() + (int) (Math.random() * 10000) + "." + end; + request.setAttribute(name, filename); + } + File file = new File(path); - if (!file.exists()) - file.mkdirs(); + if (!file.exists()) + file.mkdirs(); - String createFile = createFile(type, path); - File f = new File(createFile, filename); + String createFile = createFile(type, path); + File f = new File(createFile, filename); - String fileName = createFile.split(PROJECT_NAME)[1] + File.separator + filename; - fileName = fileName.replace("\\", "/"); - map.put(name, fileName); + String fileName = createFile.split(PROJECT_NAME)[1] + File.separator + filename; + fileName = fileName.replace("\\", "/"); + map.put(name, fileName); - OutputStream out = new FileOutputStream(f); + OutputStream out = new FileOutputStream(f); - InputStream in = item.getInputStream(); + InputStream in = item.getInputStream(); - int length = 0; - byte[] buf = new byte[1024]; + int length = 0; + byte[] buf = new byte[1024]; - LogUtil.i("鑾峰彇涓婁紶鏂囦欢鐨勬�诲叡鐨勫閲忥細" + item.getSize()); + LogUtil.i("鑾峰彇涓婁紶鏂囦欢鐨勬�诲叡鐨勫閲忥細" + item.getSize()); - // in.read(buf) 姣忔璇诲埌鐨勬暟鎹瓨鏀惧湪 buf 鏁扮粍涓� - while ((length = in.read(buf)) != -1) { - // 鍦� buf 鏁扮粍涓� 鍙栧嚭鏁版嵁 鍐欏埌 锛堣緭鍑烘祦锛夌鐩樹笂 - out.write(buf, 0, length); - } + // in.read(buf) 姣忔璇诲埌鐨勬暟鎹瓨鏀惧湪 buf 鏁扮粍涓� + while ((length = in.read(buf)) != -1) { + // 鍦� buf 鏁扮粍涓� 鍙栧嚭鏁版嵁 鍐欏埌 锛堣緭鍑烘祦锛夌鐩樹笂 + out.write(buf, 0, length); + } - in.close(); - out.close(); - } - } + in.close(); + out.close(); + } + } - return map; - } + return map; + } - // 鍒涘缓鏂囦欢澶� - private String createFile(int type, String path) { - if (type == TYPE_PICTURE_CLASS)// 鍒嗙被鍥剧墖 - { - File f = new File(path, "class"); - if (!f.exists()) - f.mkdirs(); - return f.getPath(); - } else if (type == TYPE_PICTURE_HOME_AD)// 棣栭〉骞垮憡鍥剧墖 - { - File f = new File(path, "ad"); - if (!f.exists()) - f.mkdirs(); - return f.getPath(); - } else if (type == TYPE_PICTURE_STAR)// 鏄庢槦澶村儚 - { - File ff = new File(path, "star"); - if (!ff.exists()) - ff.mkdirs(); - File f = new File(ff.getPath(), "picture"); - if (!f.exists()) - f.mkdirs(); - return f.getPath(); - } else if (type == TYPE_PICTURE_VIDEO)// 瑙嗛灏侀潰 - { - File ff = new File(path, "juhe"); - if (!ff.exists()) - ff.mkdirs(); - File f = new File(ff.getPath(), "picture"); - if (!f.exists()) - f.mkdirs(); - return f.getPath(); - } else if (type == TYPE_VIDEO)// 瑙嗛灏侀潰 - { - File ff = new File(path, "juhe"); - if (!ff.exists()) - ff.mkdirs(); - File f = new File(ff.getPath(), "juhe"); - if (!f.exists()) - f.mkdirs(); - return f.getPath(); - } - return ""; + // 鍒涘缓鏂囦欢澶� + private String createFile(int type, String path) { + if (type == TYPE_PICTURE_CLASS)// 鍒嗙被鍥剧墖 + { + File f = new File(path, "class"); + if (!f.exists()) + f.mkdirs(); + return f.getPath(); + } else if (type == TYPE_PICTURE_HOME_AD)// 棣栭〉骞垮憡鍥剧墖 + { + File f = new File(path, "ad"); + if (!f.exists()) + f.mkdirs(); + return f.getPath(); + } else if (type == TYPE_PICTURE_STAR)// 鏄庢槦澶村儚 + { + File ff = new File(path, "star"); + if (!ff.exists()) + ff.mkdirs(); + File f = new File(ff.getPath(), "picture"); + if (!f.exists()) + f.mkdirs(); + return f.getPath(); + } else if (type == TYPE_PICTURE_VIDEO)// 瑙嗛灏侀潰 + { + File ff = new File(path, "juhe"); + if (!ff.exists()) + ff.mkdirs(); + File f = new File(ff.getPath(), "picture"); + if (!f.exists()) + f.mkdirs(); + return f.getPath(); + } else if (type == TYPE_VIDEO)// 瑙嗛灏侀潰 + { + File ff = new File(path, "juhe"); + if (!ff.exists()) + ff.mkdirs(); + File f = new File(ff.getPath(), "juhe"); + if (!f.exists()) + f.mkdirs(); + return f.getPath(); + } + return ""; - } + } - public static String getAbsolutePathWithProjectName(HttpServletRequest request, String path) { - return "http://" + request.getLocalAddr() + ":" + request.getLocalPort() + path; - } + public static String getAbsolutePathWithProjectName(HttpServletRequest request, String path) { + return "http://" + request.getLocalAddr() + ":" + request.getLocalPort() + path; + } - public static void downFile(String urlStr, String filePath) throws MalformedURLException { - // 涓嬭浇缃戠粶鏂囦欢 - int bytesum = 0; - int byteread = 0; + public static void downFile(String urlStr, String filePath) throws MalformedURLException { + // 涓嬭浇缃戠粶鏂囦欢 + int bytesum = 0; + int byteread = 0; - URL url = new URL(urlStr); + URL url = new URL(urlStr); - try { - URLConnection conn = url.openConnection(); - InputStream inStream = conn.getInputStream(); - FileOutputStream fs = new FileOutputStream(filePath); + try { + URLConnection conn = url.openConnection(); + InputStream inStream = conn.getInputStream(); + FileOutputStream fs = new FileOutputStream(filePath); - byte[] buffer = new byte[1204]; - while ((byteread = inStream.read(buffer)) != -1) { - bytesum += byteread; - LogUtil.i(bytesum); - fs.write(buffer, 0, byteread); - } - } catch (FileNotFoundException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - } + byte[] buffer = new byte[1204]; + while ((byteread = inStream.read(buffer)) != -1) { + bytesum += byteread; + LogUtil.i(bytesum); + fs.write(buffer, 0, byteread); + } + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } - // 澶嶅埗鏂囦欢 - public static boolean copyFile(String fileFrom, String fileTo) { - try { - File f = new File(fileTo); - if (!f.exists()) - f.createNewFile(); + // 澶嶅埗鏂囦欢 + public static boolean copyFile(String fileFrom, String fileTo) { + try { + File f = new File(fileTo); + if (!f.exists()) + f.createNewFile(); - FileInputStream in = new java.io.FileInputStream(fileFrom); - FileOutputStream out = new FileOutputStream(fileTo); - byte[] bt = new byte[1024]; - int count; - while ((count = in.read(bt)) > 0) { - out.write(bt, 0, count); - } - in.close(); - out.close(); - return true; - } catch (IOException ex) { - return false; - } - } + FileInputStream in = new java.io.FileInputStream(fileFrom); + FileOutputStream out = new FileOutputStream(fileTo); + byte[] bt = new byte[1024]; + int count; + while ((count = in.read(bt)) > 0) { + out.write(bt, 0, count); + } + in.close(); + out.close(); + return true; + } catch (IOException ex) { + return false; + } + } - public static boolean save(String ht, String path) throws IOException { - try { - File f = new File(path); - if (!f.exists()) - f.createNewFile(); - FileWriter writer; - writer = new FileWriter(path); - writer.write(ht); - writer.flush(); - writer.close(); - return true; - } catch (Exception e) { - e.printStackTrace(); - } - return false; - } + public static boolean save(String ht, String path) throws IOException { + try { + File f = new File(path); + if (!f.exists()) + f.createNewFile(); + FileWriter writer; + writer = new FileWriter(path); + writer.write(ht); + writer.flush(); + writer.close(); + return true; + } catch (Exception e) { + e.printStackTrace(); + } + return false; + } - public static List<FileName> reNameFile(String dir, String path) { - if (dir.endsWith("/")) - dir = dir.substring(0, dir.length() - 1); - VideoManager manager = new VideoManager(); - List<FileName> list = new ArrayList<FileName>(); - if (!StringUtil.isNullOrEmpty(dir)) - if (StringUtil.isNullOrEmpty(path)) {// 鑾峰彇鏂囦欢澶逛笅闈㈢殑鎵�鏈夋枃浠� - File f = new File(dir); - File[] fileList = f.listFiles(); - if (fileList.length > 0) { - String[] sts = fileList[0].getPath().split("\\."); + public static List<FileName> reNameFile(String dir, String path) { + if (dir.endsWith("/")) + dir = dir.substring(0, dir.length() - 1); + VideoManager manager = new VideoManager(); + List<FileName> list = new ArrayList<FileName>(); + if (!StringUtil.isNullOrEmpty(dir)) + if (StringUtil.isNullOrEmpty(path)) {// 鑾峰彇鏂囦欢澶逛笅闈㈢殑鎵�鏈夋枃浠� + File f = new File(dir); + File[] fileList = f.listFiles(); + if (fileList.length > 0) { + String[] sts = fileList[0].getPath().split("\\."); - for (int i = 0; i < fileList.length; i++) { - String[] files = fileList[i].getPath().split("/"); - FileName name = new FileName(); - name.setNewName( - dir + "/" + manager.getVideoPathName(sts[sts.length - 1], files[files.length - 1])); - name.setOldName(files[files.length - 1]); - list.add(name); - fileList[i].renameTo(new File(list.get(i).getNewName())); - LogUtil.i(name.getOldName()); - LogUtil.i(name.getNewName()); - } - } - } else {// 鑾峰彇鏌愪竴涓枃浠� - String[] sts = path.split("\\."); - String newPath = dir + "/" + manager.getVideoPathName(sts[sts.length - 1], path); - new File(dir + "/" + path).renameTo(new File(newPath)); - FileName name = new FileName(); - name.setNewName(newPath); - name.setOldName(path); - list.add(name); - } - return list; - } + for (int i = 0; i < fileList.length; i++) { + String[] files = fileList[i].getPath().split("/"); + FileName name = new FileName(); + name.setNewName( + dir + "/" + manager.getVideoPathName(sts[sts.length - 1], files[files.length - 1])); + name.setOldName(files[files.length - 1]); + list.add(name); + fileList[i].renameTo(new File(list.get(i).getNewName())); + LogUtil.i(name.getOldName()); + LogUtil.i(name.getNewName()); + } + } + } else {// 鑾峰彇鏌愪竴涓枃浠� + String[] sts = path.split("\\."); + String newPath = dir + "/" + manager.getVideoPathName(sts[sts.length - 1], path); + new File(dir + "/" + path).renameTo(new File(newPath)); + FileName name = new FileName(); + name.setNewName(newPath); + name.setOldName(path); + list.add(name); + } + return list; + } - public static List<String> readText(String filepath) throws IOException { - List<String> list = new ArrayList<String>(); - // String path = "C:/Users/Administrator/Desktop/涔愯浜�/1234.txt";// 鐩稿璺緞 - File filename = new File(filepath); - BufferedReader bufread = new BufferedReader(new FileReader(filename)); - String read; - while ((read = bufread.readLine()) != null) { - LogUtil.i(read); - list.add(read); - // readStr = (readStr + read); - } - // LogUtil.i("璇诲彇閰嶇疆鏂囦欢淇℃伅锛�" + readStr); - return list; - } + public static List<String> readText(String filepath) throws IOException { + List<String> list = new ArrayList<String>(); + // String path = "C:/Users/Administrator/Desktop/涔愯浜�/1234.txt";// 鐩稿璺緞 + File filename = new File(filepath); + BufferedReader bufread = new BufferedReader(new FileReader(filename)); + String read; + while ((read = bufread.readLine()) != null) { + LogUtil.i(read); + list.add(read); + // readStr = (readStr + read); + } + // LogUtil.i("璇诲彇閰嶇疆鏂囦欢淇℃伅锛�" + readStr); + return list; + } - public static String readTxt(String filepath) throws IOException { - String result = ""; - File filename = new File(filepath); - BufferedReader bufread = new BufferedReader(new FileReader(filename)); - String read; - while ((read = bufread.readLine()) != null) { - LogUtil.i(read); - result += read; - // readStr = (readStr + read); - } - // LogUtil.i("璇诲彇閰嶇疆鏂囦欢淇℃伅锛�" + readStr); - return result; - } + public static String readTxt(String filepath) throws IOException { + String result = ""; + File filename = new File(filepath); + BufferedReader bufread = new BufferedReader(new FileReader(filename)); + String read; + while ((read = bufread.readLine()) != null) { + LogUtil.i(read); + result += read; + // readStr = (readStr + read); + } + // LogUtil.i("璇诲彇閰嶇疆鏂囦欢淇℃伅锛�" + readStr); + return result; + } - public static String saveAsFile(InputStream inputStream, String path) throws IOException { - if (!new File(path).exists()) { - new File(path).createNewFile(); - } + public static String saveAsFile(InputStream inputStream, String path) throws IOException { + if (!new File(path).exists()) { + new File(path).createNewFile(); + } - FileOutputStream fileOut = new FileOutputStream(new File(path)); - byte[] buf = new byte[1024 * 8]; - while (true) { - int read = 0; - if (inputStream != null) { - read = inputStream.read(buf); - } - if (read == -1) { - break; - } - fileOut.write(buf, 0, read); - } - fileOut.close(); - return path; - } + FileOutputStream fileOut = new FileOutputStream(new File(path)); + byte[] buf = new byte[1024 * 8]; + while (true) { + int read = 0; + if (inputStream != null) { + read = inputStream.read(buf); + } + if (read == -1) { + break; + } + fileOut.write(buf, 0, read); + } + fileOut.close(); + return path; + } - public static String getCacheDir() { + public static String getCacheDir() { - String os = System.getProperty("os.name"); - if (os.toLowerCase().startsWith("win")) { - File f = new File("D:/cache"); - if (!f.exists()) - f.mkdirs(); - return f.getPath(); - } else { - File f = new File("/usr/local/cache"); - if (!f.exists()) - f.mkdirs(); - return f.getPath(); - } - } + String os = System.getProperty("os.name"); + if (os.toLowerCase().startsWith("win")) { + File f = new File("D:/cache"); + if (!f.exists()) + f.mkdirs(); + return f.getPath(); + } else { + File f = new File("/usr/local/cache"); + if (!f.exists()) + f.mkdirs(); + return f.getPath(); + } + } + + + + + } -- Gitblit v1.8.0