如何使用GWT获取路径选择器对话框以保存文件

时间:2013-08-19 05:19:56

标签: java file gwt filechooser

我正在开发一个应用程序,我想通过调用服务从应用程序创建一个'MS-EXCEL'文件,我想将该文件作为对客户端机器的响应返回,并带有保存文件的对话框。

目前我正在编写程序中的路径。我不能使用swing我想在GWT中实现它。

     if (name.equalsIgnoreCase("DigitalInputReport")) {    
                    response.setHeader(
                                                "Content-disposition",
                                                "attachment; filename=\"DIR_"
                                                        + TimeZoneUtil.getPdfDate(fromDate) + "_"
                                                        + TimeZoneUtil.getPdfDate(toDate) + ".csv\"");

        List<ReportData> datas = new ArrayList<ReportData>();
        // Calling Service
        datas = fleetTrackingServiceImpl.getDIN2SummaryReport(vehicle,
                                    fromDate, toDate);

        FileWriter writer = new FileWriter("C:\\Windows\\Temp\\DIR_"
                                                + TimeZoneUtil.getPdfDate(fromDate) + "_"
                                                + TimeZoneUtil.getPdfDate(toDate) + ".csv");
                                        try {
                                            hashMapRunDur = datas.get(datas.size() - 1).getRunHashMap();
                                            hashMapStopDur = datas.get(datas.size() - 1)
                                                    .getStopHashMap();
                                            int runDur = 0, stopDur = 0;
                                            writer.append("From Hour");
                                            writer.append(',');
                                            writer.append("To Hour");
                                            writer.append(',');
                                            writer.append("Minutes ON");
                                            writer.append(',');
                                            writer.append("Minutes OFF");
                                            writer.append(',');
                                            writer.append('\n');
                                            writer.append("Date");
                                            writer.append(',');
                                            String dtChk = null, dtChkPrev = null;
                                            for (int i = 0; i < datas.size(); i++) {

                                                if (i == 0) {
                                                    dtChk = datas.get(i).getToDate();
                                                    writer.append(datas.get(i).getToDate());
                                                    writer.append(',');
                                                    writer.append('\n');
                                                    writer.append(datas.get(i).getRunTime());
                                                    writer.append(',');
                                                    writer.append(datas.get(i).getStopTime());
                                                    writer.append(',');
                                                    writer.append(datas.get(i).getRunDur());
                                                    writer.append(',');
                                                    writer.append(datas.get(i).getStopDur());
                                                    writer.append('\n');
                                                } else {
                                                    dtChkPrev = dtChk;
                                                    dtChk = datas.get(i).getToDate();
                                                    if (dtChkPrev.equals(dtChk)) {
                                                        writer.append(datas.get(i).getRunTime());
                                                        writer.append(',');
                                                        writer.append(datas.get(i).getStopTime());
                                                        writer.append(',');
                                                        writer.append(datas.get(i).getRunDur());
                                                        writer.append(',');
                                                        writer.append(datas.get(i).getStopDur());
                                                        writer.append('\n');
                                                    } else {
                                                        for (Map.Entry<String, String> e : hashMapRunDur
                                                                .entrySet()) {
                                                            runDur = (Integer.valueOf(e.getValue()));
                                                            stopDur = (Integer.valueOf(hashMapStopDur
                                                                    .get(e.getKey())));
                                                            break;
                                                        }
// To delete first entry
                Iterator<Map.Entry<String, String>> iterRun = hashMapRunDur
                                                                .entrySet().iterator();
                                                        while (iterRun.hasNext()) {
                                                            Map.Entry<String, String> entry = iterRun
                                                                    .next();
                                                            if (datas.get(i - 1).getToDate()
                                                                    .equalsIgnoreCase(entry.getKey())) {
                                                                iterRun.remove();
                                                            }
                                                        }
                                                        Iterator<Map.Entry<String, String>> iterStop = hashMapStopDur
                                                                .entrySet().iterator();
                                                        while (iterStop.hasNext()) {
                                                            Map.Entry<String, String> entry = iterStop
                                                                    .next();
                                                            if (datas.get(i - 1).getToDate()
                                                                    .equalsIgnoreCase(entry.getKey())) {
                                                                iterStop.remove();
                                                            }
                                                        }
                                                        writer.append(' ');
                                                        writer.append(',');
                                                        writer.append("Total ON/OFF");
                                                        writer.append(',');
                                                        writer.append(formatIntoHHMMSSWithOutDay(runDur));
                                                        writer.append(',');
                                                        writer.append(formatIntoHHMMSSWithOutDay(stopDur));
                                                        writer.append(',');
                                                        writer.append('\n');

                                                        dtChk = datas.get(i).getToDate();
                                                        writer.append("Date");
                                                        writer.append(',');
                                                        writer.append(datas.get(i).getToDate());
                                                        writer.append(',');
                                                        writer.append('\n');
                                                        writer.append(datas.get(i).getRunTime());
                                                        writer.append(',');
                                                        writer.append(datas.get(i).getStopTime());
                                                        writer.append(',');
                                                        writer.append(datas.get(i).getRunDur());
                                                        writer.append(',');
                                                        writer.append(datas.get(i).getStopDur());
                                                        writer.append('\n');
                                                    }
                                                    if (i == datas.size() - 1) {
                                                        for (Map.Entry<String, String> e : hashMapRunDur
                                                                .entrySet()) {
                                                            runDur = (Integer.valueOf(e.getValue()));
                                                            stopDur = (Integer.valueOf(hashMapStopDur
                                                                    .get(e.getKey())));
                                                            break;
                                                        }
                                                        writer.append(' ');
                                                        writer.append(',');
                                                        writer.append("Total ON/OFF");
                                                        writer.append(',');
                                                        writer.append(formatIntoHHMMSSWithOutDay(runDur));
                                                        writer.append(',');
                                                        writer.append(formatIntoHHMMSSWithOutDay(stopDur));
                                                        writer.append(',');
                                                        writer.append('\n');
                                                    }
                                                }

                                            }
                                        } catch (IOException e) {
                                            e.printStackTrace();
                                        } finally {
                                            writer.flush();
                                            writer.close();
                                        }
                }

这是Servlet编码。当我调用它时,我在这里得到输出(c:// windows / temp / DIR _...),其中包含完整内容但不在我在客户端下载的文件中(我只获得'DigitalInputReport')。请简要解释一下......

csvBtn.addSelectionListener(new SelectionListener<ButtonEvent>() {

            @Override
            public void componentSelected(ButtonEvent ce) {
                String gwt = GWT.getHostPageBaseURL();
                Window.open(
                        gwt
                                + "generateCSVServlet?name=DigitalInputReport&fromDate="
                                + fromDate + "&toDate=" + toDate + "&compName="
                                + LoginDashboardModule.companyId
                                + "&brnchName=" + LoginDashboardModule.branchId
                                + "&userName=" + LoginDashboardModule.userName
                                + "&selectedVehicle=" + selectedVehicle, null, null);               
            }
        });

这是客户端代码

2 个答案:

答案 0 :(得分:3)

File temp = new File("C:\\Windows\\Temp\\DIR_"
                                        + TimeZoneUtil.getPdfDate(fromDate) + "_"
                                        + TimeZoneUtil.getPdfDate(toDate) + ".csv");

try (BufferedReader br = new BufferedReader(new FileReader(temp)))
                                {
                                    String isCurrentLine;
                                    while ((isCurrentLine = br.readLine()) != null) {
                                        response.getOutputStream().write("\n".getBytes());
                                        byte[] b = isCurrentLine.getBytes();
                                        response.getOutputStream().write(b);
                                    }

                                } catch (IOException e) {
                                    e.printStackTrace();
                                } 

使用此代码,我可以直接从服务器端向客户端返回文件内容,并按照自己的意愿将文件保存在客户端计算机上。

答案 1 :(得分:1)

使用FileUpload获取路径。

A widget that wraps the HTML <input type='file'> element.

在changeEvent中你可以得到文件名/路径:

FileUpload file = (FileUpload) event.getSource();
String filename = file.getFilename();