2012-11-03 39 views
1

我有一個使用PostgreSQL的,JSP和Struts框架上傳文件到PostgreSQL大對象(「LO」 /「OID」)在DB

的應用程序,我想文件插入一個PostgreSQL中的表使用OID類型,所以它作爲一個大對象存儲在數據庫中。

我的表的定義是這樣的一個:

CREATE TABLE mensaje 
(
    id serial NOT NULL, 
    file oid, 
    CONSTRAINT pk_mensaje PRIMARY KEY (id) 
) 
WITH (
    OIDS=TRUE 
); 
ALTER TABLE mensaje 
OWNER TO postgres; 

有人知道如何Action,該ActionForm.jsp應該是一個例子嗎?

如果沒有,有沒有解釋如何做到這一點不使用OID類型的任何其它的例子嗎?

+0

有存儲在文件的PostgreSQL三種方式:(a)用OID類型爲'pg_largeobject'引用,[優選使用'lo'的contrib模塊來管理它們(http://www.postgresql.org /docs/current/interactive/lo.html); (b)作爲表列中的bytea值,最好只適用於較小的文件; (c)在文件系統的外部,僅存儲數據庫中的文件名。看到這個先前的答案:http://stackoverflow.com/a/11929369/398670 –

回答

2

這是一個兩個步驟的過程來解決該問題:

  1. File upload using Struts 2
  2. PostgreSQL Java tutorial,檢查寫入圖像部。

附加說明:一旦文件在您的操作中被接收到,您應該使用字節數組數據將其保存在您的OID字段中。


從您的評論,這應該是在Struts的1.x的

在JSP

<html:form action="fileUploadAction" method="post" enctype="multipart/form-data"> 
    File : <html:file property="upload" /> 
    <br /> 
    <html:submit /> 
</html:form> 

在你的動作類

YourForm uploadForm = (YourForm) form; 
FormFile file = null; 
try { 
    file = uploadForm.getFile(); 
    //FormFile#getFileData() returns the byte array containing the file data 
    //You can use it to save the file in your database and other things you want/need 
    int id = 9001; //assuming this is a valid id in the mensaje table 
    MensajeService mensajeService = new MensajeService(); 
    mensajeService.saveFile(id, file.getFileData()); 
} catch (Exception e) { 
    //log the errors for maintenance purposes (bugs, fixes, etc) 
} 

MensajeService類的方式將連接到您的Postgre數據庫並保存文件

適於從10
public class MensajeService { 

    public MensajeService() { 
    } 

    public void saveFile(int id, byte[] fileData) throws SQLException { 
     //this is a very simple skeleton, you have to adapt this to 
     //your needs, the way you're connecting to dabatase, etc... 
     Connection con = null; 
     PreparedStatement pstmt = null; 
     try { 
      con = ... //get the connection to your postgre db 

      //Initialize a new transaction 
      con.setAutoCommit(false); 
      // Get the Large Object Manager to perform operations with 
      LargeObjectManager lobj = ((org.postgresql.PGConnection)conn) 
       .getLargeObjectAPI(); 
      // Create a new large object 
      int oid = lobj.create(LargeObjectManager.READ | LargeObjectManager.WRITE); 
      // Open the large object for writing 
      LargeObject obj = lobj.open(oid, LargeObjectManager.WRITE); 
      //in the provided example, the code shows a way to get the byte array data 
      //from the file (using the File and FileInputStream classes) 
      //you don't need all that because you already have the byte array (good!) 
      //so you only write the binary data in your LargeObject (OID) object 
      obj.write(fileData); 

      //creating the SQL statement to insert the OID 
      String sql = "INSERT INTO mensaje VALUES (?, ?)"; 
      pstmt = con.prepareStatement(sql); 
      pstmt.setInt(1, id); 
      ps.setInt(2, oid); 
      // 
      pstmt.setBinaryStream(2, fin, (int) img.length()); 
      //saving the file 
      pstmt.executeUpdate(); 
      //closing the transaction successfully 
      con.commit(); 
     } catch (SQLException e) { 
      //error in the transaction, start a rollback 
      if (con != null) { 
       con.rollback(); 
      } 
      throw e; 
     } finally { 
      //don't forget to free the resources after using them 
      pstmt.close(); 
      con.close(); 
     } 
    } 
} 

Struts的1代碼:Uploading a file in struts1。改編自here

PostreSQL代碼。

+0

對不起,我編輯了標籤。這是否也適用於Struts 1? – camelCase

+0

@Mike回答更新。 –