From b63eef2cfb054b1ead234ab93e05a2aa28775d91 Mon Sep 17 00:00:00 2001 From: zhubaomin <zhubaomin> Date: 星期四, 17 四月 2025 14:54:26 +0800 Subject: [PATCH] 优化灌溉计划发布前判断条件 --- pipIrr-platform/pipIrr-web/pipIrr-web-file/src/main/java/com/dy/pipIrrWebFile/util/FileUtil.java | 122 ++++++++++++++++++++++++++++++++++++++++ 1 files changed, 122 insertions(+), 0 deletions(-) diff --git a/pipIrr-platform/pipIrr-web/pipIrr-web-file/src/main/java/com/dy/pipIrrWebFile/util/FileUtil.java b/pipIrr-platform/pipIrr-web/pipIrr-web-file/src/main/java/com/dy/pipIrrWebFile/util/FileUtil.java new file mode 100644 index 0000000..5b3bd58 --- /dev/null +++ b/pipIrr-platform/pipIrr-web/pipIrr-web-file/src/main/java/com/dy/pipIrrWebFile/util/FileUtil.java @@ -0,0 +1,122 @@ +package com.dy.pipIrrWebFile.util; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.RandomAccessFile; +import java.nio.channels.FileChannel; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + + +public class FileUtil { + + protected static final Logger log = LogManager.getLogger(FileUtil.class.getName()) ; + /** + * 淇濆瓨鏂囦欢 + * @param fileAbsolutelyPath 鏂囦欢缁濆璺緞 + * @param input + * @return 淇濆瓨鏂囦欢鐨勬槸鍚︽垚鍔熺殑缁撴灉 + */ + public boolean saveFile(String fileAbsolutelyPath , InputStream input){ + boolean success = true ; + if(fileAbsolutelyPath == null || input == null){ + success = false ; + }else{ + OutputStream os = null; + try { + os = new FileOutputStream(fileAbsolutelyPath); + byte[] b = new byte[1024] ; + int len = input.read(b) ; + while(len > 0){ + os.write(b, 0, len); + len = input.read(b) ; + os.flush(); + } + } catch (Exception e) { + e.printStackTrace(); + success = false ; + } finally { + try { + if(os != null){ + os.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + } + return success ; + } + /** + * 澶嶅埗鏂囦欢 + * @param fromFile + * @param realPath 缁濆璺緞 + * @param newFileRelativityPath 鐩稿璺緞 + * @return + */ + public boolean copyFile(File fromFile, String realPath, String newFileRelativityPath){ + boolean flag = true ; + FileChannel fromChannel = null ; + FileChannel toChannel = null ; + try { + fromChannel = new RandomAccessFile(fromFile, "r").getChannel(); + toChannel = new RandomAccessFile(new File(realPath + newFileRelativityPath), "rw").getChannel(); + fromChannel.transferTo(0, fromChannel.size(), toChannel); + } catch (FileNotFoundException e) { + flag = false ; + log.error("澶嶅埗鏂囦欢鍑洪敊:" + e.getMessage()) ; + } catch (IOException e) { + flag = false ; + log.error("澶嶅埗鏂囦欢鍑洪敊:" + e.getMessage()) ; + }finally{ + try{ + if(fromChannel != null){ + fromChannel.close() ; + } + if(toChannel != null){ + toChannel.close() ; + } + }catch(Exception e){}finally{} + } + return flag ; + } + + /** + * 鍒犻櫎鏂囦欢 + * @param f + */ + public boolean deleteFile(File f){ + boolean flag = false ; + try{ + if(f != null){ + flag = f.delete() ; + } + }catch(Exception e){ + }finally{} + return flag ; + } + + /** + * 鍒犻櫎鏂囦欢 + * @param path + */ + public boolean deleteFile(String path){ + boolean flag = false ; + try{ + if(path != null && !path.equals("")){ + File f = new File(path) ; + if(f != null && f.exists()){ + flag = f.delete() ; + } + } + }catch(Exception e){ + }finally{} + return flag ; + } + +} -- Gitblit v1.8.0