如何将所有文件类型的字节数组转换为字符串
How to convert byte array to string for all file types
目前我可以从数据库中获取 BLOB 数据类型的数据。
所以在代码中它会被存储在byte[]中。
现在我可以通过如下提及字符集 (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 上
我无法查看内容。
我可以知道如何在不指定字符集的情况下将所有文件类型(.pdf、.xls 等)的字节数组 (myData) 转换为字符串吗?
以下是字节数组[]如何传入方法。
file_content 是来自 table 的 BLOB 数据类型。
String contentString = rsStatementRequest.getValue("file_content");
BASE64Decoder en = new BASE64Decoder();
byte[] contentBytes = en.decodeBuffer(contentString);
contentBytes = mtdFileEncryption(contentBytes,fileName);
我建议用二进制写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);
}
}
目前我可以从数据库中获取 BLOB 数据类型的数据。 所以在代码中它会被存储在byte[]中。 现在我可以通过如下提及字符集 (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 上 我无法查看内容。 我可以知道如何在不指定字符集的情况下将所有文件类型(.pdf、.xls 等)的字节数组 (myData) 转换为字符串吗?
以下是字节数组[]如何传入方法。 file_content 是来自 table 的 BLOB 数据类型。
String contentString = rsStatementRequest.getValue("file_content");
BASE64Decoder en = new BASE64Decoder();
byte[] contentBytes = en.decodeBuffer(contentString);
contentBytes = mtdFileEncryption(contentBytes,fileName);
我建议用二进制写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);
}
}