问题描述
我创建一个servlet来下载一个特定的文本,客户端发送一个表单之前(在textarea)...客户端表单没有指定:
form = new FormPanel();
form.setMethod(FormPanel.METHOD_POST);
form.setAction(GWT.getModuleBaseURL()+services / export);
exportButton = new Button(resource.SUBMENU_Export(),new ClickHandler(){
public void onClick(ClickEvent event){
form.submit();
}
});
这是服务器端的代码(serlvet):
package com.server.servlet;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ExportServlet extends HttpServlet implements Servlet {
private static final long serialVersionUID = 7526472295622776147L;
@Override
protected void doPost(HttpServletRequest req,HttpServletResponse response)throws ServletException,IOException {
String expl = req.getParameter(Expl);
response.setContentType(application / octet-stream);
response.setHeader(Content-Disposition,attachment; filename = hint.txt;);
OutputStream stream = response.getOutputStream();
ObjectOutputStream objectStream = new ObjectOutputStream(stream);
objectStream.writeObject(expl);
objectStream.flush();
}
}
但是当我打开下载的文件时,字符只在文件的开头:
`¬tč-`
当我将这些字符写入我的文件时我没有任何线索...
解决方案
ObjectOutputStream
用于将任意Java对象序列化为二进制数据格式,使用ObjectInputStream
。听起来像,而不是这样做,你只想输出一些文本。最简单的方法是:OutputStream stream = response.getOutputStream
stream.write(expl.getBytes(UTF-8));
如果写入的文本数据量较大,可以使用Writer:
OutputStream stream = response.getOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(stream,UTF-8);
writer.write(expl);
writer.flush(); //将编写器中的文本数据刷新到流
输出流用于发送任意二进制数据,或者可能不是你想要的。在上面的流是保持打开,所以你可以添加更多的数据后的文本。如果所有输出都是文本,您可以将内容类型设置为
text / plain
,并使用响应
作者:response.setCharacterEncoding(UTF-8);
PrintWriter writer = response.getWriter();
writer.write(expl);
请注意,编写器和流将由Web容器自动关闭,因此您不必执行它自己。
I create a servlet to download a specific text which the client post with a form before (in a textarea)... The form in the client side is nothing speciel:
form = new FormPanel(); form.setMethod(FormPanel.METHOD_POST); form.setAction(GWT.getModuleBaseURL() + "services/export"); exportButton = new Button(resource.SUBMENU_Export(), new ClickHandler() { public void onClick(ClickEvent event) { form.submit(); } });
And this is the code at the server side (serlvet):
package com.server.servlet; import java.io.IOException; import java.io.ObjectOutputStream; import java.io.OutputStream; import javax.servlet.Servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ExportServlet extends HttpServlet implements Servlet { private static final long serialVersionUID = 7526472295622776147L; @Override protected void doPost(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException { String expl = req.getParameter("Expl"); response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment; filename=hint.txt;"); OutputStream stream = response.getOutputStream(); ObjectOutputStream objectStream = new ObjectOutputStream(stream); objectStream.writeObject(expl); objectStream.flush(); } }
but when i open the downloaded file, there are unexpected character only in the beginning in the file:
"`¬í tč-`"
I have no clue when write these characters to my file...
解决方案
ObjectOutputStream
is used for serializing "arbitrary" Java objects into a binary data format that is easy to read from another Java program usingObjectInputStream
. Sounds like instead of doing that you just want to output some text. The easiest way to it is this:OutputStream stream = response.getOutputStream(); stream.write(expl.getBytes("UTF-8"));
If you had a bigger amount of text data to write you could use a Writer instead:
OutputStream stream = response.getOutputStream(); OutputStreamWriter writer = new OutputStreamWriter(stream, "UTF-8"); writer.write(expl); writer.flush(); // flush text data from writer to stream
The output stream is for sending arbitrary binary data, which may or may not be what you want. In the above the stream is left open so you can add more data after the text. If all your output is text you might as well set the content type to
text/plain
and use theresponse
's writer instead:response.setCharacterEncoding("UTF-8"); PrintWriter writer = response.getWriter(); writer.write(expl);
Note that the writers and streams are automatically closed by the web container so you don't have to do it yourself.
这篇关于从servlet下载文件客户端时出现意外字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
07-31 05:30