problem when downloading excel generated from java

0

I have a web application created in java I use the Apache library poi to generate an excel of a query, when I run the application from the netbens the excel file is perfectly generated, but when I deploy the war in the tomcat apache to execute it in the server and try to generate excel does not work. What can happen?

the java version that I am using is java7 the version of the apache poi 3.14

This is the class that executes the process

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package modelo;

import Controlador.Informe;
import java.awt.Desktop;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Array;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

/**
 *
 * @author hp
 */
public class Informes extends conexion {

    List<Informe> listaf = new ArrayList<Informe>();

    public boolean aprovecha(String ini, String fin) throws SQLException, IOException {
        Statement st = con.createStatement();
        ResultSet rs = null;
        String nit;
        int numRecibo;
        String inir = ini.replace("-", "");
        String finr = fin.replace("-", "");

        PreparedStatement consulta = con.prepareStatement("select DISTINCT nit_fuente from fuentes where activo = 'SI'");

        try {
            rs = consulta.executeQuery();
            String rutaArchivo = System.getProperty("user.home") + "/Downloads/Suscriptores Aforados.xls";
            File archivoXLS = new File(rutaArchivo);
            if (archivoXLS.exists()) {
                archivoXLS.delete();
            }
            archivoXLS.createNewFile();
            Workbook libro = new HSSFWorkbook();
            FileOutputStream archivo = new FileOutputStream(archivoXLS);
            Sheet hoja = libro.createSheet("Hoja 1");
            int f = 0;
            while (rs.next()) {
                nit = rs.getString("nit_fuente");
                ResultSet rsmate = null;

                PreparedStatement consultarecibo = con.prepareStatement("select clasematerial,tipoMaterial,nit_asociado,sum(cantidad) as total "
                        + "from ingresomaterial "
                        + "LEFT JOIN recibo on ingresomaterial.numRecibo = recibo.num_recibo "
                        + "WHERE nit_fuente ='"+nit+"' and FECHa BETWEEN '"+inir+"' and '"+finr+"' "
                        + "GROUP BY tipoMaterial ");
                rsmate = consultarecibo.executeQuery();
                System.out.println("modelo.Informes.aprovecha()" + consultarecibo);
                while (rsmate.next()) {

                    Informe lista = new Informe(nit, rsmate.getString("clasematerial"), rsmate.getString("tipoMaterial"),
                            rsmate.getString("total"), rsmate.getString("nit_asociado"));

                    listaf.add(lista);
                }

            }

            consruta(inir,finr);

            for (int x = 0; x < listaf.size(); x++) {
                //  System.out.println(listaf.get(x));
                Row fila = hoja.createRow(x);
                for (int c = 0; c < 5;) {
                    /*Creamos la celda a partir de la fila actual*/
                    Cell celda = fila.createCell(c);

                    if (x == 0) {
                        switch (c) {
                            case 0:
                                celda.setCellValue("Nit Fuente");
                                break;
                            case 1:
                                celda.setCellValue("Nit Asosicado");
                                break;
                            case 2:
                                celda.setCellValue("Clase");
                                break;
                            case 3:
                                celda.setCellValue("Total");
                                break;
                            case 4:
                                celda.setCellValue("Total Toneladas ");

                                break;
                        }
                    } else {
                        /*Si no es la primera fila establecemos un valor*/

                        switch (c) {
                            case 0:
                                celda.setCellValue(listaf.get(x).getNit());
                                break;
                            case 1:
                                celda.setCellValue(listaf.get(x).getNitSocio());
                                break;
                            case 2:
                                celda.setCellValue(listaf.get(x).getClase());
                                break;
                            case 3:
                                celda.setCellValue(listaf.get(x).getTipo());
                                break;
                            case 4:
                                double total = Double.parseDouble(listaf.get(x).getTotal());

                                celda.setCellValue(total);

                                break;
                        }

                    }
                    c++;
                }

            }

            libro.write(archivo);
            archivo.close();
         //   Desktop.getDesktop().open(archivoXLS);

            System.out.println("lista ");
        } catch (Exception e) {
            System.err.println("error " + e);
            return false;
        }
        con.close();

        return true;
    }

    public void consruta(String inir, String finr) throws SQLException {

        Statement st = con.createStatement();
        ResultSet rs = null;
        PreparedStatement consulta = con.prepareStatement("select DISTINCT Cod_ruta from rutas");
        try {
        rs = consulta.executeQuery();

        while (rs.next()) {
            String ruta = rs.getString("Cod_ruta");
            ResultSet rsmate = null;

                PreparedStatement consultarecibo = con.prepareStatement("select clasematerial,tipoMaterial,nit_asociado,sum(cantidad) as total "
                        + "from ingresomaterial "
                        + "LEFT JOIN recibo on ingresomaterial.numRecibo = recibo.num_recibo "
                        + "WHERE nit_fuente ='"+ruta+"' and FECHa BETWEEN '"+inir+"' and '"+finr+"' "
                        + "GROUP BY tipoMaterial ");
            rsmate = consultarecibo.executeQuery();
            System.out.println("modelo.Informes.aprovecha()" + consultarecibo);
            while (rsmate.next()) {

                Informe lista = new Informe(ruta, rsmate.getString("clasematerial"), rsmate.getString("tipoMaterial"),
                        rsmate.getString("total"), rsmate.getString("nit_asociado"));

                listaf.add(lista);
            }

        }

         } catch (Exception e) {
        }
    }
}
    
asked by Jeferson Martinez 08.03.2017 в 06:46
source

2 answers

1

It is very likely to be a problem with directory or permissions routes:

String rutaArchivo = System.getProperty("user.home") + "/Downloads/Suscriptores Aforados.xls";

This route may exist and you may have write permissions in your development environment, but it is likely that this is not the case on the server.

Therefore, check:

  • The existence of the directory path taking into account that "user.home" which may vary according to the user who runs the web application.
  • That said user has write permissions in directory directory.

Apart from that, I strongly recommend that you do not use spaces neither in the names of directories nor in the file names, they can only bring you problems.

    
answered by 08.03.2017 в 11:07
0

The solutions placing an absolute path independently if it is disk C or D place

user= System.getProperty("user.name");
new File("/Users/"+user+"/Downloads/Certificaciones/Certificiación" + nombre + ".PDF")
    
answered by 17.03.2017 в 18:01