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 3225240
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:19:00+00:00 2026-05-17T16:19:00+00:00

Here is the latest code. I wanted to try something different but I am

  • 0

Here is the latest code. I wanted to try something different but I am still getting the same results.

I am using postgres DB and I created a column called file with a data type bytea.
The image does get uploaded to the DB (I can see it in there when I view the table’s data).

I tried it with several images. The size appears to be correct when I upload it but then it doubles in size when I download it. (which breaks the image).

Any help on why this is not working is greatly appreciated.

public void uploadImage(File image, String imageName) throws SQLException {     
    try {
        conn = connectionManager.ConnectToDb();

        String sql = "INSERT INTO images (name, file) "+ "VALUES(?,?)";
        System.out.println("your image name is " + imageName);
        System.out.println("Uploaded image name is " + image);      

        PreparedStatement stmt = conn.prepareStatement(sql);
        stmt.setString(1,imageName);

        fis = new  FileInputStream(image);
        stmt.setBinaryStream(2, fis, (int) image.length());

        System.out.println("Image SIZE = " +image.length());

        stmt.execute(); 
        conn.commit();

    } catch (SQLException e) {
        System.out.println("Could not insert into DB");
        e.printStackTrace();        
    } catch (FileNotFoundException e) {
            System.out.println("Could not insert into DB");
            e.printStackTrace();
    } 
     finally {
        //close DB Objects
        try {
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        conn.close();
    }
}

public void downloadImageFromDB(String imagename) throws SQLException
{
    ConnectionManager connectionManager = new ConnectionManager();

    try {
        conn = connectionManager.ConnectToDb();
        Statement downloadImageStatement = conn.createStatement();
        downloadImageStatement.executeQuery("SELECT file FROM images WHERE name = '"+imagename+"'");
        ResultSet rs = downloadImageStatement.getResultSet();
        int i=1;
        InputStream in = null;
        int returnValue = 0;

        if(rs.next())
        {
            String len1 = rs.getString("file");
            int len = len1.length();
            byte [] b = new byte[len];
            in = rs.getBinaryStream("file");
            int index = in.read(b, 0, len);
            OutputStream outImej = new FileOutputStream("C:\\EclipseProjects\\StrutsHelloWorld\\"+imagename+".JPG");

            while (index != -1)
            {
            outImej.write(b, 0, index);
            index = in.read(b, 0, len);
            System.out.println("==========================");
            System.out.println(index);
            System.out.println(outImej);
            System.out.println("==========================");
            }
            outImej.close();
        }else
            {
            returnValue = 1;
            }

        downloadImageStatement.close();
        conn.close();

    } catch (SQLException e) {
        System.out.println("Could not get image from DB");
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
    finally
    {
      // close db objects.
        conn.close();
    }
    //return imgData;
}
  • 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-05-17T16:19:00+00:00Added an answer on May 17, 2026 at 4:19 pm

    You don’t want to use InputStream.available() to measure its size. Here’s a Q&A that describes what available() actually does – in short, not much that’s useful.

    One (correct) way to get the actual size is to check the size of the File after you’ve written to it, using File.length().

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

Sidebar

Related Questions

This is often situation, but here is latest example: Companies have various contact data
Well, here's a nice obscure one. I'm trying to compile the latest open transport
Here is my code, which takes two version identifiers in the form 1, 5,
I have this old C++ COM component. I took the latest code base, built
Here's a basic regex technique that I've never managed to remember. Let's say I'm
Here's a problem I ran into recently. I have attributes strings of the form
Here is the issue I am having: I have a large query that needs
Here's my scenario - I have an SSIS job that depends on another prior
Here is a simplification of my database: Table: Property Fields: ID, Address Table: Quote
Here's a coding problem for those that like this kind of thing. Let's see

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.