Ouverture d'une popup pour ouvrir/enregistrer un fichier depuis une servlet
Bonjour !
j'ai un problème pour ouvrir un popup d'ouverture/enregistrement de ficher dans ma servlet.
ce que je fais :
1) depuis une jsp, je fais appel à une servlet en utilisant AJAX :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| function dataExport()
{
var ajaxRequest4 = new Ajax.Request("Export_data", {
method : 'post',
asynchronous : true,
onSuccess : function() {
alert('Success');
},
onFailure : function() {
alert('Something went wrong...');
}
});
return false;
} |
2) une fois dans ma servlet , je créer un fichier excel que je remplis et enregistre dans un répertoire c:/Temp
3) je veux pouvoir ouvrir via un pop ouvrir/enregistrer ce fichier mais rien ne se passe à l'écran et aucune exception n'est levée.
voici le code de ma servlet :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
| public class Export_data extends HttpServlet
{
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Export_data()
{
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
try
{
//création nom de fichier
String FileName = "Export_DELAI_";
String format = "ddMMyyHmmss";
java.text.SimpleDateFormat formater = new java.text.SimpleDateFormat(format);
java.util.Date date = new java.util.Date();
FileName += formater.format(date);
FileName += ".xls";
// chemin du ficher.
String FilePathAndName = "C:\\Temp\\" + FileName;
FileOutputStream fileOut = new FileOutputStream(FilePathAndName);
//création éléments Excels.
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet worksheet = workbook.createSheet("POI Worksheet");
worksheet.setColumnWidth((short) 0, (short) (15 * 256));
worksheet.setColumnWidth((short) 1, (short) (14 * 256));
worksheet.setColumnWidth((short) 2, (short) (20 * 256));
worksheet.setColumnWidth((short) 3, (short) (20 * 256));
worksheet.setColumnWidth((short) 4, (short) (17 * 256));
worksheet.setColumnWidth((short) 5, (short) (20 * 256));
worksheet.setColumnWidth((short) 6, (short) (20 * 256));
worksheet.setColumnWidth((short) 7, (short) (16 * 256));
worksheet.setColumnWidth((short) 8, (short) (14 * 256));
worksheet.setColumnWidth((short) 9, (short) (17 * 256));
// création de la première ligne
HSSFRow initialRow = worksheet.createRow((short) 0);
HSSFCell initialCell = null;
HSSFCellStyle EnTeteCellStyle = workbook.createCellStyle();
HSSFFont font = workbook.createFont();
font.setFontName("Trebuchet MS");
font.setColor(HSSFColor.WHITE.index);
HSSFPalette palette = workbook.getCustomPalette();
palette.setColorAtIndex(HSSFColor.RED.index, (byte) 163, (byte) 2, (byte) 52);
EnTeteCellStyle.setFont(font);
EnTeteCellStyle.setFillForegroundColor(HSSFColor.RED.index);
EnTeteCellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
EnTeteCellStyle.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
EnTeteCellStyle.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);
EnTeteCellStyle.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);
EnTeteCellStyle.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
// initialCell.setCellStyle(EnTeteCellStyle);
//récupération des noms de colonnes
DBConnection conn = new DBConnection();
ResultSet dataTab = conn.getDataExport();
ResultSetMetaData rsmd = dataTab.getMetaData();
int columnCount = rsmd.getColumnCount();
for (int i = 1; i < columnCount + 1; i++)
{
String name = rsmd.getColumnName(i);
initialCell = initialRow.createCell((short) i);
initialCell.setCellValue(new HSSFRichTextString(rsmd.getColumnName(i)));
initialCell.setCellStyle(EnTeteCellStyle);
}
// insertion des données dans le documents excel.
HSSFCellStyle DataCellStyle = workbook.createCellStyle();
HSSFFont Datafont = workbook.createFont();
Datafont.setFontName("Trebuchet MS");
DataCellStyle.setFont(Datafont);
DataCellStyle.setFillForegroundColor(HSSFColor.WHITE.index);
DataCellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
DataCellStyle.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
DataCellStyle.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);
DataCellStyle.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);
DataCellStyle.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
int nbreRow = 1;
while (dataTab.next())
{
nbreRow++;
HSSFRow currentrow = worksheet.createRow((short) nbreRow);
HSSFCell currentCell = null;
for (int j = 1; j <= columnCount; j++)
{
System.out.print("\n\t valeur :" + dataTab.getString(j));
currentCell = currentrow.createCell((short) j);
currentCell.setCellStyle(DataCellStyle);
currentCell.setCellValue(new HSSFRichTextString(dataTab.getString(j)));
}
}
workbook.write(fileOut);
fileOut.flush();
fileOut.close();
//Ouverture du fichier dans une popup.
File myfile = new File(FilePathAndName);
response.setContentType("text/xls");
response.setHeader("Content-Disposition", "attachment; filename=" + FileName);
InputStream in = new FileInputStream(myfile);
ServletOutputStream outs = response.getOutputStream();
try
{
int bit = in.read();
while ((bit) >= 0)
{
outs.write(bit);
bit = in.read();
}
}
catch (Exception e)
{
e.printStackTrace(System.out);
}
outs.flush();
outs.close();
in.close();
conn.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (SQLException e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
}
} |
quelqu'un aurait une idée ?