Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1az5f2-0006sN-D6 for pgsql-jdbc@arkaria.postgresql.org; Sat, 07 May 2016 17:05:00 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1az5f1-0004vw-WA for pgsql-jdbc@arkaria.postgresql.org; Sat, 07 May 2016 17:05:00 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1az5eX-0004Oh-LQ for pgsql-jdbc@postgresql.org; Sat, 07 May 2016 17:04:29 +0000 Received: from mbob.nabble.com ([162.253.133.15]) by makus.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1az5eU-0003CV-Vt for pgsql-jdbc@postgresql.org; Sat, 07 May 2016 17:04:28 +0000 Received: from msam.nabble.com (unknown [162.253.133.85]) by mbob.nabble.com (Postfix) with ESMTP id C7FAC270AE23 for ; Sat, 7 May 2016 09:49:01 -0700 (PDT) Date: Sat, 7 May 2016 10:04:25 -0700 (MST) From: "peter.penzov" To: pgsql-jdbc@postgresql.org Message-ID: <1462640665299-5902501.post@n5.nabble.com> In-Reply-To: References: <1462562098748-5902345.post@n5.nabble.com> <1462568821046-5902400.post@n5.nabble.com> Subject: Re: org.postgresql.util.PSQLException: ERROR: column "file" is of type oid but expression is of type bytea MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -0.3 (/) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-jdbc Precedence: bulk Sender: pgsql-jdbc-owner@postgresql.org I managed to create this: public void upload() throws SQLException, GSSException { if (file != null) { try { InputStream inputStream = file.getInputStream(); Connection conn = ds.getConnection(); PreparedStatement ps = null; boolean committed = false; try { conn.setAutoCommit(false); ps = conn.prepareStatement("INSERT INTO PROCEDURE_FILES (ID, PROCEDURE_ID, FILE_NAME, FILE) " + " VALUES (?, ?, ?, ?)"); ps.setInt(1, obj.number); ps.setInt(2, obj.number); ps.setString(3, file.getSubmittedFileName()); ps.setBlob(4, inputStream, inputStream.available()); ps.executeUpdate(); ps.close(); conn.commit(); committed = true; } catch (SQLException e) { e.printStackTrace(); } finally { if (!committed) { conn.rollback(); } if (ps != null) { ps.close(); } conn.close(); } } catch (IOException e) { FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Upload failed!")); } } } public void initFileDBData() throws SQLException, IOException { Connection conn = ds.getConnection(); PreparedStatement ps = null; try { conn.setAutoCommit(false); ps = conn.prepareStatement("SELECT * FROM PROCEDURE_FILES WHERE ID = ?"); ps.setInt(1, id); ResultSet rs = ps.executeQuery(); while (rs.next()) { String file_name = rs.getString("FILE_NAME"); Blob oid = rs.getBlob("FILE"); InputStream binaryStreasm = oid.getBinaryStream(); FacesContext fc = FacesContext.getCurrentInstance(); ExternalContext ec = fc.getExternalContext(); ec.responseReset(); ec.setResponseContentLength(binaryStreasm.available()); ec.setResponseHeader("Content-Disposition", "attachment; filename=\"" + file_name + "\""); byte[] buf; buf = new byte[binaryStreasm.available()]; int offset = 0; int numRead = 0; while ((offset < buf.length) && ((numRead = binaryStreasm.read(buf, offset, buf.length - offset)) >= 0)) { offset += numRead; } HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance() .getExternalContext().getResponse(); response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment;filename=" + file_name); response.getOutputStream().write(buf); response.getOutputStream().flush(); response.getOutputStream().close(); FacesContext.getCurrentInstance().responseComplete(); } } finally { if (ps != null) { ps.close(); } conn.close(); } } But when I download the file it's always 0 bytes in size. Do you have any idea how I can fix this? -- View this message in context: http://postgresql.nabble.com/org-postgresql-util-PSQLException-ERROR-column-file-is-of-type-oid-but-expression-is-of-type-bytea-tp5902345p5902501.html Sent from the PostgreSQL - jdbc mailing list archive at Nabble.com. -- Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-jdbc