使用java从PostgreSQL中的bytea检索文件 [英] Retrieving file from bytea in PostgreSQL using java

查看:1033
本文介绍了使用java从PostgreSQL中的bytea检索文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在使用以下代码从postgresql bytea使用java,
检索文件但在文件中我得到的数字如314530413142313141

Hi I'm using the below code to retrieve the file from the postgresql bytea using java, but inside the file I'm getting numbers like 314530413142313141

File file = new File("c:/test.doc");
FileOutputStream fos = new FileOutputStream(file);
ResultSet rs = st.executeQuery("SELECT * FROM test_bytea where id=" + 1);
        if (rs != null) {
            while (rs.next()) {

                byte[] fileBytes = new byte[1024];
                InputStream is = rs.getBinaryStream("type_file");
                while (is.read(fileBytes) > 0) {
                    fos.write(fileBytes);
                }

                // use the stream in some way here
            }
            rs.close();
        }    

请告诉我代码中出了什么问题?

Please let me know what goes wrong in my code?

推荐答案

数据被转义(以\ x开头,然后是每个字节的十六进制两个字符)这是来自bytea字段的内容。你需要在将它存储到文件中之前将其取消。

The data is escaped ( starts with \x and then is hexadecimal two chars for each byte) this is what comes out of a bytea field. you need to unescape it before storing it in the file.

这篇关于使用java从PostgreSQL中的bytea检索文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆