1 32 33 package com.knowgate.hipergate; 34 35 import java.lang.System ; 36 37 import java.io.File ; 38 39 import java.sql.SQLException ; 40 import java.sql.Connection ; 41 import java.sql.PreparedStatement ; 42 43 import com.knowgate.dataobjs.DB; 44 45 50 public class FileHandler { 51 52 public FileHandler() { 53 sFileSeparator = System.getProperty("file.separator"); 54 } 55 56 58 private String getExtension(String sFile) { 59 int iLast = sFile.lastIndexOf("."); 60 61 if (iLast>0) 62 return sFile.substring(++iLast); 63 else 64 return ""; 65 } 67 69 public void upload(String sPath, String sFile, Connection oConn, String sLocationId) throws SQLException { 70 int iFileLen; 71 File oOldFile; 72 String sExt; 73 String sUniqueName; 74 PreparedStatement oStmt; 75 76 if (!sPath.endsWith(sFileSeparator)) sPath = sPath + sFileSeparator; 77 78 sExt = getExtension(sFile); 79 80 sUniqueName = (sExt.length()==0 ? sLocationId : sLocationId + "." + sExt); 81 82 oOldFile = new File (sPath + sFile); 83 iFileLen = new Long (oOldFile.length()).intValue(); 84 oOldFile.renameTo(new File (sPath + sUniqueName)); 85 oOldFile = null; 86 87 if (null!=oConn && null!=sLocationId) { 88 oStmt = oConn.prepareStatement("UPDATE " + DB.k_prod_locats + " SET " + DB.xfile + "=?," + DB.len_file + "=? WHERE " + DB.gu_location + "=?"); 89 oStmt.setString(1, sUniqueName); 90 oStmt.setInt(2, iFileLen); 91 oStmt.setString(3, sLocationId); 92 oStmt.executeUpdate(); 93 oStmt.close(); 94 oStmt = null; 95 } } 98 100 public void delete(String sPath, String sFile, Connection oConn, String sLocationId) throws SQLException { 101 File oFile; 102 String sPathFile; 103 PreparedStatement oStmt; 104 105 if (null!=sPath && null!=sFile) { 106 if (sPath.endsWith(sFileSeparator)) 107 sPathFile = sPath + sFile; 108 else 109 sPathFile = sPath + sFileSeparator + sFile; 110 111 oFile = new File (sPathFile); 112 oFile.delete(); 113 oFile = null; 114 } 116 if (null!=oConn && null!=sLocationId) { 117 oStmt = oConn.prepareStatement("DELETE " + DB.k_prod_locats + " WHERE " + DB.gu_location + "=?"); 118 oStmt.setString(1, sLocationId); 119 oStmt.executeUpdate(); 120 oStmt.close(); 121 oStmt = null; 122 } } 125 127 private String sFileSeparator; 128 } | Popular Tags |