Generate and download Txt files in Java
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;/*** generate a TXT file * @ author lizhiyong * @ version $ Id: TxtUtils. java, v 0.12014-September 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 ("folder created");} // defines the File name format and creates File txtFile = File. createTempFile (fileName ,". txt ", new File (filePath); // read the original File content first, and then perform the write operation FileWriter writer = null; if (CommonUtils. isNotEmpty (map) {// write header information 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 () ;}}// write details 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 ("Data filein1:" + filein1); // generate the serial number if (ChannelCodeEnums. CCB. getChannelCode (). equals (channelCode) {filein1 = String. valueOf (num) + filein1;} writer = new FileWriter (txtFile, true); writer. write (filein1); // each line of the Bank of China ends with a line break if (ChannelCodeEnums. BOC. getChannelCode (). equal S (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 amountdecimal = BigDecimal. valueOf (Double. parseDouble (amount )). setScale (2, BigDecimal. ROUND_HALF_UP); amountDecimal = amountDecimal. add (amountdecimal);} 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 = "Everbright Bank (CEB) Refund data"; // create a File file File = TxtUtils. writeTxtFile (channelCode, exportData, map, filePath, readStr, fileName); // download the downLoadFile (response, filePath, file );
/*** Download file ** @ param response * @ param filePath File path * @ param file * @ throws IOException */public void downLoadFile (HttpServletResponse response, String filePath, File file) throws IOException {String fileName = file. getName (); // download the file FileManageUtils. exportFile (response, filePath + fileName, fileName); // delete a single file 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;/*** file management ** @ author lizhiyong * @ version $ Id: FileManageUtils. java, v 0.12014-September 11 9:37:47 Exp $ */public class FileManageUtils {/*** Download file * @ param response * @ param csvFilePath * file path * @ param fileName * file name * @ 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"); // UR LEncoder. 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 );}}}} /*** delete all files under the filePath directory * @ param filePath * File directory path */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 () ;}}}/*** delete a single file * @ param filePath * file directory path * @ param fileName * file name */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 ;}}}}}}