From b2b5bdf6f42597e582da96aa56707e1f68d3936e Mon Sep 17 00:00:00 2001
From: wuxw <928255095@qq.com>
Date: 星期二, 10 五月 2022 11:41:44 +0800
Subject: [PATCH] 优化 费用
---
java110-generator/src/main/java/com/java110/code/util/FileUtilBase.java | 456 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 456 insertions(+), 0 deletions(-)
diff --git a/java110-generator/src/main/java/com/java110/code/util/FileUtilBase.java b/java110-generator/src/main/java/com/java110/code/util/FileUtilBase.java
new file mode 100755
index 0000000..0b75f08
--- /dev/null
+++ b/java110-generator/src/main/java/com/java110/code/util/FileUtilBase.java
@@ -0,0 +1,456 @@
+package com.java110.code.util;
+
+
+import java.io.BufferedReader;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.URL;
+import java.nio.ByteBuffer;
+import java.nio.channels.FileChannel;
+import java.util.Date;
+
+
+
+
+public class FileUtilBase {
+
+ public static void save(byte[] body, File file) throws IOException {
+ writeToFile(body, file);
+ }
+
+ public static void save(byte[] body, String fileName, String filePath)
+ throws IOException{
+ writeToFile(body, fileName, filePath);
+ }
+
+
+
+ /***************************************************************************
+ * 鍒犻櫎鎸囧畾鐩綍缁撴瀯
+ *
+ * @param directoryName
+ */
+ protected static void deleteFileDirectory(String filePath) {
+ /**
+ * 鎸囧畾鍒犻櫎鐩綍璺緞鏋勯�犱竴涓枃浠跺璞�
+ *
+ */
+ File file = new File(filePath);
+
+ File[] fileList = file.listFiles();
+ /**
+ * 鍒濆鍖栧瓙鐩綍璺緞
+ */
+ String dirPath = null;
+
+ if (fileList != null)
+ for (int i = 0; i < fileList.length; i++) {
+ /**
+ * 濡傛灉鏄枃浠跺氨灏嗗叾鍒犻櫎
+ */
+ if (fileList[i].isFile())
+ fileList[i].delete();
+ /**
+ * 濡傛灉鏄洰褰�,閭d箞灏嗕簺鐩綍涓嬫墍鏈夋枃浠跺垹闄ゅ悗鍐嶅皢鍏剁洰褰曞垹闄�,
+ */
+ if (fileList[i].isDirectory()) {
+
+ dirPath = fileList[i].getPath();
+ // 閫掑綊鍒犻櫎鎸囧畾鐩綍涓嬫墍鏈夋枃浠�
+ deleteFileDirectory(dirPath);
+ }
+ }
+ /**
+ * 鍒犻櫎缁欏畾鏍圭洰褰�
+ */
+ file.delete();
+ }
+
+ /**
+ * 鎶婃枃浠惰鍙栧埌byte[]涓�
+ *
+ * @param fileName
+ * @return
+ * @throws FileNotFoundException
+ */
+ protected static byte[] getFileByte(String fileName, boolean isDelete)
+ throws FileNotFoundException {
+ FileInputStream fileInputStream = new FileInputStream(fileName);
+ byte[] buffer = getFileByte(fileInputStream);
+ if (isDelete) {
+ new File(fileName).delete();
+ }
+ return buffer;
+ }
+
+ /**
+ * 灏哹yte[]鍐欏叆鏂囦欢
+ *
+ * @param buffer
+ * @param file
+ * @throws IOException
+ */
+ protected static File writeToFile(byte[] buffer, String fileName,
+ String filePath) throws IOException {
+ File dir = new File(filePath);
+
+ if (!dir.exists()) {
+ dir.mkdirs();
+ }
+ String abPath = filePath.concat(fileName);
+ File file = new File(abPath);
+ if (!file.exists()) {
+ file.createNewFile();
+ }
+ FileOutputStream out = new FileOutputStream(file);
+ out.write(buffer);
+ out.close();
+ return file;
+ }
+
+ /**
+ * 灏哹yte[]鍐欏叆鏂囦欢
+ *
+ * @param buffer
+ * @param file
+ * @throws IOException
+ */
+ protected static File writeToFile(byte[] buffer, File file) throws IOException {
+
+ FileOutputStream out = new FileOutputStream(file);
+ out.write(buffer);
+ out.close();
+ return file;
+ }
+
+ /**
+ * 鎶奤RL涓殑鏁版嵁璇诲彇鍒癰yte[]涓�
+ *
+ * @param url
+ * @return
+ * @throws IOException
+ */
+ protected static byte[] getFileByte(URL url) throws IOException {
+ if (url != null) {
+ return getFileByte(url.openStream());
+ } else {
+ return null;
+ }
+ }
+
+ /**
+ * 浠嶪S涓幏鍙朾yte[]
+ *
+ * @param in
+ * @return
+ */
+ protected static byte[] getFileByte(InputStream in) {
+ ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
+ try {
+ copy(in, out);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ return out.toByteArray();
+ }
+
+ protected static void copy(InputStream in, OutputStream out)
+ throws IOException {
+ try {
+ byte[] buffer = new byte[4096];
+ int nrOfBytes = -1;
+ while ((nrOfBytes = in.read(buffer)) != -1) {
+ out.write(buffer, 0, nrOfBytes);
+ }
+ out.flush();
+ } catch (IOException e) {
+ } finally {
+ try {
+ if (in != null) {
+ in.close();
+ }
+ } catch (IOException ex) {
+ }
+ try {
+ if (out != null) {
+ out.close();
+ }
+ } catch (IOException ex) {
+ }
+ }
+ }
+ /**
+ * 鎴彇璺緞鏂囦欢鍚嶏紝鍖呮嫭鎵╁睍鍚�
+ * @param fileName
+ * @return
+ */
+ public static String getFileNameSuff(String fileName) {
+ return fileName.substring(fileName.lastIndexOf("/")+1);
+ }
+ /**
+ * 浠ヨ矾寰勬渶鍚庢枩鏉犱綅缃姞1寮�濮嬪彇,鍙栧埌鎵╁睍鍚嶃�備笉鍖呮嫭鎵╁睍鍚�
+ * @param fileName
+ * @return
+ */
+ public static String getFileName(String fileName) {
+ int pos = fileName.lastIndexOf(".");
+ if(pos==-1){
+ return fileName;
+ }
+ return fileName.substring(fileName.lastIndexOf("/")+1,pos);
+
+ }
+ /**
+ * 澶嶅埗鏂囦欢
+ *
+ * @param f1
+ * 褰撳墠鏂囦欢娴�
+ * @param f2
+ * 鐩爣鏂囦欢娴�
+ * @return
+ * @throws Exception
+ */
+ public static long copyfile(File f1, File f2) throws Exception {
+ mkdirs(f2.getParent());
+ if (f2.exists() && f2.isFile()) {
+ f2.delete();
+ }
+ System.out.println("娣诲姞锛�"+f2.getAbsolutePath());
+ long time = new Date().getTime();
+ int length = 2097152;
+ FileInputStream in = new FileInputStream(f1);
+ FileOutputStream out = new FileOutputStream(f2);
+ FileChannel inC = in.getChannel();
+ FileChannel outC = out.getChannel();
+ ByteBuffer b = null;
+ while (true) {
+ if (inC.position() == inC.size()) {
+ inC.close();
+ outC.close();
+ return new Date().getTime() - time;
+ }
+ if ((inC.size() - inC.position()) < length) {
+ length = (int) (inC.size() - inC.position());
+ } else
+ length = 2097152;
+ b = ByteBuffer.allocateDirect(length);
+ inC.read(b);
+ b.flip();
+ outC.write(b);
+ outC.force(false);
+ }
+
+ }
+ public static long copyfile(String filePath1, String filePath2) throws Exception {
+ File f1=new File(filePath1);
+ File f2=new File(filePath2);
+ return copyfile(f1,f2);
+
+ }
+
+
+ /**
+ * 鍒涘缓鏂囦欢澶瑰強鐖惰妭鐐�
+ * @param file
+ */
+ public static void mkdirs(File file){
+ if(file.isFile()){
+ file=new File(file.getParent());
+ }
+ if(!file.exists()){
+ file.mkdirs();
+ }
+ }
+ /**
+ * 鍒涘缓鏂囦欢澶瑰強鐖惰妭鐐�
+ * @param file
+ */
+ public static void mkdirs(String filePath){
+ File file = new File(filePath);
+ mkdirs(file);
+ }
+ /**
+ * 鍏堝垱寤虹埗鑺傜偣锛屽啀鍒涘缓鏂囦欢
+ * @param file
+ * @throws IOException
+ */
+ public static void createrFile(File file) throws IOException{
+ mkdirs(file.getParent()); // 鍒ゆ柇鐖惰妭鐐规槸鍚﹀瓨鍦�
+ if(!file.exists()){
+ file.createNewFile();
+ }
+ }
+
+ /**
+ * 鎶婂瓧鑺傚啓鍏ユ枃浠朵腑
+ * @param buffer
+ * @param file
+ * @return
+ * @throws IOException
+ */
+ public static File writeNewFile(byte[] buffer, File file) throws IOException{
+ return writeToFile(buffer, file);
+ }
+
+ /**
+ * 鍒犻櫎鏂囦欢
+ * @param file File 瀵硅薄
+ */
+ public static void deleteFile(File file) {
+ System.out.println("鍒犻櫎鏂囦欢锛�"+file.getAbsolutePath());
+ file.delete();
+ }
+ /**
+ * 鍒犻櫎鏂囦欢
+ * @param filePath
+ */
+ public static void deleteFile(String filePath) {
+ deleteFile(new File(filePath));
+ }
+ /**
+ * 鍒涘缓鐖惰妭鐐圭洰褰曪紝
+ * 鍒ゆ柇鍒犻櫎鏂囦欢2骞跺鍒舵枃浠�1鍒版枃浠�2
+ * @param f1 褰撳墠鏂囦欢娴�
+ * @param f2 鐩爣鏂囦欢娴�
+ * @return
+ * @throws Exception
+ */
+ public static void deleteAndCopyFile(File file1, File file2) throws Exception {
+ mkdirs(file2.getParent());
+ if (file2.exists() && file2.isFile()) {
+ file2.delete();
+ }
+ if(file1.exists() && file1.isFile()){
+ copyfile(file1, file2);
+ file1.delete();
+ }
+ }
+
+ /**
+ * 鍒涘缓鐖惰妭鐐圭洰褰曪紝
+ * 鍒ゆ柇鍒犻櫎鏂囦欢2骞跺鍒舵枃浠�1鍒版枃浠�2
+ *
+ * @param f1
+ * 褰撳墠鏂囦欢娴�
+ * @param f2
+ * 鐩爣鏂囦欢娴�
+ * @return
+ * @throws Exception
+ */
+ public static void deleteAndCopyFile(String file1Path, String file2Path) throws Exception {
+ File file1 = new File(file1Path);
+ File file2 = new File(file2Path);
+ deleteAndCopyFile(file1, file2);
+ }
+ /**
+ * 鑾峰緱鏂囦欢鐨勬墿灞曞悕 鍚� 鏍囩偣绗﹀彿
+ * @param fileName
+ * @return
+ */
+ public static String getExtention(String fileName) {
+ int pos = fileName.lastIndexOf(".");
+ if(pos+1<fileName.length()){
+ return fileName.substring(pos);
+ }
+ return "";
+ }
+ /**
+ * 鑾峰緱鏂囦欢鐨勬墿灞曞悕 涓嶅惈鏍囩偣绗﹀彿
+ * @param fileName
+ * @return
+ */
+ public static String getFileType(String fileName) {
+ int pos = fileName.lastIndexOf(".");
+ if(pos+1<fileName.length()){
+ return fileName.substring(pos+1);
+ }
+ return "";
+ }
+ /**
+ * 鏍规嵁璺緞鑾峰緱鏂囦欢鍐呭
+ * @param fileName
+ * @return
+ */
+ public static String getFileContent(String filePath) {
+ File file = new File(filePath);
+ return getFileContent(file);
+ }
+ /**
+ * 鏍规嵁File瀵硅薄鑾峰緱鏂囦欢鍐呭
+ * @param fileName
+ * @return
+ */
+ public static String getFileContent(File file) {
+ String htmlCode = "";
+ try {
+ String encoding = "UTF-8";
+ if (file.isFile() && file.exists()) { // 鍒ゆ柇鏂囦欢鏄惁瀛樺湪
+
+ BufferedReader bufferedReader = new BufferedReader(new UnicodeReader(new FileInputStream(file), encoding));
+ String lineTxt = null;
+ while ((lineTxt = bufferedReader.readLine()) != null) {
+ htmlCode += lineTxt;
+ }
+ bufferedReader.close();
+ } else {
+ System.out.println("鎵句笉鍒版寚瀹氱殑鏂囦欢");
+ }
+ } catch (Exception e) {
+ System.out.println("璇诲彇鏂囦欢鍐呭鍑洪敊");
+ e.printStackTrace();
+ }
+ return htmlCode;
+ }
+ public static void upload(String filePath, String saveAsFileName,File upload) throws Exception {
+ if (upload != null) {
+
+ if (!filePath.equals("")) {
+ File file = new File(filePath);
+ if (!file.exists()) {
+ file.mkdirs();
+ }
+ }
+ filePath =filePath.concat(saveAsFileName);
+ File imageFile = new File(filePath);
+ deleteAndCopyFile(upload, imageFile);
+ }
+ }
+
+
+
+
+ /**
+ * 鏂囦欢瀛楄妭澶у皬杞崲鏂囧瓧鎻忚堪
+ * @param fileName
+ * @return
+ */
+ public static String convertfilesize(long filesize)
+ {
+ String strunit="bytes";
+ String straftercomma="";
+ int intdivisor=1;
+ if(filesize>=1024*1024)
+ {
+ strunit = "mb";
+ intdivisor=1024*1024;
+ }
+ else if(filesize>=1024)
+ {
+ strunit = "kb";
+ intdivisor=1024;
+ }
+ if(intdivisor==1)
+ return filesize + " " + strunit;
+ straftercomma = "" + 100 * (filesize % intdivisor) / intdivisor ;
+ if(straftercomma=="")
+ straftercomma=".0";
+ return filesize / intdivisor + "." + straftercomma + " " + strunit;
+ }
+
+}
--
Gitblit v1.8.0