0

ユーザーから入力文字列を正常に受信し、サーバー側で処理して、結果をWebページに表示するアプリケーションがあります。これをRemoteServiceServletとして実装しました。これにより、すべてのWebサイトガジェットを簡単に処理できるようになりました。

それでも、結果をWebページに表示する代わりに、「コンテンツ処理添付ファイル」の可能性を使用して、ユーザーが処理された文字列をtxtファイルにダウンロードできるようにすることにしました。

アプリケーション全体をRemoteServiceServletからHttpServletに変更せずにこれを行う方法はありますか?

私のコードの下。どうもありがとう。

ProjectServiceImpl.java

public class ProjectServiceImpl extends RemoteServiceServlet implements ProjectService 
{
    public String project(String input) throws IllegalArgumentException 
    {
        String output = processString(input);
        // Below something I tried to do, but it does not work at all
        try {
            HttpServletResponse resp = getThreadLocalResponse();
        resp.reset();
        resp.setContentType("application/octet-stream");
        resp.setContentLength(10);
        resp.setHeader("Content-disposition", "attachment; filename=\"test.txt\"");
        ServletOutputStream op = resp.getOutputStream();
        op.write(convertToByteArray(output),0,10);
        op.flush();
        op.close();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        return output;
    }
}

ProjectService.java

public interface ProjectService extends RemoteService {
    String project(String name) throws IllegalArgumentException;
}

ProjectServiceAsync.java

public interface ProjectServiceAsync {
    void project(String input, AsyncCallback<String> callback)
            throws IllegalArgumentException;
}

MyProject.java:クライアント側

[...]
projectService.project(originalString, new AsyncCallback<String>() {
    [...]
    public void onSuccess(final String result) 
    {
        [...] // Or perhaps should I create here in client-side the txt file with "result"
    }
});
4

1 に答える 1

0

別のサーブレットに変更する代わりに、それぞれを使用することを検討してください。RPCがトランスポートとして使用するXmlHttpRequestを使用してファイルをダウンロードすることはできませんが、サーバーへのほとんどすべてのリクエストには非常に便利です。XHRは、JavaScriptからサーバーへの通信にのみ有効であり、ダウンロード(または、コンテンツを含む新しいウィンドウを開くなどの他の目的)には使用できません。

別のサーブレットを作成し、RPC呼び出しで文字列、他のサーブレットのURL(および、ダウンロードする必要があるものを示す他のパラメーター)を返すようにすることを検討してください。

于 2012-01-07T18:15:03.023 に答える