【发布时间】:2017-04-20 09:33:59
【问题描述】:
目前我可以从数据库中获取 BLOB 数据类型的数据。 所以在代码中它将存储在字节[]中。 现在我可以通过提及字符集(例如 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 我无法查看内容。 我可以知道如何在不指定字符集的情况下将所有文件类型(.pdf、.xls 等)的字节数组(myData)转换为字符串吗?
以下是字节数组[] 是如何传递到方法中的。 file_content 是表中的 BLOB 数据类型。
String contentString = rsStatementRequest.getValue("file_content");
BASE64Decoder en = new BASE64Decoder();
byte[] contentBytes = en.decodeBuffer(contentString);
contentBytes = mtdFileEncryption(contentBytes,fileName);
【问题讨论】:
-
尝试用普通的文本编辑器打开同一个pdf文件,你会看到同样的结果。所以你的转换操作是成功的,但不是逻辑。对于转换字节后的每种文件类型,您应该使用适当的解析器
-
该线程可能会有所帮助:stackoverflow.com/questions/5449903/…
-
“二进制字节列表(Blob)”可以容纳无限的数据,但字节数组只能通过 int 值进行索引,因此它具有 int 长度的限制(实际上intmax-8 出于某种原因)。