1 22 package org.objectweb.petals.binding.filetransferbc; 23 24 import java.io.BufferedWriter ; 25 import java.io.File ; 26 import java.io.FileOutputStream ; 27 import java.io.FileWriter ; 28 import java.io.IOException ; 29 import java.text.SimpleDateFormat ; 30 import java.util.Date ; 31 import java.util.Set ; 32 33 import javax.activation.DataHandler ; 34 import javax.jbi.messaging.NormalizedMessage; 35 36 42 public class IOUtils { 43 44 private static final String DATE_FORMAT = "ddMMyyHHmmssSSS"; 45 46 56 public static void writeAttachmentsToFiles(NormalizedMessage message, 57 File destDir) throws FileTransferBCException { 58 59 SimpleDateFormat sdf = new SimpleDateFormat (DATE_FORMAT); 60 String date = sdf.format(new Date (System.currentTimeMillis())); 61 Set attachIds = message.getAttachmentNames(); 62 63 for (Object name : attachIds) { 64 DataHandler handler = message.getAttachment((String ) name); 65 FileOutputStream fos = null; 66 67 try { 68 if (!destDir.exists()) 69 destDir.mkdirs(); 70 71 File outFile = new File (destDir.getAbsolutePath(), handler 72 .getName()); 73 74 fos = new FileOutputStream (new File (outFile.getAbsoluteFile() 75 + "_" + date)); 76 77 handler.writeTo(fos); 78 fos.flush(); 79 80 } catch (Exception e) { 81 throw new FileTransferBCException("Error: writing attachment " 82 + handler.getName() + " " + e.getMessage()); 83 } finally { 84 try { 85 fos.close(); 86 } catch (IOException e) { 87 } 88 } 89 } 90 } 91 92 100 public static File writeStringToFile(String content, File destDir, 101 String fileName) throws FileTransferBCException { 102 BufferedWriter out = null; 103 File outFile = null; 104 105 SimpleDateFormat sdf = new SimpleDateFormat (DATE_FORMAT); 106 fileName = fileName + "_" + sdf.format(new Date (System.currentTimeMillis())) + ".xml"; 107 108 try { 109 if (!destDir.exists()) { 111 destDir.mkdirs(); 112 } 113 114 outFile = new File (destDir.getAbsolutePath(), fileName); 116 out = new BufferedWriter (new FileWriter (outFile.getAbsolutePath())); 117 out.write(content); 118 119 } catch (IOException e) { 120 throw new FileTransferBCException("Error creating new file " 121 + fileName, e.getMessage()); 122 } finally { 123 try { 124 out.close(); 125 } catch (IOException e) { 126 } 127 } 128 129 return outFile; 130 } 131 } 132 | Popular Tags |