package com.saicfc.pmpf.internal.manage.utils;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import com.saicfc.pmpf.common.enums.ChannelCodeEnums;
/**
* 生成TXT文件
* @author lizhiyong
* @version $Id: TxtUtils.java, v 0.1
2014年9月11日 上午8:40:02 Exp $
*/
public class TxtUtils {
@SuppressWarnings("rawtypes")
public static File writeTxtFile(String channelCode, List contentList, LinkedHashMap map,
String filePath, String readStr, String fileName)
throws IOException {
File file = new File(filePath);
if (!file.exists()) {
file.mkdir();
System.out.println("文件夹已创建");
}
//定义文件名格式并创建
File txtFile = File.createTempFile(fileName, ".txt", new File(filePath));
//先读取原有文件内容,然后进行写入操作
FileWriter writer = null;
if (CommonUtils.isNotEmpty(map)) {
//写入头信息
String filein = "";
for (Iterator tileIterator = map.entrySet().iterator(); tileIterator.hasNext();) {
java.util.Map.Entry propertyEntry = (java.util.Map.Entry) tileIterator.next();
filein += (String) propertyEntry.getValue() == null ? "" : (String) propertyEntry
.getValue();
if (tileIterator.hasNext()) {
filein = filein + " ";
}
}
try {
writer = new FileWriter(txtFile, true);
writer.write(filein);
} catch (IOException e1) {
e1.printStackTrace();
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e2) {
e2.printStackTrace();
}
}
}
}
//写入详细信息
int num = 1;
for (Iterator iterator = contentList.iterator(); iterator.hasNext();) {
LinkedHashMap txtMap = (LinkedHashMap) iterator.next();
String filein1 = "";
for (Iterator propertyIterator = txtMap.entrySet().iterator(); propertyIterator
.hasNext();) {
java.util.Map.Entry propertyEntry = (java.util.Map.Entry) propertyIterator.next();
filein1 += (String) propertyEntry.getValue() == null ? "" : (String) propertyEntry
.getValue();
if (propertyIterator.hasNext()) {
filein1 = filein1 + readStr;
}
}
try {
System.out.println("数据filein1:" + filein1);
//给建行生成序号
if (ChannelCodeEnums.CCB.getChannelCode().equals(channelCode)) {
filein1 = String.valueOf(num) + filein1;
}
writer = new FileWriter(txtFile, true);
writer.write(filein1);
//中国银行每行以换行结束
if (ChannelCodeEnums.BOC.getChannelCode().equals(channelCode)) {
writer.write("\r\n");
} else if (ChannelCodeEnums.CIB.getChannelCode().equals(channelCode)) {
writer.write("\r\n");
} else {
if (num < contentList.size()) {
writer.write("\r\n");
}
num++;
}
} catch (IOException e1) {
e1.printStackTrace();
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e2) {
e2.printStackTrace();
}
}
}
}
return txtFile;
}
}
BigDecimal amountDecimal = new BigDecimal(0);
for (Iterator iterator = exportData.iterator(); iterator.hasNext();) {
LinkedHashMap cebMap = (LinkedHashMap) iterator.next();
String amount = (String) cebMap.get("amount");
BigDecimal amountDecimal2 = BigDecimal.valueOf(Double.parseDouble(amount))
.setScale(2, BigDecimal.ROUND_HALF_UP);
amountDecimal = amountDecimal.add(amountDecimal2);
}
int cebdataSize = exportData.size();
LinkedHashMap amountHashMap = new LinkedHashMap();
amountHashMap.put("amount", amountDecimal.toPlainString());
exportData.add(0, amountHashMap);
LinkedHashMap sizeHashMap = new LinkedHashMap();
sizeHashMap.put("totalCount", String.valueOf(cebdataSize));
exportData.add(1, sizeHashMap);
String readStr = ",";
String fileName = "光大银行(CEB)退款数据";
//创建文件
File file = TxtUtils.writeTxtFile(channelCode, exportData, map, filePath, readStr,
fileName);
//下载文件
downLoadFile(response, filePath, file);
/**
* 下载文件
* @param response
* @param filePath 文件路径
* @param file 文件
* @throws IOException
*/
public void downLoadFile(HttpServletResponse response, String filePath, File file)
throws IOException {
String fileName = file.getName();
//下载文件
FileManageUtils.exportFile(response, filePath + fileName, fileName);
//删除单个文件
FileManageUtils.deleteFile(filePath, fileName);
}
package com.saicfc.pmpf.internal.manage.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.http.HttpServletResponse;
/**
* 文件管理
* @author lizhiyong
* @version $Id: FileManageUtils.java, v 0.1
2014年9月11日 上午9:37:47 Exp $
*/
public class FileManageUtils {
/**
* 下载文件
* @param response
* @param csvFilePath
* 文件路径
* @param fileName
* 文件名称
* @throws IOException
*/
public static void exportFile(HttpServletResponse response, String csvFilePath, String fileName)
throws IOException {
response.setContentType("application/csv;charset=GBK");
response.setHeader("Content-Disposition",
"attachment; filename=" + new String(fileName.getBytes("GBK"), "ISO8859-1"));
//URLEncoder.encode(fileName, "GBK")
InputStream in = null;
try {
in = new FileInputStream(csvFilePath);
int len = 0;
byte[] buffer = new byte[1024];
response.setCharacterEncoding("GBK");
OutputStream out = response.getOutputStream();
while ((len = in.read(buffer)) > 0) {
//out.write(new byte[] { (byte) 0xEF, (byte) 0xBB, (byte) 0xBF });
out.write(buffer, 0, len);
}
} catch (FileNotFoundException e) {
System.out.println(e);
} finally {
if (in != null) {
try {
in.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}
/**
* 删除该目录filePath下的所有文件
* @param filePath
* 文件目录路径
*/
public static void deleteFiles(String filePath) {
File file = new File(filePath);
if (file.exists()) {
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isFile()) {
files[i].delete();
}
}
}
}
/**
* 删除单个文件
* @param filePath
* 文件目录路径
* @param fileName
* 文件名称
*/
public static void deleteFile(String filePath, String fileName) {
File file = new File(filePath);
if (file.exists()) {
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isFile()) {
if (files[i].getName().equals(fileName)) {
files[i].delete();
return;
}
}
}
}
}
}