Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8779687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:51:31+00:00 2026-06-13T19:51:31+00:00

I have an application that uses PostgreSQL, JSP and the STRUTS Framework I want

  • 0

I have an application that uses PostgreSQL, JSP and the STRUTS Framework

I want to insert a file into a table in PostgreSQL using the OID type, so it’s stored as a large object in the database.

My table definition is this one:

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

Anybody know an example of how the Action, the ActionForm and the .jsp should be?

If not, is there any other example that explains how to do it without using the OID type?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T19:51:32+00:00Added an answer on June 13, 2026 at 7:51 pm

    This is a two step process to solve the problem:

    1. File upload using Struts 2
    2. PostgreSQL Java tutorial, check the Writing images section.

    Additional note: Once the file has been received in your Action, you should use the byte array data to save it in your OID field.


    From your comment, this should be the way in Struts 1.x

    In the JSP

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

    In your action class

    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) 
    }
    

    The MensajeService class will connect to your Postgre database and save the file

    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 code adapted from: Uploading a file in struts1.

    PostreSQL code adapted from here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that uses the Mojolicious framework. I have a table in
I am thinking of working on a Rails application that uses PostgreSQL. I have
I have an application that uses Rave Reports on Delphi 7. I want the
I have a spring application that uses several services such as MySQL, PostgreSQL, and
I have an application that uses the Facebook SDK for Android to login into
I have an application that uses Entity Framework to access the data, and throughout
I have client application that uses WCF service to insert some data to backend
I have an application that uses xml config file, where I'm describing layouts for
I have an application that uses an aps:GridView. The GridView is populated using a
I have an application that uses a VBScript file within operation to make HTTP

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.