2017-04-20 52 views
0

目前,我可以从数据库中获取将处于BLOB数据类型的数据。 因此,在代码中它将存储在字节[]中。 现在我可以通过提字符集(eg.UTF-8)作为下面的字节数组转换为字符串:如何将字节数组转换为所有文件类型的字符串

public byte[] mtdFileEncryption(byte[] myData,String fileName) throws DNSException, Exception 
{ 
    String strLocalFolder = SysProperties.getprocessFile_LOCAL_FOLDER(); 
    String strOnlineSSTPublicKey = SysProperties.getOnline_SST_ENCRYPTION_PUBLIC_KEY(); 
    String strFilename = fileName; 
    PGPFileEncryptor fileEncryptor = new com.test.PGPFileEncryptor(); 

    File outputFile = null; 
    FileOutputStream fos = null; 
    OutputStreamWriter osw = null; 
    BufferedWriter bufferOutput = null; 

    File toFile = new File(strLocalFolder); 
    if(!toFile.isDirectory()) 
    { 
    //Create temp folder 
    toFile.mkdir(); 
    } 

    try 
    { 
     //generate file to local folder 
     //outputFile = new File(strLocalFolder + strFilename + ".txt"); 

     outputFile = new File(strLocalFolder + strFilename); 
     fos = new FileOutputStream(outputFile); 
     osw = new OutputStreamWriter(fos); 
     bufferOutput = new BufferedWriter(osw); 
     //WORKING FOR TXT 
     String str = new String(myData, "UTF-8"); 
     bufferOutput.write(str); 

    }catch(Exception e) 
    { 
     e.printStackTrace(); 
     throw e; 
    }finally 
    { 
     if(bufferOutput != null) 
     { 
      bufferOutput.close(); 
      bufferOutput = null; 
     } 

     if (osw != null) 
     { 
      osw.close(); 
      osw = null; 
     } 

     if (fos != null) 
     { 
      fos.close(); 
      fos = null; 
     } 
    } 

这意味着以下可以被转换为字符串,并我可以查看的内容.TXT存储的文件

//WORKING FOR TXT 
     String str = new String(myData, "UTF-8"); 
     bufferOutput.write(str); 

但这种情况不会发生其他文件,.PDF或.xls 我无法查看内容。 我可以知道如何在不指定字符集的情况下将字节数组(myData)转换为所有文件类型(.pdf,.xls等)的字符串?

以下是如何将字节数组[]传递给方法。 file_content是表中的BLOB数据类型。

String contentString = rsStatementRequest.getValue("file_content"); 
        BASE64Decoder en = new BASE64Decoder(); 
        byte[] contentBytes = en.decodeBuffer(contentString); 
contentBytes = mtdFileEncryption(contentBytes,fileName); 
+1

尝试使用普通文本编辑器打开同一pdf文件,您将看到相同的结果。所以你的转换操作是成功的,但不是逻辑。对于转换字节后的每种文件类型,您应该使用适当的解析器 – strash

+0

该线程可能有帮助:http://stackoverflow.com/questions/5449903/advanced-pdf-parser-for-java – strash

+0

“二进制字节列表(Blob)“可以容纳无限数据,但是字节数组只能通过int值进行索引,所以它对int的长度有限制(实际上由于某种原因intmax-8)。 –

回答

2

我建议以二进制形式写入byte [] BLOB。写作是因为文本可能会破坏它,尤其是如果它被加密的话。

public void mtdFileEncryption(byte[] myData,String fileName) throws IOException { 
    String strLocalFolder = SysProperties.getprocessFile_LOCAL_FOLDER(); 
    new File(strLocalFolder).mkdir(); 

    try (FileOutputStream fos = new FileOutputStream(new File(strLocalFolder, fileName)) { 
     fos.write(myData); 
    } 
} 
+0

嗨,彼得,谢谢你。这是最简单的解决方案,它为PDF工作。我希望它适用于所有其他类型的文件,如.docx,.xls等。如果我错了,请纠正我。 – Albert

+0

这是我的代码:我删除了上面显示的BufferedWriter和OutputStreaWriter类: outputFile = new File(strLocalFolder + strFilename); \t \t fos = new FileOutputStream(outputFile); \t \t fos.write(myData); – Albert

+0

@Albert如果你使用'new File(String,String)',你不必担心路径分隔符。 –

相关问题