Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
utf-8 oracle clob,用java编写/读取pdf?_Java_Php_Oracle_Clob - Fatal编程技术网

utf-8 oracle clob,用java编写/读取pdf?

utf-8 oracle clob,用java编写/读取pdf?,java,php,oracle,clob,Java,Php,Oracle,Clob,我的任务是使用java webapp更改2个php页面,该应用将上传pdf文件写入clob,并在用户请求下载时读取 我威胁说pdf是一个字节数组,并且能够正确地读/写它 最大的问题是向后兼容性:php编写的文件不能被java webapp读取,反之亦然 提前谢谢你的帮助 注意:不要回答我使用Blob,我知道这是一种简单的方法,但在这种情况下,我们必须假设由于向后兼容性,我们无法在db上创建alter表 下面是我将clob读入字节数组的代码: byte[] result = null; Input

我的任务是使用java webapp更改2个php页面,该应用将上传pdf文件写入clob,并在用户请求下载时读取

我威胁说pdf是一个字节数组,并且能够正确地读/写它

最大的问题是向后兼容性:php编写的文件不能被java webapp读取,反之亦然

提前谢谢你的帮助

注意:不要回答我使用Blob,我知道这是一种简单的方法,但在这种情况下,我们必须假设由于向后兼容性,我们无法在db上创建alter表

下面是我将clob读入字节数组的代码:

byte[] result = null;
InputStream            is = null;
ByteArrayOutputStream bos = null;
//...prepare the query to get clob as first column in the resultset
ResultSet rs = stmt.executeQuery();
int len;
int size = 1024;
byte[] buf;
if(rs.next()) {
      is = rs.getBinaryStream(1);
      bos = new ByteArrayOutputStream();
      buf = new byte[size];
      while ((len = is.read(buf, 0, size)) != -1)
         bos.write(buf, 0, len);
      bos.flush();
      bos.close();
      is.close();
      result = bos.toByteArray();
}

rs.close();
下面是将字节数组写入clob的代码:

//...some other sql stuff here...
stmt = conn.prepareStatement("SELECT clob_col FROM my_table WHERE prim_key = ? FOR UPDATE");
stmt.setString(1, param);

ResultSet rs = stmt.executeQuery();
Clob myclob=null;
if(rs.next())
    myclob=rs.getClob(1);

OutputStream writer = myclob.setAsciiStream(1L);
writer.write(allegato);
writer.flush();
writer.close();
stmt = conn.prepareStatement("UPDATE my_table SET clob_col = ? WHERE prim_key = ? ");
stmt.setClob(1, myclob);
stmt.setString(2, param);
stmt.executeUpdate();
oracle编码是意大利语。WE8ISO8859P1 php编码是意大利语。UTF8是一种可能的解决方案: 向clob写入字节数组的十六进制表示,并在读取阶段执行相同操作

主要优势是 -php和java中的一些更改 -对db alter table没有更改 -独立于数据库编码

在php中,我们在编写clob之前和读取clob之后使用and函数

在java中,我们实现了bin2hex和hex2bin的两个简单等效函数:

    public static byte[] HexToBin(String str){
        try{
            byte[] result=new byte[str.length()/2];
            for (int i = 0; i < result.length; i++)
                result[i]=(byte) Integer.parseInt(str.substring(2*i, (2*i)+2), 16);
            return result;
        }catch(Exception x){
            x.printStackTrace();
            return null;
        }
    }

    public static String BinToHex(byte[] b){
        try{
            StringBuffer sb=new StringBuffer();
            for (byte bb:b) {
                String hexStr = String.format("%02x", bb);
                sb.append((hexStr.length()<2)?"0":"");
                sb.append(hexStr);
            }
            return sb.toString();
        }catch(Exception x){
            x.printStackTrace();
            return null;
        }
    }

您不想使用Blob,但这可能正是您的问题所在。如果将类似pdf的二进制文件存储在CLOB而不是BLOB中,则可能会出现文件编码问题和其他问题。此外,问题是字符集转换。正确的答案是使用BLOB。如果确定不使用正确的解决方案,则需要在每个应用程序流经系统时检查二进制数据,确定字符集转换发生的位置,然后确定如何模拟旧系统如何在新系统中损坏数据。很有可能需要进行不受支持的配置设置,以防止其他应用程序正确读取此数据。