저는 Postgres PostgreSQL 9.1을 사용하고 있고 컬럼 바이트 데이터 타입을 가진 테이블을 가지고 있습니다.이미지 크기가 포스트 그레스에서 일치하지 않습니다. 9.1
package com.q4;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.sql.*;
public class DataTest {
public void insert() throws Exception
{
Connection c = ConnectionHelper.getConnection();
FileInputStream fis = new FileInputStream("C:\\pics\\viticcio.jpg");
int counter = 0;
String sql ="insert into emp(uname, pass,name,photo) values (?,?,?,?)";
PreparedStatement pstmt = c.prepareStatement(sql);
pstmt.setString(1, "Senthil1");
pstmt.setString(2, "Password");
pstmt.setString(3, "Senthil1");
//pstmt.setBlob(4, fis);
while(fis.read() != -1) counter++;
byte[] b= new byte[counter];
fis.close();
fis = new FileInputStream("C:\\pics\\viticcio.jpg");
for(int i = 0;i<counter;i++)
{
b[i] = (byte)fis.read();
// System.out.print(b[i]);
}
System.out.println("Input File Size : " + counter); //Output 1
System.out.println(counter);
pstmt.setBytes(4, b);
// pstmt.setBlob(4, fis, counter);
pstmt.executeUpdate();
System.out.println("Successfully insertted ...");
}
public void select() throws Exception
{
String sql = "select * from emp where uname = ?";
Connection c = ConnectionHelper.getConnection();
PreparedStatement pstmt = c.prepareStatement(sql);
pstmt.setString(1, "Senthil1");
ResultSet set = pstmt.executeQuery();
if(set.next())
{
FileOutputStream fos = new FileOutputStream("C:\\test.jpg");
byte[] a = set.getBytes("photo");
System.out.println("Output Filesize : "+a.length); //Output 2
for(int i = 0; i <a.length;i++)
{
fos.write((byte)a[i]);
// System.out.print((byte)a[i]);
fos.flush();
}
fos.close();
}
pstmt.close();
c.close();
}
public static void main(String[]s) throws Exception
{
new DataTest().insert();
new DataTest().select();
}
}
을 다음과 같이
CREATE TABLE emp
(
uname character varying(100) NOT NULL,
pass character varying(100) NOT NULL,
name character varying(100) NOT NULL,
dob date,
country character varying(20),
region character varying(20),
description character varying(3000),
role character varying(100),
photo bytea,
CONSTRAINT emp_pkey PRIMARY KEY (uname)
)
WITH (
OIDS=FALSE
);
ALTER TABLE emp
OWNER TO postgres;
샘플 자바 코드는 다음과 같이 이미지를 삽입하려 할 때, 테이블에 이미지를 삽입 할 수있는 테이블의 스키마입니다 output이 프로그램을 실행할 때 출력이 나오고 test.jpg 파일이 C : \에 생성되지만이 파일의 크기는 읽은 파일 크기의 두 배입니다.
Input File Size : 6455
6455
Successfully insertted ...
SELECT ......... 12909
무엇이 문제의 근본 원인 일 수 있는지 명확히하십시오. 사전에 감사
센씰
감사합니다, 문제가 해결 된 것입니다. postgres 9.1을 사용하고 postgres 8.2 용 드라이버를 사용하고 있습니다. 내가 postgres 9.1에 대한 드라이버를 바꿀 때 문제가 해결되었습니다. – user1503117