在 RemoteServiceServlet 中使用内容处置附件
我有一个应用程序,它成功接收来自用户的输入字符串,在服务器端处理它,并将结果显示在网页上。我将其实现为 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;
}
}
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"
}
});
I have an application that successfully receives an input string from the user, processes it on server-side, and displays the result on a webpage. I had implemented it as a RemoteServiceServlet, because this way I can handle all the website gadgets easily.
Nevertheless I have decided to, instead of displaying the result on a webpage, use the "content-disposition attachment" possibilities so that the user can download the processed string in a txt file.
Is there a way to do this without changing the whole application from RemoteServiceServlet to HttpServlet?
Below my code. Thanks a million.
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: Client side
[...]
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"
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要更改为另一个 servlet,而是考虑使用其中一个 - RPC 用作传输的 XmlHttpRequest 不能用于下载文件,但对于几乎所有对服务器的请求仍然非常方便。 XHR 仅适用于从 javascript 到服务器的通信,不能用于下载(或其他操作,例如打开包含内容的新窗口)。
考虑创建另一个 servlet,并让 RPC 调用返回一个字符串、另一个 servlet 的 url(可能还加上一些其他参数来指示应下载的内容)。
Instead of changing to another servlet, consider using one of each - the XmlHttpRequest that RPC uses as transport can't be used to download a file, but is still very handy for almost all of your requests to the server. XHRs are only good for communicating from javascript to the server, and can't be used for downloads (or other things, such as opening new windows with content).
Consider making another servlet, and having the RPC call return a string, a url for that other servlet (plus probably some other parameters to indicate what should be downloaded).