123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026 |
- package com.behosoft.util;
- 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.io.OutputStreamWriter;
- import java.io.PrintStream;
- import java.io.PrintWriter;
- import java.io.RandomAccessFile;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.List;
- import javax.servlet.ServletOutputStream;
- import javax.servlet.http.HttpServletResponse;
- import com.behosoft.framework.web.exception.CommonException;
- public class FileExposeUtil {
- /**
- * 获取classpath的根目录
- *
- * @return
- */
- public static String getRootClasspath() {
- String path = FileExposeUtil.class.getResource("/").getPath();
- // if (AppContextUtils.getServletContext() != null
- // && (AppContextUtils.getServletContext().getServerInfo()
- // .startsWith(GlobalConstants.SERVER_KEY_WEBSPHERE) || AppContextUtils.getServletContext()
- // .getServerInfo().startsWith("Apusic Application Server"))) {
- //
- // }
- // 兼容 Apusic Application Server
- if (path.indexOf("/WEB-INF/classes/") == -1) {
- String tmpPath = FileExposeUtil.class.getResource("").getPath().replaceAll("%20", " ");
- path = tmpPath.substring(0, tmpPath.indexOf("/WEB-INF/classes/") + "/WEB-INF/classes/".length());
- }
- path = path.replaceAll("%20", " ");
- return path;
- }
- /**
- * 获取根据目录路径 绝对路径
- *
- * @return
- */
- public static String getRootPath() {
- String path = getRootClasspath();
- if (path.indexOf("WEB-INF") != -1) {
- path = path.substring(0, path.indexOf("WEB-INF"));
- }
- // comment below because of unix path
- // path = path.substring(1, path.length());
- return path;
- }
- /**
- * <p>
- * Description:checkFileExist
- * </p>
- *
- * @param filePath filePath
- * @return
- */
- public static boolean checkFileExist(String filePath) {
- File file = new File(filePath);
- return file.exists();
- }
- /**
- * 获取文件大小
- *
- * @return
- */
- public static long getFileSize(String filePath) {
- long ret = 0;
- File file = new File(filePath);
- if (file.exists()) {
- ret = file.length();
- }
- return ret;
- }
- /**
- * <p>
- * Description:[方法功能中文描述]
- * </p>
- *
- * @param buffer
- * @param filePath
- */
- public static void readToBuffer(StringBuffer buffer, String filePath) {
- // 先根据输入/输出文件生成相应的输入/输出流
- InputStream is = null;
- try {
- is = new FileInputStream(filePath);
- readToBuffer(buffer, is);
- }
- catch (IOException e) {
- throw new CommonException("common.read.file.error");
- }
- finally {
- try {
- if (is != null) {
- is.close();
- }
- }
- catch (IOException e) {
- throw new CommonException("common.read.file.error");
- }
- }
- }
- /**
- * <p>
- * Description:将流中的文本读入一个 StringBuffer 中
- * </p>
- *
- * @param buffer buffer
- * @param is InputStream
- */
- public static void readToBuffer(StringBuffer buffer, InputStream is) {
- // 用来保存每行读取的内容
- String line;
- try {
- BufferedReader reader = new BufferedReader(new InputStreamReader(is));
- // 读取第一行
- line = reader.readLine();
- // 如果 line 为空说明读完了
- while (line != null) {
- // 将读到的内容添加到 buffer 中
- buffer.append(line);
- // 添加换行符
- buffer.append("\n");
- // 读取下一行
- line = reader.readLine();
- }
- }
- catch (IOException e) {
- throw new CommonException("common.read.file.error");
- }
- }
- /**
- * <p>
- * Description:将 StringBuffer 中的内容读出到流中
- * </p>
- *
- * @param buffer StringBuffer
- * @param os OutputStream
- */
- public static void writeFromBuffer(StringBuffer buffer, OutputStream os) {
- PrintStream ps = new PrintStream(os);
- ps.print(buffer.toString());
- }
- /**
- * 22. 将 StringBuffer 中的内容读出到流中
- */
- public static void writeFromBuffer(StringBuffer buffer, String outPath) {
- OutputStream out = null;
- try {
- out = new FileOutputStream(outPath);
- writeFromBuffer(buffer, out);
- }
- catch (IOException e) {
- throw new CommonException("common.write.file.error");
- }
- }
- /**
- * 3*. 从输出流中拷贝内容到输入流中
- */
- private static void copyStream(InputStream is, OutputStream os) {
- byte[] bytes = new byte[1024];
- int i = -1;
- try {
- while ((i = is.read(bytes)) != -1) {
- os.write(bytes, 0, i);
- }
- os.flush();
- }
- catch (IOException e) {
- throw new CommonException("common.copy.file.error");
- }
- }
- /**
- * 4. 调用 (orgFilePath, outFilePath) 方法拷贝文本文件
- *
- * @param orgFilePath 原文件路径,包含文件名
- * @param outFilePath 拷贝文件路径,包含文件名
- */
- public static void copyFile(String orgFilePath, String outFilePath) {
- // 先根据输入/输出文件生成相应的输入/输出流
- InputStream is = null;
- OutputStream os = null;
- try {
- createFile(outFilePath);
- is = new FileInputStream(orgFilePath);
- os = new FileOutputStream(outFilePath);
- copyStream(is, os); // 用 copyStream 拷贝内容
- os.flush();
- }
- catch (Exception e) {
- throw new CommonException("common.copy.file.error");
- }
- finally {
- try {
- if (is != null)
- is.close();
- }
- catch (Exception e) {
- throw new CommonException("common.copy.file.error");
- }
- try {
- if (os != null)
- os.close();
- }
- catch (Exception e) {
- throw new CommonException("common.copy.file.error");
- }
- }
- }
- /**
- * 4. 调用 (orgFilePath, outFilePath) 方法拷贝文本文件
- *
- * @param orgFilePath 原文件路径,包含文件名
- * @param outFilePath 拷贝文件路径,包含文件名
- */
- public static void copyFile(File file, String outFilePath) {
- // 先根据输入/输出文件生成相应的输入/输出流
- InputStream is = null;
- OutputStream os = null;
- try {
- createFile(outFilePath);
- is = new FileInputStream(file);
- os = new FileOutputStream(outFilePath);
- copyStream(is, os); // 用 copyStream 拷贝内容
- os.flush();
- }
- catch (Exception e) {
- throw new CommonException("common.copy.file.error");
- }
- finally {
- try {
- if (is != null)
- is.close();
- }
- catch (Exception e) {
- throw new CommonException("common.copy.file.error");
- }
- try {
- if (os != null)
- os.close();
- }
- catch (Exception e) {
- throw new CommonException("common.copy.file.error");
- }
- }
- }
- /**
- * 5. 调用 (orgFilePath, outFilePath) 方法拷贝文本文件
- *
- * @param orgFilePath 原文件路径,包含文件名
- * @param outFilePath 拷贝文件路径,包含文件名
- */
- public static void copyFile(String orgFilePath, String outFileRootPath, String aimFileName) {
- // 先根据输入/输出文件生成相应的输入/输出流
- createFile(outFileRootPath, aimFileName);
- copyFile(orgFilePath, outFileRootPath + aimFileName);
- }
- /**
- * 创建文件或者文件夹
- *
- * @param filePath
- */
- public static void createFile(String filePath) {
- if (filePath != null && !"".equals(filePath) && !checkFileExist(filePath)) {
- File file = null;
- try {
- if (filePath.indexOf(".") != -1) {
- filePath = filePath.replace('\\', '/');
- String temp = filePath.substring(0, filePath.lastIndexOf("/") + 1);
- if (checkFileExist(temp)) {
- file = new File(filePath);
- file.createNewFile();
- }
- else {
- file = new File(temp);
- file.mkdirs();
- file = new File(filePath);
- file.createNewFile();
- }
- }
- else {
- file = new File(filePath);
- file.mkdirs();
- }
- }
- catch (IOException e) {
- throw new CommonException("common.create.file.error");
- }
- }
- }
- /**
- * 7*. 把流保存到本地文件(路径和文件名分开)
- *
- * @param is
- * @param filePath
- * @param fileName
- */
- public static void saveFile(InputStream is, String filePath, String fileName, boolean isBinary) {
- File file = new File(filePath);
- if (!file.exists()) {
- file.mkdirs();
- }
- OutputStream os = null;
- PrintWriter writer = null;
- BufferedReader reader = null;
- try {
- os = new FileOutputStream(filePath + "\\" + fileName);
- if (isBinary) {
- int len;
- byte[] buf = new byte[1024];
- while ((len = is.read(buf, 0, 1024)) != -1) {
- os.write(buf, 0, len);
- }
- os.flush();
- }
- else {
- String line;
- reader = new BufferedReader(new InputStreamReader(is));
- writer = new PrintWriter(new OutputStreamWriter(os));
- line = reader.readLine();
- while (line != null) {
- writer.println(line);
- line = reader.readLine();
- }
- writer.flush();
- }
- }
- catch (IOException e) {
- throw new CommonException("common.save.file.error");
- }
- finally {
- try {
- if (os != null) {
- os.close();
- }
- }
- catch (IOException e) {
- throw new CommonException("common.save.file.error");
- }
- try {
- if (writer != null) {
- writer.close();
- }
- }
- catch (Exception e) {
- throw new CommonException("common.save.file.error");
- }
- try {
- if (reader != null) {
- reader.close();
- }
- }
- catch (Exception e) {
- throw new CommonException("common.save.file.error");
- }
- }
- }
- /**
- * 7*. 把流保存到本地文件(路径包含文件名)
- *
- * @param is
- * @param filePath
- * @param fileName
- * @throws IOException
- */
- public static void saveFile(InputStream is, String filePath) {
- int dirStrLoc = filePath.lastIndexOf("\\");
- if (dirStrLoc < 0) {
- dirStrLoc = filePath.lastIndexOf("/");
- }
- if (dirStrLoc > 1) {
- File file = new File(filePath.substring(0, dirStrLoc));
- if (!file.exists()) {
- file.mkdirs();
- }
- }
- OutputStream os = null;
- PrintWriter writer = null;
- BufferedReader reader = null;
- try {
- os = new FileOutputStream(filePath);
- String line;
- reader = new BufferedReader(new InputStreamReader(is));
- writer = new PrintWriter(new OutputStreamWriter(os));
- line = reader.readLine();
- while (line != null) {
- writer.println(line);
- line = reader.readLine();
- }
- writer.flush();
- }
- catch (Exception e) {
- throw new CommonException("common.save.file.error");
- }
- finally {
- try {
- if (os != null) {
- os.close();
- }
- }
- catch (Exception e) {
- throw new CommonException("common.save.file.error");
- }
- try {
- if (writer != null) {
- writer.close();
- }
- }
- catch (Exception e) {
- throw new CommonException("common.save.file.error");
- }
- try {
- if (reader != null) {
- reader.close();
- }
- }
- catch (Exception e) {
- throw new CommonException("common.save.file.error");
- }
- }
- }
- /**
- * 5. 替文本中特殊字符
- *
- * @param filePath 文件路径,包含文件名
- * @param
- */
- public static void replaceChar(String filePath, String[] oldChar, String[] newChar) {
- // 先根据输入/输出文件生成相应的输入/输出流
- InputStream is = null;
- OutputStream out = null;
- try {
- is = new FileInputStream(filePath);
- StringBuffer buffer = new StringBuffer();
- readToBuffer(buffer, is);
- String textContent = buffer.toString();
- // 替换开始
- if (oldChar != null && newChar != null && (oldChar.length == newChar.length)) {
- for (int i = 0; i < oldChar.length; i++) {
- textContent = textContent.replaceAll(oldChar[i], newChar[i]);
- }
- }
- buffer.delete(0, buffer.length());
- buffer.append(textContent);
- out = new FileOutputStream(filePath);
- writeFromBuffer(buffer, out);
- out.flush();
- }
- catch (Exception e) {
- throw new CommonException("common.write.file.error");
- }
- finally {
- try {
- if (is != null)
- is.close();
- }
- catch (Exception e) {
- throw new CommonException("common.write.file.error");
- }
- try {
- if (out != null)
- out.close();
- }
- catch (Exception e) {
- throw new CommonException("common.write.file.error");
- }
- }
- }
- /**
- * 删除给定文件
- *
- * @param path
- */
- public static void delFile(String path) {
- File file = new File(path);
- if (file.exists()) {
- file.delete();
- }
- }
- /**
- * 删除给定文件
- *
- * @param path
- */
- public static void delFile(File file) {
- if (file.exists()) {
- file.delete();
- }
- }
- /**
- * 功能:删除文件夹方法
- *
- * @param delPath 要删除的文件夹路径
- * @return true delete file success,false delete file faild
- */
- public static boolean deleteDirs(String delPath) throws Exception {
- return deleteDirs(new File(delPath));
- }
- /**
- * <p>
- * Description:删除文件夹方法
- * </p>
- *
- * @param delPath
- * @param l 如果文件生成时间与现在时间的差大于l则删除
- * @return
- * @throws Exception
- */
- public static boolean deleteDirs(String delPath, long l) throws Exception {
- return deleteDirs(new File(delPath), l);
- }
- /**
- * 功能:删除文件夹方法
- *
- * @param dir 要删除的文件夹路径(java.io.File类型)
- * @return true delete file success,false delete file faild
- */
- public static boolean deleteDirs(File dir) {
- boolean delSuccess = false;
- File m_root = dir;
- List<File> m_dirs = new ArrayList<File>();
- if (!m_root.isDirectory()) { // 判断输入的是否为路径
- if (m_root.exists() && m_root.canWrite()) {// 如果文件存在就删除
- m_root.delete();
- delSuccess = true;
- }
- }
- else {
- m_dirs.add(m_root);
- findFile(m_root, m_dirs); // 获取指定路径下的所有文件已经文件夹(递归调用)
- rootDelete(m_dirs); // 删除list中的所有文件(倒叙循环删除)
- delSuccess = true;
- }
- return delSuccess;
- }
- /**
- * <p>
- * Description:[方法功能中文描述]
- * </p>
- *
- * @param dir
- * @param l 如果文件生成时间与现在时间的差大于l则删除
- * @return
- */
- public static boolean deleteDirs(File dir, long l) {
- boolean delSuccess = false;
- File m_root = dir;
- List<File> m_dirs = new ArrayList<File>();
- if (!m_root.isDirectory()) { // 判断输入的是否为路径
- if (m_root.exists() && m_root.canWrite() && (new Date().getTime() - m_root.lastModified()) > l) {// 如果文件存在就删除
- try {
- m_root.delete();
- }
- catch (Exception e) {
- }
- delSuccess = true;
- }
- }
- else {
- m_dirs.add(m_root);
- try {
- findFile(m_root, m_dirs); // 获取指定路径下的所有文件已经文件夹(递归调用)
- rootDelete(m_dirs, l); // 删除list中的所有文件(倒叙循环删除)
- }
- catch (Exception e) {
- throw new CommonException("common.delete.file.error");
- }
- delSuccess = true;
- }
- return delSuccess;
- }
- /**
- * 功能:删除文件或文件夹 注意:使用倒叙删除,先删除文件,然后删除空文件夹
- */
- private static void rootDelete(List<File> m_dirs) {
- if (m_dirs != null && m_dirs.size() > 0) {
- // 使用倒叙循环删除(先删除文件,再删除文件夹)
- for (int i = m_dirs.size() - 1; i >= 0; i--) {
- File f = m_dirs.remove(i);// 获取之后删除list中的数据
- // 删除数据
- if (f.exists() && f.canWrite()) {
- f.delete();
- }
- }
- }
- else {
- throw new CommonException("common.delete.file.error");
- }
- }
- /**
- * <p>
- * Description:[方法功能中文描述]
- * </p>
- *
- * @param m_dirs
- * @param l 如果文件生成时间与现在时间的差大于l则删除
- */
- private static void rootDelete(List<File> m_dirs, long l) {
- if (m_dirs != null && m_dirs.size() > 0) {
- // 使用倒叙循环删除(先删除文件,再删除文件夹)
- for (int i = m_dirs.size() - 1; i >= 0; i--) {
- File f = m_dirs.remove(i);// 获取之后删除list中的数据
- // 删除数据
- if (f.exists() && f.canWrite() && (new Date().getTime() - f.lastModified()) > l) {
- f.delete();
- }
- }
- }
- else {
- throw new CommonException("common.delete.file.error");
- }
- }
- /**
- * 功能:获取所有文件和文件夹,存储在m_dirs中 注意:递归调用
- *
- * @param tempRoot 文件路径
- */
- public static void findFile(File tempRoot, List<File> m_dirs) {
- findFile(tempRoot, m_dirs, null);
- }
- /**
- * 功能:获取给定文件类型的所有文件,存储在m_dirs中 注意:递归调用
- *
- * @param tempRoot 文件路径
- */
- public static void findFile(File tempRoot, List<File> m_dirs, String suffix) {
- // 获取指定路径下的所有文件
- File[] dirs = tempRoot.listFiles();
- if (dirs != null) {
- // 将文件数组转换成List对象
- List<File> dirslist = Arrays.asList(dirs);
- if (suffix == null)
- m_dirs.add(tempRoot);
- // 递归调用
- for (int i = 0; i < dirslist.size(); i++) {
- findFile(dirslist.get(i), m_dirs, suffix);
- }
- }
- else {
- String name = tempRoot.getName();
- if (suffix != null) {
- name = name.substring(name.lastIndexOf(".") + 1);
- if (suffix.equals(name)) {
- m_dirs.add(tempRoot);
- }
- }
- else {
- m_dirs.add(tempRoot);
- }
- }
- }
- /**
- * 功能:检查文件是否存在 注意:递归调用
- *
- * @param tempRoot 文件路径
- */
- public static boolean checkFileExist(File tempRoot, String fileName) {
- boolean ret = false;
- // 获取指定路径下的所有文件
- File[] dirs = tempRoot.listFiles();
- if (dirs != null) {
- // 将文件数组转换成List对象
- List<File> dirslist = Arrays.asList(dirs);
- // 递归调用
- for (int i = 0; i < dirslist.size(); i++) {
- ret = checkFileExist(dirslist.get(i), fileName);
- if (ret) {
- break;
- }
- }
- }
- else {
- if (tempRoot.getName().trim().equals(fileName.trim())) {
- ret = true;
- }
- }
- return ret;
- }
- public static boolean checkFileExist(String dirRoot, String fileName) {
- boolean ret = false;
- try {
- File file = new File(dirRoot);
- ret = checkFileExist(file, fileName);
- }
- catch (Exception e) {
- throw new CommonException("common.check.file.error");
- }
- return ret;
- }
- /**
- * 功能:获取所有文件和文件夹,存储在m_dirs中
- *
- * @param rootPath 文件路径
- */
- public static void findFile(String rootPath, List<File> m_dirs) {
- File file = new File(rootPath);
- findFile(file, m_dirs);
- }
- /**
- * 功能:获取给定扩展名的所有文件,存储在m_dirs中
- *
- * @param rootPath 文件路径
- * @param m_dirs 存放文件
- * @param suffix 扩展名
- */
- public static void findFile(String rootPath, List<File> m_dirs, String suffix) {
- File file = new File(rootPath);
- findFile(file, m_dirs, suffix);
- }
- /**
- * 功能:获取所有文件和文件夹的绝对路径,存储在filePath中
- *
- * @param rootPath 文件路径
- */
- public static void findFilePath(String rootPath, List<String> filePath) throws Exception {
- File file = new File(rootPath);
- List<File> m_dirs = new ArrayList<File>();
- findFile(file, m_dirs);// 获取所有文件
- if (m_dirs != null && m_dirs.size() > 0) {
- int size = m_dirs.size();
- // 遍历所有文件,获取其绝对路径
- for (int i = 0; i < size; i++) {
- File f = m_dirs.get(i);
- filePath.add(f.getPath());
- }
- }
- }
- /**
- * 根据路径获取文件名称
- *
- * @param filePath
- * @return
- */
- public static String getFileName(String filePath) {
- String ret = "";
- filePath = filePath.replace("\\", "/");
- if (filePath.lastIndexOf("/") != -1) {
- ret = filePath.substring(filePath.lastIndexOf("/") + 1);
- }
- else {
- ret = filePath;
- }
- return ret;
- }
- public static void rename(String filepath, String oldName, String newName) {
- try {
- File source = new File(filepath + "\\" + oldName);
- File target = new File(filepath + "\\" + newName);
- source.renameTo(target);
- }
- catch (Exception e) {
- throw new CommonException("common.rename.file.error");
- }
- }
- public static void rename(String oldPath, String newPath) {
- try {
- File source = new File(oldPath);
- File target = new File(newPath);
- source.renameTo(target);
- }
- catch (Exception e) {
- throw new CommonException("common.rename.file.error");
- }
- }
- /**
- * 根据文件名称获取文件后缀
- *
- * @param filePath
- * @return
- */
- public static String getFileSuffix(String fileName) {
- String ret = "";
- if (fileName != null && fileName.indexOf(".") > 0) {
- ret = fileName.substring(fileName.indexOf("."), fileName.length());
- }
- return ret;
- }
- /**
- * 生成文件
- *
- * @param rootPath
- * @param fileName
- * @param fileSize
- * @return
- */
- public static File createFile(String rootPath, String fileName, Long fileSize) {
- File file = new File(rootPath);
- if (!file.exists()) {
- file.mkdirs();
- }
- file = new File(rootPath + "\\" + fileName);
- RandomAccessFile acessFile = null;
- try {
- acessFile = new RandomAccessFile(file, "rw");
- acessFile.setLength(fileSize.longValue());
- }
- catch (Exception e) {
- throw new CommonException("common.create.file.error");
- }
- finally {
- try {
- if (acessFile != null)
- acessFile.close();
- }
- catch (Exception e) {
- throw new CommonException("common.create.file.error");
- }
- }
- return file;
- }
- public static List<String> getFilenames(String filepath) {
- List<String> list = new ArrayList<String>();
- File directory = new File(filepath);
- File[] files = directory.listFiles();
- for (int i = 0; i < files.length; i++) {
- list.add(filepath + files[i].getName());
- }
- return list;
- }
- public static List<File> getFiles(String filepath) {
- List<File> list = new ArrayList<File>();
- File directory = new File(filepath);
- File[] files = directory.listFiles();
- for (int i = 0; i < files.length; i++) {
- list.add(files[i]);
- }
- return list;
- }
- /**
- * 生成文件
- *
- * @param rootPath
- * @param fileName
- * @param fileSize
- * @return
- */
- public static File createFile(String rootPath, String fileName) {
- return createFile(rootPath, fileName, new Long(0));
- }
- public static void downLoadFile(HttpServletResponse response, String downPath, String fileName, String targetfile) {
- ServletOutputStream os = null;
- try {
- File dFile = new File(downPath + fileName);
- if (dFile.exists()) {
- response.setContentType("application/octet-stream");
- response.setHeader("Content-Disposition", "filename=" + targetfile);
- os = response.getOutputStream();
- }
- else {
- throw new CommonException("common.find.no.file", new String[] { downPath + fileName });
- }
- String downLoadFile = downPath + fileName;
- // 下载文件的路径是否存在
- File downLoadPath = new File(downPath);
- if (!downLoadPath.exists()) {
- downLoadPath.mkdirs();
- }
- // 创建文件
- FileInputStream in = new FileInputStream(downLoadFile);
- int byteRead = 0;
- byte[] buffer = new byte[1024];
- while ((byteRead = in.read(buffer, 0, 1024)) != -1) {
- os.write(buffer, 0, byteRead);
- }
- os.flush();
- os.close();
- in.close();
- }
- catch (IOException e) {
- try {
- os.flush();
- os.close();
- }
- catch (IOException e1) {
- }
- throw new CommonException("");
- }
- }
- public static void downLoadFile(HttpServletResponse response, FileInputStream in, String targetfile) {
- ServletOutputStream os = null;
- try {
- // response.setCharacterEncoding("UTF-8");
- response.setContentType("application/x-msexcel");
- response.setHeader("Content-Disposition", "filename=" + targetfile);
- os = response.getOutputStream();
- // 创建文件
- int byteRead = 0;
- byte[] buffer = new byte[8192];
- while ((byteRead = in.read(buffer, 0, 8192)) != -1) {
- os.write(buffer, 0, byteRead);
- }
- os.flush();
- os.close();
- in.close();
- }
- catch (IOException e) {
- try {
- os.flush();
- os.close();
- }
- catch (IOException e1) {
- }
- throw new CommonException("bizarea.AnnualService.downLoadFile");
- }
- }
- public static String getDisFileName(String bakstr) {
- Calendar ca = Calendar.getInstance();
- ca.setTime(new java.util.Date());
- SimpleDateFormat simple = new SimpleDateFormat("yyyyMMddkkmmssSSS");
- String date = simple.format(ca.getTime());
- String randomstr = String.valueOf(Math.random());
- String wantstr = randomstr.substring(randomstr.length() - 6, randomstr.length());
- return date + wantstr + bakstr;
- }
- public static String getFrontStr(String filename) {
- return filename.substring(0, filename.length() - 4);
- }
- public static void saveFile(InputStream is, String filePath, String fileName) {
- saveFile(is, filePath, fileName, false);
- }
- }
|