0

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);
Albert
  • 21
  • 3
  • 8
  • 1
    try to open the same pdf file with a normal text editor and you will see the same result. So your operation for conversion was successful but not the logic. For each file type after converting the bytes you should use the appropriate parser – strash Apr 20 '17 at 09:36
  • that thread may be of help: http://stackoverflow.com/questions/5449903/advanced-pdf-parser-for-java – strash Apr 20 '17 at 09:37
  • A "Binary list of bytes (Blob)" can hold unlimited datas but a byte-array can be indexed only via int-values, so it has a limitation of the length of an int (in practice intmax-8 for some reason). – Grim Apr 20 '17 at 09:44

1 Answers1

2

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);
    }
}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Hi Peter, Thanks man. It was easiest solution and it worked for pdf. I hope it will work for all other type files such as .docx , .xls etc. Correct me if I am wrong. – Albert Apr 21 '17 at 02:01
  • This is my code: I have removed the BufferedWriter and OutputStreaWriter class shown above : outputFile = new File(strLocalFolder + strFilename); fos = new FileOutputStream(outputFile); fos.write(myData); – Albert Apr 21 '17 at 02:01
  • @Albert if you use the `new File(String, String)` you don't have to worry about the path separator. – Peter Lawrey Apr 22 '17 at 07:23