我目前正在尝试创建一个 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);