0

I got a problem when files were download , I can't get any actions or events when clicking any links , butttons and menues after download process was done.

Below is my codes for excel file download button ...

    Button btnDownloadExcel = new Button("Excel Download");
    btnDownloadExcel.addStyleName("downloadButton");
    btnDownloadExcel.addClickListener(new ClickListener() {

        @Override
        public void buttonClick(final ClickEvent event) {
            StringBuilder url = new StringBuilder("/myproject/filedownload.html?category=excel");
            url.append("&seq=" + 111);
            getUI().getPage().open(url.toString(), "_self");
        }
    });

Below is servlet for handle excel file download request (I used JExcel API for excel file)

@WebServlet(value = "/filedownload.html")
public class DownloadServletController extends HttpServlet {
private final Logger log = LoggerFactory.getLogger(DownloadServletController.class);

protected final void doGet(final HttpServletRequest request,
        final HttpServletResponse response) throws ServletException, IOException {
    String category = request.getParameter("category");
    long seq = request.getParameter("seq") == null ? -1L : Long.parseLong(request.getParameter("seq"));
    byte[] stream = null;
    if (category.equals("excel")) {
        try {
            stream = getSampleExcelStream(seq);
        }
        catch (BusinessException e) {
            log.error("Generating streams for " + category + " got Error !" + e);
        }
        ExcelSupport.createExcel("Test", seq, stream, response);
    }
}

private byte[] getSampleExcelStream(final long seq) throws BusinessException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        String staticDir = System.getProperty("staticDir");
        String templateDir = staticDir + "/templates/sample_excel_template.xls";

        WorkbookSettings wsWrite = new WorkbookSettings();
        wsWrite.setEncoding("UTF-8");
        wsWrite.setAutoFilterDisabled(false);
        WritableWorkbook workBook = Workbook.createWorkbook(baos, Workbook.getWorkbook(new File(templateDir)),
                wsWrite);

        workBook.write();
        baos.close();
        workBook.close();
    }
    catch (BiffException e) {
        throw new BusinessException("Excel file Creating Error!");
    }
    catch (WriteException e) {
        throw new BusinessException("Error ! writing excel file process has occured!");
    }
    catch (FileNotFoundException e) {
        throw new BusinessException("FileNotFoundException, when getting stream for excel", e);
    }
    catch (IOException e) {
        throw new BusinessException("IOException, when getting stream for excel", e);
    }
    return baos.toByteArray();
}
}

ExcelSupport.java is below

public final class ExcelSupport {
private ExcelSupport() {
}

private static final Logger LOGGER = LoggerFactory.getLogger(ExcelSupport.class);

public static void createExcel(final String fileNamePrefix, final long seq,
        final byte[] stream, final HttpServletResponse response) {
    StringBuffer fileName = new StringBuffer();
    fileName.append(fileNamePrefix + "_");
    if (seq > -1) {
        fileName.append("(uid-" + seq + ")_");
    }
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    fileName.append(sdf.format(new Date()));
    fileName.append(".xls");

    StringBuffer sbContentDispValue = new StringBuffer();
    sbContentDispValue.append("inline");
    sbContentDispValue.append("; filename=");
    sbContentDispValue.append(fileName);

    response.setContentType("application/msexcel");
    response.addHeader("Cache-Control", "max-age=30");
    response.addHeader("Content-disposition", sbContentDispValue.toString());
    response.setContentLength(stream.length);
    try {
        ServletOutputStream osStream = response.getOutputStream();
        osStream.write(stream);
        osStream.flush();
        osStream.close();
    }
    catch (IOException e) {
        LOGGER.error("Creating Excel for " + fileName + " got Error !" + e);
    }
}
}

Can somebody correct me what I am wrong ? Download process was fine , nothing error and I got excel file successfully. But I have no idea why browser was freeze. I can't see any error logs or messages in IDE console and browser's console. Thanks for reading my question !

PS : I am sure this codes work fine and did not freeze on other GWT projects.

Burkhard
  • 14,596
  • 22
  • 87
  • 108
Cataclysm
  • 7,592
  • 21
  • 74
  • 123
  • Which browser does freeze? Perhaps try with other browsers just to be sure it's a server issue – André Schild Aug 29 '14 at 06:13
  • @AndréSchild I tested in ff 26 , ff 31 , chrome , safari. They are same , I guess this is due to Vaadin ,but I am not sure. – Cataclysm Aug 29 '14 at 07:20
  • What do you mean by "freeze" ? Do you have to forcefully kill the webbrowser? At which point does it freeze? You can look at the network traffic in the console of FF for example. (Does the request go to the server, does a answer come back from server etc.) – André Schild Aug 29 '14 at 09:49
  • @AndréSchild **freeze** that I mean *.. I can't do anythings or I didn't get any actions , events.* – Cataclysm Aug 29 '14 at 11:25

1 Answers1

0

Now I found the problem . I used Network console of Firefox 31 and here is screen-shoot for before download and here is after download. I notice that I lost all web datas because the replacing url by getUI().getPage().open(url.toString(), "_self");

So , if I use others instead of _self , everythings were fine but browsers were block popups. I can't tell the users to enable popups of their browsers . So , finally I use Link component as below ..

    Link linkDownloadExcel = new Link("Excel Download", new ExternalResource(
        "/myproject/filedownload.html?category=excel&seq=" + 111), "_blank", -1, -1, BorderStyle.DEFAULT);
    linkDownloadExcel.addStyleName("downloadButton");
    linkDownloadExcel.setIcon(new ExternalResource("/myproject/images/excel-icon.png"));
    hlButtonLayout.addComponent(linkDownloadExcel);
Cataclysm
  • 7,592
  • 21
  • 74
  • 123
  • 1
    using `open()` for downloads is discouraged and newer vaadin version have parts of this api even deprecated to prevent the user from shooting himself in the foot. https://vaadin.com/book/-/page/advanced.printing.html#advanced.printing.pdf is an example, how to handle downloads in case, you can not link directly. – cfrick Aug 29 '14 at 13:44