0

コードは文字列を 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();
    }       
}

}

4

0 に答える 0