package com.hundsun.emall.common.util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
*
* @author Administrator
* 目录结构变更
* recdoc/120/zw/120.doc
* recdoc/120/fj/bbb.doc
* /aaa.gif
*
* senddoc/130/zw/130.doc
* senddoc/130/fj/ttt.doc
* /ddd.gif
*/
public class FileUtil {
private static Log log = LogFactory.getLog(FileUtil.class);
public static final int MAX_FILE_SIZE = 5 * 1024 * 1024;//5M
public static final String FILE_SEPARATOR = "/";
public static final String ZW = "zw";//正文目录
public static final String FJ = "fj";//附件目录
public static final String SENDDOC_DIR = "senddoc";
public static final String RECDOC_DIR = "recdoc";
public static final String TEMPLATE_DIR = "template";
public static final String ZWSEND_DIR = "zw"+FILE_SEPARATOR+"senddoc";
public static final String ZWREC_DIR = "zw"+FILE_SEPARATOR+"recdoc";
public static final String JOURNAL_DIR = "journal";
public static final String cx_two_publicity_DIR = "cx_two_publicity"; //双公示
public static final String cx_credit_publicity_DIR = "cx_credit_publicity"; //信用公示
public static void main(String[] args){
String targetName = "D:/oabsfile/zw/149/149.doc";
String sourceName = "D:/oabsfile/oabs/template/浙交.doc";
copyFile(sourceName,targetName);
createDir("D:/oabsfile/zw/10000000/1000000");
}
//保存文件 10M以内文件
public static boolean SaveFileFromInputStream(InputStream stream,String filename){
try{
FileOutputStream fs=new FileOutputStream( filename);
byte[] buffer =new byte[MAX_FILE_SIZE];
int bytesum = 0;
int byteread = 0;
while ((byteread=stream.read(buffer))!=-1)
{
bytesum+=byteread;
fs.write(buffer,0,byteread);
fs.flush();
}
fs.close();
stream.close();
}catch(Exception e){
log.error("SaveFileFromInputStream error:",e);
return false;
}
return true;
}
/**
* 将文件目录中文件全部删除
* @param filepath
* @return
*/
public static boolean delfiles(String filepath){
File file = new File(filepath);
if(file.exists()){
if(file.isDirectory()){
File[] ff = file.listFiles();
for(File f:ff){
f.deleteOnExit();
}
}
}
return true;
}
/**
* 将目录更改
* D:/oabsfile/senddoc/tmp10017
* D:/oabsfile/senddoc/10017
* @param uploadRootpath
* @param cat
* @param fjdir
* @return senddoc/10017
*/
public static String renameDir(String uploadRootpath,String cat,String fjdir){
String sourcepath = uploadRootpath + FILE_SEPARATOR + cat + FILE_SEPARATOR + fjdir;
String targetpath = uploadRootpath + FILE_SEPARATOR + cat + FILE_SEPARATOR + fjdir.replace("tmp", "");
File f = new File(sourcepath);
if(f.exists()){
if(f.isDirectory()){
File newf = new File(targetpath);
f.renameTo(newf);
}
}
return fjdir.replace("tmp", "");
}
public static boolean renameFile(String uploadRootpath,String cat,String fjdir){
try{
String filepath = uploadRootpath + FILE_SEPARATOR + cat + FILE_SEPARATOR + fjdir;
File file = new File(filepath);
File[] files = file.listFiles();
if(files!=null && files.length>0){
File ff = files[0];
String targetFilename = uploadRootpath + FILE_SEPARATOR + cat + FILE_SEPARATOR + fjdir + FILE_SEPARATOR + fjdir+".doc";
File targetFile = new File(targetFilename);
ff.renameTo(targetFile);
}
}catch(Exception e){
log.error("FileUtil.renameFile error:" ,e );
return false;
}
return true;
}
/**
* 新建目录结构,目录中不能包含字符 . 点号才能新建
* @param filepath
*/
public static void createDir(String filepath){
File file = new File(filepath);
if(!StringUtils.contains(filepath, ".") && !file.exists()){
file.mkdirs();
}
}
/**
* 是否存在该文件
* @param filepath 文件路径
* @return
*/
public static boolean hasFile(String filepath){
File file = new File(filepath);
if(file.exists()){
if(file.isFile()){
return true;
}
}
return false;
}
public static boolean copyFile(String sourcefilename,String targetfilename){
boolean flag = true;
try {
log.info("sourcefilename=="+sourcefilename);
log.info("targetfilename=="+targetfilename);
File sourcefile = new File(sourcefilename);
File targetfile = new File(targetfilename);
if(sourcefile.exists()){
new File(targetfile.getParent()).mkdirs();
copyFile(sourcefile,targetfile);
}
} catch (IOException e) {
flag = false;
log.error("FileUtil.copyFile error:",e);
}
return flag;
}
// 复制文件
public static void copyFile(File sourceFile, File targetFile) throws IOException {
BufferedInputStream inBuff = null;
BufferedOutputStream outBuff = null;
try {
// 新建文件输入流并对它进行缓冲
inBuff = new BufferedInputStream(new FileInputStream(sourceFile));
// 新建文件输出流并对它进行缓冲
outBuff = new BufferedOutputStream(new FileOutputStream(targetFile));
// 缓冲数组
byte[] b = new byte[1024 * 5];
int len;
while ((len = inBuff.read(b)) != -1) {
outBuff.write(b, 0, len);
}
// 刷新此缓冲的输出流
outBuff.flush();
} finally {
// 关闭流
if (inBuff != null)
inBuff.close();
if (outBuff != null)
outBuff.close();
}
}
}