GWT App Engine文件下载问题

时间:2013-09-16 06:56:45

标签: google-app-engine gwt servlets

代码将一个字符串发送到DownloadServlet,然后创建一个可以下载的文本文件。 它在本地服务器上运行良好,但是当我将其部署到App Engine时,不会显示要下载的文件。

这是客户端代码:

public class SaveDialog extends DialogBox {
public SaveDialog(final Turing turing) {

    final FormPanel formPanel = new FormPanel();
    formPanel.setEncoding(FormPanel.ENCODING_URLENCODED);
    formPanel.setMethod(FormPanel.METHOD_POST);
    formPanel.setAction("downloadServlet");
    setWidget(formPanel);
    formPanel.setSize("100%", "100%");

    AbsolutePanel absolutePanel = new AbsolutePanel();
    formPanel.setWidget(absolutePanel);
    absolutePanel.setSize("245px", "82px");

    Grid grid = new Grid(1, 2);
    absolutePanel.add(grid, 0, 0);
    grid.setSize("240px", "40px");

    Label lblFilename = new Label("Filename:");
    grid.setWidget(0, 0, lblFilename);

    final TextBox txtbxTuring = new TextBox();
    txtbxTuring.setText(turing.getDocumentName());
    grid.setWidget(0, 1, txtbxTuring);

    Button btnCancel = new Button("Cancel");
    absolutePanel.add(btnCancel, 62, 46);

    Button btnSave = new Button("Save");
    absolutePanel.add(btnSave, 10, 46);

    final Hidden hidden = new Hidden("Hidden name");
    hidden.setID("hidden");
    absolutePanel.add(hidden);
    hidden.setName("hidden");

    this.setText("Save As...");
    this.setTitle(this.getText());
    this.setAnimationEnabled(true);
    this.setGlassEnabled(true);


    btnSave.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
            hidden.setValue(txtbxTuring.getText().replace("\n", "")  + "." + Turing.FILE_EXTENSION + "\n" + turing.getTuringMachine().getGraphXML());
            turing.setDocumentName(txtbxTuring.getText());
            formPanel.submit();
            hide();             
        }
    });

    btnCancel.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
            hide();
        }
    });     

    formPanel.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {

        @Override
        public void onSubmitComplete(SubmitCompleteEvent event) {
            System.out.println("formPanel.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() { " + event.getResults());
            hide();
        }

    }); 

    txtbxTuring.addValueChangeHandler(new ValueChangeHandler<String>() {
        @Override
        public void onValueChange(ValueChangeEvent<String> event) {

            if(txtbxTuring.getText().length() < 1) {
                txtbxTuring.setText("turing");
            } else {
                hidden.setValue(txtbxTuring.getText().replace("\n", "") + "\n" + turing.getTuringMachine().getGraphXML());
            }

        }
    });
}

}

这是服务器代码:

public class DownloadServlet extends HttpServlet {

/**
 * 
 */
private static final long serialVersionUID = 1L;

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

    String before = req.getReader().readLine();

    if(before == null) {
        resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "No data received!");
        return;
    }

    String decoded = URLDecoder.decode(before, "UTF-8");

    String filename = "";
    String data = "";

    {
        int i = 0;
        StringTokenizer tokenizer = new StringTokenizer(decoded, "\n");

         while (tokenizer.hasMoreTokens()) {
             if(i == 0) {
                 filename += tokenizer.nextToken();
             } else {
                 data += tokenizer.nextToken();
             }
             i++;
        }
    }

    filename = filename.replace("hidden=", "").replace("\n", "").replace("\r", "");

    try {           
        ServletOutputStream out = resp.getOutputStream();
        ServletContext context = getServletConfig().getServletContext();

        String mimetype = context.getMimeType(filename);
        byte[] bytes = (data == null) ? "".getBytes() : data.getBytes();

        resp.setContentType((mimetype != null) ? mimetype : "application/octet-stream");

        if (mimetype == null) {
            mimetype = "application/octet-stream";
        }


        resp.setContentType(mimetype);          
        resp.setContentLength((int) bytes.length);
        resp.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
        out.write(bytes);
        out.flush();
        out.close();
    }
    catch (Exception e) {
        e.printStackTrace();
    }       
}

}

0 个答案:

没有答案