關於把數據保存到oracle中的blob字段

 首先用這麼一個form字段的屬性去保存這個blob字段的值。
 <input style="width:100%" type="file" name="UploadFile" > </td>   
 然後就是調用savecmd這個動作,需要注意的是,在這個時候form表單的屬性區別已經是
 <form name="formDetail" method="post" encType="multipart/form-data">
 這就提示在邏輯中數據的提取需要用到不一樣的request的方法
 
 進入邏輯後需要用到一個關鍵那就是SaveFile()這個對象
 重點就是看:Vector vec = sf.getFileInfo(super.getSmartUpload());
 進入邏輯後,
 public Vector getFileInfo(SmartUpload su){
System.out.println("+++++++++++su::"+su); 
System.out.println("+++++++++++su.getFiles.getCount()::"+su.getFiles().getCount());
  Vector vec = new Vector();

  for (int i=0;i<su.getFiles().getCount();i++) {
   
   Hashtable hble = new Hashtable();
    
    com.jspsmart.upload.File file = su.getFiles().getFile(i);

   if (file.isMissing()) continue; 
   
   Hashtable htable = new Hashtable();
   htable.put("filename",file.getFileName());
   htable.put("file",file);
   htable.put("FieldName",file.getFieldName());
   htable.put("fileExt", file.getFileExt());
   vec.add(htable);
    
  }
  return vec;
 }
}
 用這段代碼實現的是從添加的目錄中找到需要上傳的文件名字,以及是blob字段的名字。
 代碼中的file對象是比較重要的一個對象
 首先到的是SmartUpload的這個對象裏面
 直接進入到SmartUpload這個類裏面
 public class SmartUpload
{

    public SmartUpload()
    {
        m_totalBytes = 0;
        m_currentIndex = 0;
        m_startData = 0;
        m_endData = 0;
        m_boundary = new String();
        m_totalMaxFileSize = 0L;
        m_maxFileSize = 0L;
        m_deniedFilesList = new Vector();
        m_allowedFilesList = new Vector();
        m_denyPhysicalPath = false;
        m_forcePhysicalPath = false;
        m_contentDisposition = new String();
        m_files = new Files();
        m_formRequest = new Request();
    }
 其實裏面用的也就是一個getFiles這個方法,返回的是m_files這個屬性,繼續追蹤下去,你就會看到
 實際上跑到的是Files這個文件
 public class Files
{

    Files()
    {
        m_files = new Hashtable();
        m_counter = 0;
    }

    protected void addFile(File newFile)
    {
        if(newFile == null)
        {
            throw new IllegalArgumentException("newFile cannot be null.");
        } else
        {
            m_files.put(new Integer(m_counter), newFile);
            m_counter++;
            return;
        }
    }

    public File getFile(int index)
    {
        if(index < 0)
            throw new IllegalArgumentException("File's index cannot be a negative value (1210).");
        File retval = (File)m_files.get(new Integer(index));
        if(retval == null)
            throw new IllegalArgumentException("Files' name is invalid or does not exist (1205).");
        else
            return retval;
    }

 最終返回的是一個file對象變量
 通過return retval去實現這個效果。
 
 也不知道是通過什麼實現的,返回的這個file就具有了上傳文件的類型,名稱,以及路徑,還有它的數據庫對應的字段名稱
 
 在執行玩getFileInfo()這個方
 法後,就可以得到一個包含要上傳到blob字段裏面文件的各個方面的屬性。
 
 然後繼續執行下一步的操作。
 然後通過vec.size()就可以得到一個hashtalbe然後就可以把從上面得到的有效東西放在起初從prefix裏得到的有效值。
 其中包含:文件名,以及blob字段的名字.
 

 在存放blob字段數據的時候需要執行不一樣的方法:
 
 下面來比較這兩個方法:
 

 protected boolean executeUpdate(
  String tableName,
  Hashtable data,
  String condition) {
  Connection conn = DataSourceFactory.create().getConnection();
  if (conn == null) {
   setMessage("對不起,獲取數據庫連接時出現錯誤!");
   return false;
  }
  Hashtable newData = toUperCaseHashtable(data);
  StringBuffer metaSql = new StringBuffer("select "); //用於獲取表結構的Sql語句
  StringBuffer sql = new StringBuffer("update ");
  sql.append(tableName);
  sql.append(" set ");
  Enumeration items = newData.keys();
  String item;
  while (items.hasMoreElements()) {
   item = items.nextElement().toString();
   sql.append(item);
   sql.append("=?,");
   metaSql.append(item);
   metaSql.append(",");
  }
  if (sql.charAt(sql.length() - 1) == ',')
   sql.deleteCharAt(sql.length() - 1);
  if (metaSql.charAt(metaSql.length() - 1) == ',')
   metaSql.deleteCharAt(metaSql.length() - 1);
  sql.append(" where ");
  sql.append(condition);
  metaSql.append(" from ");
  metaSql.append(tableName);
  metaSql.append(" where rownum <= 1");

  PreparedStatement pre = null;
  PreparedStatement preMeta = null; //用於獲取表結構的PreparedStatement對象
  ResultSetMetaData rsmd = null; //用於獲取表結構的ResultSetMetaData對象
  ResultSet rs = null; //用於獲取表結構的ResultSet對象
  
  try {
   conn.setAutoCommit(false);
   preMeta = conn.prepareStatement(metaSql.toString());
   rs = preMeta.executeQuery();
   rsmd = rs.getMetaData();
   pre = conn.prepareStatement(sql.toString());
   Object columnValue;
   for (int i = 1; i <= rsmd.getColumnCount(); i++) {
    columnValue = newData.get(rsmd.getColumnName(i).toUpperCase());
    
    pre =
     setColumnValue(
      conn,
      pre,
      i,
      rsmd.getColumnType(i),
      columnValue);
   }
   pre.execute();

   conn.commit();
   effRow = pre.getUpdateCount();
   return true;
  } catch (ParseException e) {
   try {
    conn.rollback();
   } catch (SQLException e1) {
    e1.printStackTrace();
   }
   System.out.println("數據轉換錯誤:" + e.getMessage());
   setMessage("數據轉換錯誤:" + e.getMessage());
   return false;
  } catch (SQLException e) {
   try {
    conn.rollback();
   } catch (SQLException e1) {
    e1.printStackTrace();
   }
   System.out.println("插入記錄錯誤:" + e.getMessage());
   System.out.println("錯誤的SQL語句爲: " + sql.toString());
   System.out.print("錯誤時的插入數據爲:" + newData);
   setMessage("插入記錄錯誤:" + e.getMessage());
   return false;
  } catch (IOException e) {
   try {
    conn.rollback();
   } catch (SQLException e1) {
    e1.printStackTrace();
   }
   System.out.println("文件數據讀取錯誤:" + e.getMessage());
   setMessage("文件數據讀取錯誤:" + e.getMessage());
   return false;
  } catch (Exception e) {
   try {
    conn.rollback();
   } catch (Exception e1) {
    e1.printStackTrace();
   }
   System.out.println("未知錯誤:" + e.getMessage());
   System.out.println("錯誤時的SQL語句爲: " + sql.toString());
   System.out.print("錯誤時的插入數據爲:" + newData);
   setMessage("對不起,發生未知錯誤!\\n" + e.getMessage());
   return false;
  } finally {
   try {
    if (preMeta != null) {
     preMeta.close();
    }
   } catch (SQLException e) {
    System.out.println("關閉prepareStatement對象時錯誤:" + e.getMessage());
   }
   try {
    if (pre != null) {
     pre.close();
    }
   } catch (SQLException e) {
    System.out.println("關閉prepareStatement對象時錯誤:" + e.getMessage());
   }
   try {
    if (conn != null) {
     conn.setAutoCommit(true);
    }
   } catch (SQLException e) {
    System.out.println("恢復Connection對象時錯誤:" + e.getMessage());
   }
   try {
    if (conn != null) {
     conn.close();
    }
   } catch (SQLException e) {
    System.out.println("關閉Connection對象時錯誤:" + e.getMessage());
   }
  }
 }

 下面這個是用來主攻存放blob字段數據的:
 

 protected boolean updateLob(
  String table,
  Hashtable data,
  String conditions) {
  if (table == null) {
   System.out.println("沒有指定更新表!");
   return false;
  }
  if (data == null) {
   System.out.println("沒有指定要更新的數據!");
   return false;
  }
  if (conditions == null) {
   System.out.println("沒有用於定位記錄的條件!");
   return false;
  }
  Hashtable newData = toUperCaseHashtable(data);
  Connection conn = DataSourceFactory.create().getConnection();
  if (conn == null) {
   setMessage("對不起,獲取數據庫連接失敗!");
   return false;
  }
  StringBuffer sql = new StringBuffer("select ");
  Enumeration items = newData.keys();
  String item;
  while (items.hasMoreElements()) {
   item = items.nextElement().toString();
   sql.append(item);
   sql.append(",");
  }
  if (sql.charAt(sql.length() - 1) == ',')
   sql.deleteCharAt(sql.length() - 1);
  sql.append(" from ");
  sql.append(table);
  sql.append(" where ");
  sql.append(conditions);
  sql.append(" for update");
  PreparedStatement pre = null;
  ResultSet rs = null;
  ResultSetMetaData rsmd = null;
  try {
   conn.setAutoCommit(false);
   pre = conn.prepareStatement(sql.toString());
   rs = pre.executeQuery();
   if (rs.next()) {
    rsmd = rs.getMetaData();
    BLOB blob = null;
    CLOB clob = null;
    for (int i = 1; i <= rsmd.getColumnCount(); i++) {
     switch (rsmd.getColumnType(i)) {
      case Types.BLOB :
       blob = (BLOB) rs.getBlob(i);
       if (blob != null) {
        blob.trim(0);
        OutputStream blobout =
         blob.getBinaryOutputStream();
        Object obj = newData.get(rsmd.getColumnName(i));
        if (obj instanceof File) {
         blobout.write(((File) obj).getBytes());
        } else if (obj instanceof String) {
         blobout.write(
          (new BASE64Decoder()).decodeBuffer(
           obj.toString()));
        }
        blobout.flush();
        blobout.close();
       }
       break;
      case Types.CLOB :
       clob = (CLOB) rs.getClob(i);
       if (clob != null) {
        //System.out.println("CLOB*****");
        clob.trim(0);
        Writer clobWriter =
         clob.getCharacterOutputStream();
        if (newData
         .get(rsmd.getColumnName(i))
         .toString()
         .equals("")) {
         clobWriter.write(" ");
        } else {
         clobWriter.write(
          newData
           .get(rsmd.getColumnName(i))
           .toString());
        }
        clobWriter.flush();
        clobWriter.close();
       }
       break;
      default :
       break;
     }
    }
    conn.commit();
    return true;
   }
   System.out.println("沒有檢索到要更新的記錄!");
   System.out.println("錯誤的SQL語句爲: " + sql.toString());
   return false;
  } catch (SQLException e) {
   try {
    conn.rollback();
   } catch (SQLException e1) {
    e1.printStackTrace();
   }
   System.out.println("修改LOB數據時錯誤:" + e.getMessage());
   System.out.println("錯誤的SQL語句爲: " + sql.toString());
   System.out.println("錯誤的數據爲: " + data);
   setMessage("對不起,插入記錄發生錯誤!\\n" + e.getMessage());
   return false;
  } catch (IOException e) {
   try {
    conn.rollback();
   } catch (SQLException e1) {
    e1.printStackTrace();
   }
   System.out.println("文件數據讀取錯誤:" + e.getMessage());
   setMessage("對不起,文件數據讀取發生錯誤!\\n" + e.getMessage());
   return false;
  } catch (Exception e) {
   try {
    conn.rollback();
   } catch (Exception e1) {
    e1.printStackTrace();
   }
   System.out.println("未知錯誤:" + e.getMessage());
   System.out.println("錯誤時的SQL語句爲: " + sql.toString());
   System.out.println("錯誤的數據爲: " + data);
   setMessage("對不起,發生未知錯誤!\\n" + e.getMessage());
   return false;
  } finally {
   try {
    if (rs != null) {
     rs.close();
    }
   } catch (Throwable e) {
    System.out.println("關閉rs對象時錯誤:" + e.getMessage());
   }
   try {
    if (pre != null) {
     pre.close();
    }
   } catch (SQLException e) {
    System.out.println("關閉prepareStatement對象時錯誤:" + e.getMessage());
   }
   try {
    if (conn != null) {
     conn.setAutoCommit(true);
    }
   } catch (SQLException e) {
    System.out.println("恢復Connection對象時錯誤:" + e.getMessage());
   }
   try {
    if (conn != null) {
     conn.close();
    }
   } catch (SQLException e) {
    System.out.println("關閉Connection對象時錯誤:" + e.getMessage());
   }
  }
 }


 在這兩塊都結束後,便完成了對blob字段數據庫的存放。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章