Pregunta

Esta parte de mi código fue la creación de archivos xls exitosamente

FileOutputStream fileOut = new FileOutputStream("c:\\Decrypted.xls");
wb.write(fileOut);
fileOut.close();

cuando la otra parte del código tenía esta declaración (que era antes de que el código anterior)

in = new ByteArrayInputStream(theCell_00.getBytes(""));

Pero cuando lo cambié a

in = new ByteArrayInputStream(theCell_00.getBytes("UTF-8"));

esta parte

FileOutputStream fileOut = new FileOutputStream("c:\\Decrypted.xls");
wb.write(fileOut);
fileOut.close();

No se genera ningún archivo xls más.

Tengo que cambiar la codificación UTF-8 como lo he hecho en la línea ByteArrayInputStream, así que lo que debería hacer que el código todavía genera un archivo xls.

En el caso de que lo necesite, las dos partes se toman de esta función.

public void getExcel() throws Exception {

    try {
        ByteArrayInputStream in = null;
        FileOutputStream out = null;

        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("new sheet");

        /*
         * KeyGenerator kgen = KeyGenerator.getInstance("AES"); kgen.init(128); SecretKey key =
         * kgen.generateKey(); byte[] encoded = key.getEncoded();
         * 
         * IOUtils.write(encoded, new FileOutputStream(new
         * File("C:\\Users\\abc\\Desktop\\key.txt")));
         */

        FileInputStream fin = new FileInputStream("C:\\key.txt");
        DataInputStream din = new DataInputStream(fin);

        byte b[] = new byte[16];

        din.read(b);

        InputStream excelResource = new FileInputStream(path);
        Workbook rwb = Workbook.getWorkbook(excelResource);
        int sheetCount = rwb.getNumberOfSheets();
        Sheet rs = rwb.getSheet(0);
        int rows = rs.getRows();
        int cols = rs.getColumns();
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < Col.length; j++) {
                String theCell_00 = rs.getCell(j, i).getContents();
                System.out.println("the Cell Content : " + theCell_00);

                in = new ByteArrayInputStream(theCell_00.getBytes(""));
                out = new FileOutputStream("c:\\Decrypted.txt");

                try {

                    // System.out.println(b);
                    SecretKey key1 = new SecretKeySpec(b, "AES");
                    // Create encrypter/decrypter class
                    AESDecrypter encrypter = new AESDecrypter(key1);

                    encrypter.encrypt(new ByteArrayInputStream(theCell_00.getBytes()),
                        new FileOutputStream("temp.txt"));
                    // Decrypt
                    // encrypter.encrypt(,new FileOutputStream("Encrypted.txt"));

                    encrypter.decrypt(in, out);

                    try {
                        if (out != null)
                            out.close();
                    } finally {
                        if (in != null)
                            in.close();
                    }

                    // encrypter.decrypt(new
                    // ByteArrayInputStream(theCell_00.getBytes(Latin_1)),new
                    // FileOutputStream("c:\\Decrypted.txt"));
                    String filename = "c:\\Decrypted.txt";

                    BufferedReader bufferedReader = null;

                    try {

                        // Construct the BufferedReader object
                        bufferedReader = new BufferedReader(new FileReader(filename));
                        // System.out.println(bufferedReader.readLine());
                        String line = null;

                        while ((line = bufferedReader.readLine()) != null) {
                            // Process the data, here we just print it out

                            /*
                             * HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet =
                             * wb.createSheet("new sheet"); HSSFRow row = sheet.createRow(2);
                             */
                            // System.out.println(i);

                            HSSFRow row = sheet.createRow(i);
                            int s_col = 0;
                            row.createCell(s_col).setCellValue(line);
                            // s_col++;
                            // row.createCell(1).setCellValue(new Date());
                            FileOutputStream fileOut = new FileOutputStream("c:\\Decrypted.xls");
                            wb.write(fileOut);
                            fileOut.close();

                            // System.out.println(line);
                        }

                    } catch (FileNotFoundException ex) {
                        ex.printStackTrace();
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    } finally {
                        // Close the BufferedReader
                        try {
                            if (bufferedReader != null)
                                bufferedReader.close();
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }
        rwb.close();
    } catch (Exception ex) {
        ex.printStackTrace();
        ex.getMessage();
    }
}
¿Fue útil?

Solución

¿Qué tipos de datos se espera por la llamada a AESDecrypter.decrypt? ¿Tiene que tomar en un objeto FileOutputStream? O se puede pasar en un escritor u otro OutputStream?

Yo suelo hacer algo como esto para escribir UTF-8 de salida:

Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("c:\\Decrypted.txt"), "UTF-8"));
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top