Download csv file in JSP

0

In an onclick event, I am trying to download a csv file with FileWriter, but download to a specific path that I provide

How should I get the downloaded file to be downloaded automatically and shown in the browser?

The code is as follows:

private static final String COMMA_DELIMITER = " ,";

private static final String NEW_LINE_SEPARATOR = "\n";

private static final String FILE_HEADER = "Banca,Par de Divisa,Segmento,"+
   "Segmento-Compra,Segmento-Venta,Zona,Zona-Compra,Zona-Venta,Canal,"+
   "Canal-Compra,Canal-Venta,Monto,"+
   "Monto-Compra,Monto-Venta,Total Compra,Total Venta";

String fileName = System.getProperty("user.home")+"/registros.csv";


        FileWriter fileWriter = null;

        try {
            fileWriter = new FileWriter(fileName);

            fileWriter.append(FILE_HEADER.toString());

            fileWriter.append(NEW_LINE_SEPARATOR);

            for (InfoMatrixSpreads info : listmatrix) {

                fileWriter.append(String.valueOf(info.getBanking()));
                fileWriter.append(COMMA_DELIMITER);
                fileWriter.append(String.valueOf(info.getCurrencyPar()));
                fileWriter.append(COMMA_DELIMITER);
                fileWriter.append(String.valueOf(info.getSegment()));
                fileWriter.append(COMMA_DELIMITER);
                fileWriter.append(String.valueOf(info.getSegmentBuySpread()));
                fileWriter.append(COMMA_DELIMITER);
                fileWriter.append(String.valueOf(info.getSegmentSaleSpread()));
                fileWriter.append(COMMA_DELIMITER);
                fileWriter.append(String.valueOf(info.getZone()));
                fileWriter.append(COMMA_DELIMITER);
                fileWriter.append(String.valueOf(info.getZoneBuySpread()));
                fileWriter.append(COMMA_DELIMITER);
                fileWriter.append(String.valueOf(info.getZoneSaleSpread()));
                fileWriter.append(COMMA_DELIMITER);
                fileWriter.append(String.valueOf(info.getChannel()));
                fileWriter.append(COMMA_DELIMITER);
                fileWriter.append(String.valueOf(info.getChannelBuySpread()));
                fileWriter.append(COMMA_DELIMITER);
                fileWriter.append(String.valueOf(info.getChannelSaleSpread()));
                fileWriter.append(COMMA_DELIMITER);
                fileWriter.append(String.valueOf(info.getAmount()));
                fileWriter.append(COMMA_DELIMITER);
                fileWriter.append(String.valueOf(info.getAmountBuySpread()));
                fileWriter.append(COMMA_DELIMITER);
                fileWriter.append(String.valueOf(info.getAmountSaleSpread()));
                fileWriter.append(COMMA_DELIMITER);
                fileWriter.append(String.valueOf(info.getTotalBuy()));
                fileWriter.append(COMMA_DELIMITER);
                fileWriter.append(String.valueOf(info.getTotalSale()));
                fileWriter.append(NEW_LINE_SEPARATOR);
            }

            System.out.println("CSV file was created successfully !!!");

        } catch (Exception e) {
            System.out.println("Error in CsvFileWriter !!!");
            e.printStackTrace();
        } finally {

            try {
                fileWriter.flush();
                fileWriter.close();
            } catch (IOException e) {
                System.out.println("Error while flushing/closing fileWriter !!!");
                e.printStackTrace();
            }

        }
    
asked by Paula Marín 07.02.2018 в 15:57
source

1 answer

1

What you need is to get the bytes of your String that you fill when you go through the list when you have the bytes you send it to write it, that does it in the variable called resultado so you could download the file I leave an example is similar to yours only without the list you run.

File file = new 
File("C:\temp\downloadfilename.csv");
FileInputStream fileIn = new FileInputStream(file);
 ServletOutputStream out = 
 response.getOutputStream();

 byte[] outputByte = new byte[4096];
 //copy binary contect to output stream
 while(fileIn.read(outputByte, 0, 4096) != -1)
 {
out.write(outputByte, 0, 4096);
 }
 fileIn.close();
 out.flush();
  out.close();
    
answered by 07.02.2018 / 20:08
source