import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import org.apache.commons.lang3.exception.ExceptionUtils;
import com.lowagie.text.pdf.AcroFields;
import com.lowagie.text.pdf.AcroFields.Item;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
public class FileTool {
public static boolean makeDir(String dir) {
File f = new File(dir);
if (!f.exists()) {
return f.mkdirs();
}
return true;
}
public static boolean deleteFile(String fullPath) {
File f = new File(fullPath);
if (f.exists() && f.isFile()) {
return f.delete();
}
return false;
}
public static byte[] toByteArray(InputStream input, int size) throws IOException {
if (size < 0) {
throw new IllegalArgumentException("Size must be equal or greater than zero: " + size);
}
if (size == 0) {
return new byte[0];
}
byte[] data = new byte[size];
int offset = 0;
int readed;
while (offset < size && (readed = input.read(data, offset, size - offset)) != -1) {
offset += readed;
}
if (offset != size) {
throw new IOException("Unexpected readed size. current: " + offset + ", excepted: " + size);
}
return data;
}
public static byte[] toByteArray(File file) throws Exception {
byte[] buffer = null;
FileInputStream fis = null;
ByteArrayOutputStream bos = null;
try {
fis = new FileInputStream(file);
bos = new ByteArrayOutputStream();
byte[] b = new byte[1000];
int n;
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
buffer = bos.toByteArray();
} catch (Exception e) {
throw e;
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
//
}
}
}
return buffer;
}
/**
* @return bRe false 失败 true 成功 1.将报文写为文件
*/
public static Boolean writeFile(byte[] bytes, String sName, String sPath) {
Boolean bRe = false;
FileOutputStream fos = null;
String sFullPath = sPath + File.separator + sName;
try {
File fFile = new File(sPath);
if (!fFile.exists()) {
LogUtil.trace("建立目录[" + fFile + "]");
fFile.mkdirs();
}
LogUtil.trace("开始写文件");
fos = new FileOutputStream(new File(sFullPath));
fos.write(bytes);
bRe = true;
LogUtil.trace("写文件完毕");
} catch (Exception e) {
LogUtil.error("写文件异常[" + sFullPath + "]", e);
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
//
}
}
}
return bRe;
}
public static void deleteFolder(String folder) {
File file = new File(folder);
if (file.isFile()) {
file.delete();
return;
} else {
for (String temp : file.list()) {
deleteFolder(new File(file, temp).toString());
}
}
}
/**
* 根据模板生成PDF文件
* @param templatePath
* @param exportPath
* @param data
* @throws Exception
*/
public static void createPdfByTemplate(String templatePath,
String exportPath,Map<String,Object> data) throws Exception{
PdfReader pdfReader = null;
PdfStamper ps = null;
try {
pdfReader = new PdfReader(templatePath);
ps = new PdfStamper(pdfReader,
new FileOutputStream(exportPath));// 生成的输出流
AcroFields as = ps.getAcroFields();
Map<String, Item> fieldMap = as.getFields(); // pdf表单相关信息展示
for (Map.Entry<String, Item> entry : fieldMap.entrySet()) {
String name = entry.getKey(); // name就是pdf模版中各个文本域的名字
//map中的key设置为跟pdf模板key一样
as.setField(name, data.get(name) == null?"":String.valueOf(data.get(name)));
}
ps.setFormFlattening(true);
} catch (Exception e) {
LogUtil.error("createPdfByTemplate failed: " + ExceptionUtils.getStackTrace(e));
throw e;
} finally{
if(ps != null){
ps.close();
}
if(pdfReader != null){
pdfReader.close();
}
}
}
/**
* 创建制定目录文件夹
* 有删除再创建
* @param path
*/
public static void createFile(File file) {
if (!file.exists() && !file.isDirectory()){
file.mkdirs();
} else {
deleteAllFilesOfDir(file);
file.mkdirs();
}
}
/**
* 删除文件夹
* @param path
*/
public static void deleteAllFilesOfDir(File path) {
if (!path.exists())
return;
if (path.isFile()) {
path.delete();
return;
}
File[] files = path.listFiles();//获取所有文件
for (int i = 0; i < files.length; i++) {
deleteAllFilesOfDir(files[i]);//删除文件
}
path.delete();
}
}