2

我正在开发一个应用程序,我想通过调用服务从应用程序创建一个“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);               
            }
        });

这是客户端代码

4

2 回答 2

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();
                                } 

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

于 2013-08-21T05:38:25.083 回答
1

使用 FileUpload 获取路径。

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

在 changeEvent 中,您可以获得文件名/路径:

FileUpload file = (FileUpload) event.getSource();
String filename = file.getFilename();
于 2013-08-19T06:00:49.660 回答