【问题标题】:How to convert byte array to string for all file types如何将所有文件类型的字节数组转换为字符串
【发布时间】: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 出于某种原因)。

标签: java arrays byte tostring


【解决方案1】:

我建议以二进制形式编写 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);
    }
}

【讨论】:

  • 嗨,彼得,谢谢。这是最简单的解决方案,它适用于 pdf。我希望它适用于所有其他类型的文件,例如 .docx 、 .xls 等。如果我错了,请纠正我。
  • 这是我的代码:我删除了上面显示的 BufferedWriter 和 OutputStreaWriter 类: outputFile = new File(strLocalFolder + strFilename); fos = new FileOutputStream(outputFile); fos.write(myData);
  • @Albert 如果您使用new File(String, String),则不必担心路径分隔符。
猜你喜欢
  • 2017-05-08
  • 2022-01-23
  • 2011-10-04
  • 2013-05-08
  • 2015-08-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多