Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将文件从Struts/JSP上载到数据库中的PostgreSQL大型对象(“lo”/“oid”)_Java_Jsp_Postgresql_Struts - Fatal编程技术网

Java 将文件从Struts/JSP上载到数据库中的PostgreSQL大型对象(“lo”/“oid”)

Java 将文件从Struts/JSP上载到数据库中的PostgreSQL大型对象(“lo”/“oid”),java,jsp,postgresql,struts,Java,Jsp,Postgresql,Struts,我有一个使用PostgreSQL、JSP和STRUTS框架的应用程序 我想在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 p

我有一个使用PostgreSQL、JSP和STRUTS框架的应用程序

我想在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;
有人知道
操作
操作表单
jsp
应该如何使用的例子吗


如果没有,是否有其他示例说明如何在不使用OID类型的情况下执行此操作?

这是一个两步过程来解决此问题:

  • ,检查“写入图像”部分
  • 附加说明:在操作中收到文件后,应使用字节数组数据将其保存在
    OID
    字段中


    从您的评论来看,Struts1.x应该是这样的

    在JSP中

    <html:form action="fileUploadAction" method="post" enctype="multipart/form-data">
        File : <html:file property="upload" /> 
        <br />
        <html:submit />
    </html:form>
    
    MensajeService
    类将连接到您的Postgre数据库并保存文件

    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代码改编自:


    PostreSQL代码改编自。

    在PostgreSQL中存储文件有三种方法:(a)oid类型作为对
    pg_largeobject
    的引用;(b) 作为表列中的
    bytea
    值,最好仅用于较小的文件;(c) 在文件系统外部,仅在数据库中存储文件名。请参阅前面的回答:对不起,我编辑了标签。这也适用于Struts 1吗?
    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();
            }
        }
    }