Reputation: 21
Currently the I can get the data from the database which will be in BLOB datatype. So in code it will be stored in the byte []. For now I can convert the byte array to string by mentioning the character set (eg.UTF-8) as below:
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;
}
}
Which means the following can be converted to string and I can view the content of the .txt stored file
//WORKING FOR TXT
String str = new String(myData, "UTF-8");
bufferOutput.write(str);
But this does not happen for other file , .pdf or .xls I was not able to view the content. Can I know how to convert the byte array(myData) to string for all file types (.pdf,.xls,etc) without specifying the character set ?
The following are how the byte array[] passed into the method. The file_content is the BLOB Datatype from table.
String contentString = rsStatementRequest.getValue("file_content");
BASE64Decoder en = new BASE64Decoder();
byte[] contentBytes = en.decodeBuffer(contentString);
contentBytes = mtdFileEncryption(contentBytes,fileName);
Upvotes: 0
Views: 1959
Reputation: 533710
I suggest writing the byte[] BLOB in binary. Writing is as text is likely to corrupt it esp if it is encrypted.
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);
}
}
Upvotes: 2