zhuguifei
2026-03-10 58402bd5e762361363a0f7d7907153c77dbb819f
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package com.shlanbao.tzsc.utils.tools;
 
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URLEncoder;
 
import javax.servlet.http.HttpServletResponse;
 
import org.apache.log4j.Logger;
/**
 * 文件操作类
 * <li>@author Leejean
 * <li>@create 2014-6-24 下午05:06:23
 */
public class FileOptionsUtil {
    private static Logger log=Logger.getLogger(FileOptionsUtil.class);
    /**
     * 获取文件的扩展名获得     ->.jpg
     * @param fileName
     * @return
     */
    public static String getFileExt(String fileName) {
        return fileName.substring(fileName.lastIndexOf("."));
    }
 
    public static String getFileName(String fileName) {
        int pos = fileName.lastIndexOf('.');
        if (pos == -1) {
            return "";
        } else {
            return fileName.substring(0,pos);
        }
    }
    
    /**
     * @param url       写入路径
     * @param content   写入内容
     * @param encoder   写入编码
     * @param append    false:覆盖原有文件。ture:在原有文件后追加内容。 
     */
    public static boolean writeFile(String url, String content, String encoder, boolean append){
        File file = new File(url);
        String parent = file.getParent();
        File dir = new File(parent);
        if (!dir.exists()) dir.mkdirs();    //如果目录和文件不存在,创建目录和文件
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(url, append);
            fos.write(content.getBytes(encoder));
            fos.close();
            return true;
        } catch (Exception e) {
            log.error("写入文件失败", e);
            return false;
        }
    }
    /**
     * 保存
     * 
     * @return
     * @throws Exception
     */
    public static boolean upload(File upload, String fileName,
            String parentMenu) {
        
            if (upload == null) {
                return false;
            }
            // 3、保存上传的文件到upload目录下
            FileInputStream fis = null;
            FileOutputStream fos = null;
            try {
                File fileRootDir = new File(parentMenu);
                if (!fileRootDir.exists())
                    fileRootDir.mkdirs();
                // 2、目录
                fis = new FileInputStream(upload);
                fos = new FileOutputStream(new File(fileRootDir, fileName));
                // 批量,每次传输2K
                byte[] b = new byte[2 * 1024];
                int len = fis.read(b);
                while (len > 0) {
                    fos.write(b, 0, len);
                    len = fis.read(b);
                }
            } catch (Exception e) {
                e.printStackTrace();
                log.error("上传失败", e);
            } finally {
                try {
                    // 4、关闭流
                    if (fos != null) {
                        fos.flush();
                        fos.close();
                    }
                    if (fis != null) {
                        fis.close();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    log.error("关闭流失败", e);
                }
            }
            return true;
    }
 
    /**
     * 文件下载
     * 
     * @param filePath
     *            下载文件的完整路径
     * @param response
     *            响应对象
     * @throws IOException
     */
    @SuppressWarnings("unused")
    public static boolean download(String filePath,
            HttpServletResponse response) {
        
            // 将服务器上的文件写入本地文件
            OutputStream outputStream = null;
            // 读取服务器上的文件信息
            InputStream inputStream = null;
            // 文件名,输出到用户的下载对话框
            String fileName = null;
            try {
            if (filePath.lastIndexOf("/") != -1) {
                // 从文件完整路径中提取文件名,转码是防止中文不能正确显示
                fileName = new String(filePath.substring(
                        filePath.lastIndexOf("/") + 1).getBytes("utf-8"),
                        "ISO-8859-1");
            } else if (filePath.lastIndexOf("\\") != -1) {
                fileName = new String(filePath.substring(
                        filePath.lastIndexOf("\\") + 1).getBytes("utf-8"),
                        "ISO-8859-1");
            }
                inputStream = new FileInputStream(filePath);
                response.setContentType("text/plain");
                response.setHeader("Location", fileName);
                response.setHeader("Content-Disposition",
                        "attachment;filename=" + fileName);
                outputStream = response.getOutputStream();
                byte[] buffer = new byte[1024];
                int i = -1;
                while ((i = inputStream.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, i);
                }
            
            } catch (Exception e) {
                log.error("下载失败", e);
                return false;
            } finally {
                try {
                    if (outputStream != null) {
                        outputStream.flush();
                        outputStream.close();
                    }
                    if (inputStream != null) {
                        inputStream.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    log.error("关闭流失败", e);
                }
            }
            return true;
    }
    
    
    /**
     * 文件下载
     * @param filePath 文件路径
     * @param fileName 文件名称
     * @param response 响应对象
     * @return
     */
    @SuppressWarnings("unused")
    public static boolean download2(String filePath,String fileName,
            HttpServletResponse response) {
        
            // 将服务器上的文件写入本地文件
            OutputStream outputStream = null;
            // 读取服务器上的文件信息
            InputStream inputStream = null;
            // 文件名,输出到用户的下载对话框
            //String fileName = null;
            try {
                inputStream = new FileInputStream(filePath);
                response.setContentType("text/plain");
                response.setHeader("Location", fileName);
                response.setHeader("Content-Disposition",
                        "attachment;filename=" + URLEncoder.encode(fileName,"utf-8"));
                outputStream = response.getOutputStream();
                byte[] buffer = new byte[1024];
                int i = -1;
                while ((i = inputStream.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, i);
                }
            
            } catch (Exception e) {
                log.error("下载失败", e);
                return false;
            } finally {
                try {
                    if (outputStream != null) {
                        outputStream.flush();
                        outputStream.close();
                    }
                    if (inputStream != null) {
                        inputStream.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    log.error("关闭流失败", e);
                }
            }
            return true;
    }
    /**
     * 读取普通文本文档中的内容
     * 
     * @param sPath
     *            文件路径
     * @return 字符串 .txt .java .xml
     */
    public static String readTXT(String sPath) {
        StringBuffer sb = new StringBuffer();
        try {
            String encoding = "utf-8";
            File file = new File(sPath);
            if (file.isFile() && file.exists()) { // 判断文件是否存在
                InputStreamReader read = new InputStreamReader(
                        new FileInputStream(file), encoding);// 考虑到编码格式
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = null;
                while ((lineTxt = bufferedReader.readLine()) != null) {
                    sb.append(lineTxt + "\n");
                }
                read.close();
            } else {
                System.out.println("找不到指定的文件");
            }
        } catch (Exception e) {
            System.out.println("读取文件内容出错");
            e.printStackTrace();
        }
        return sb != null ? sb.toString() : "";
    }
    public static boolean deleteFileurl(String filePath) {  
        try {
            File file = new File(filePath); 
            if(file.exists()){
                file.delete();
            }        
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        } 
        return true;
    } 
}