liurunyu
2025-01-20 7f66dd2dee66a81df6ab999fc9daea3ac60a3642
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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 ;
    }
 
}