目前,我可以从数据库中获取将处于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);
尝试使用普通文本编辑器打开同一pdf文件,您将看到相同的结果。所以你的转换操作是成功的,但不是逻辑。对于转换字节后的每种文件类型,您应该使用适当的解析器 – strash
该线程可能有帮助:http://stackoverflow.com/questions/5449903/advanced-pdf-parser-for-java – strash
“二进制字节列表(Blob)“可以容纳无限数据,但是字节数组只能通过int值进行索引,所以它对int的长度有限制(实际上由于某种原因intmax-8)。 –