我有一个应用程序成功地从用户接收输入字符串,在服务器端处理它,并在网页上显示结果。我将它实现为 RemoteServiceServlet,因为这样我可以轻松处理所有网站小工具。
尽管如此,我还是决定,而不是在网页上显示结果,而是使用“内容处置附件”的可能性,以便用户可以将处理后的字符串下载到 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;
}
}
项目服务.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"
}
});