[英]Google App Engine opens excel file in the browser instead of downloading - Vaadin
我目前正在嘗試創建一個Web應用程序。 我的任務很簡單,我正在動態創建一個excel文件,並為客戶端創建一個下載此文件的鏈接。 我正在使用vaadin 6.8.9和谷歌應用程序引擎。 當我在本地測試代碼時,一切正常,我可以下載文件但是當我將代碼部署到谷歌應用程序引擎時,它會嘗試在瀏覽器中打開excel文件。 請幫我修理一下。 我嘗試了所有可能的解決方案,但它沒有奏效。
或者任何人都可以告訴我如何在不是應用程序類的vaadin類中監聽HttpServletRequestListener。 並設置其Content-disposition並編寫文件。
這是已部署代碼的鏈接, http://vaadingo.appspot.com/
這是我的代碼,
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
WritableWorkbook workBook;
try {
workBook = Workbook.createWorkbook(outputStream);
WritableSheet sheet = workBook.createSheet("User List", 0);
// Generates Headers Cells
WritableFont headerFont = new WritableFont(WritableFont.TAHOMA, 12, WritableFont.BOLD);
WritableCellFormat headerCellFormat = new WritableCellFormat(headerFont);
try {
headerCellFormat.setBackground(Colour.PALE_BLUE);
} catch (WriteException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
sheet.addCell(new jxl.write.Label(1, 1, "LastName", headerCellFormat));
sheet.addCell(new jxl.write.Label(2, 1, "FirstName", headerCellFormat));
WritableFont dataFont = new WritableFont(WritableFont.TAHOMA, 12);
WritableCellFormat dataCellFormat = new WritableCellFormat(dataFont);
sheet.addCell(new jxl.write.Label(1, 2, "last", dataCellFormat));
sheet.addCell(new jxl.write.Label(2, 2, "first", dataCellFormat));
try {
workBook.write();
workBook.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (RowsExceededException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
final ByteArrayInputStream in = new ByteArrayInputStream(outputStream.toByteArray());
StreamSource source = new StreamSource() {
public InputStream getStream() {
// TODO Auto-generated method stub
return in;
}
};
String namefile = "deneme.xls";
StreamResource resource = new StreamResource(source, namefile, getApplication());
resource.getStream().setParameter("Content-Disposition", "attachment; filename=\"" + namefile + "\"");
resource.setMIMEType("application/vnd.ms-excel");
resource.setCacheTime(-1);
Link link = new Link();
link.setCaption("dosya");
link.setResource(resource);
link.setTargetName("_blank");
mainLayout.addComponent(link);
嘗試在回復中添加內容處置標頭:
Content-Disposition: attachment
也許這不是答案,但你可以在瀏覽器中設置一些mimes應該打開或下載 - 它也決定了會發生什么。
檢查HTTP響應。 1.(Ctrl + Shift + J)使用chrome - > inspect - > network 2.下載文件3.使用chrome - > inspect - > network - >文件名 - > headers / response(如果沒有設置,請查看設置的標題是什么解決它或向GAE報告錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.