czupe
czupe

Reputation: 4919

Unexpected character when downloading file client side from a servlet

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...

Upvotes: 1

Views: 982

Answers (1)

Joni
Joni

Reputation: 111239

ObjectOutputStream is used for serializing "arbitrary" Java objects into a binary data format that is easy to read from another Java program using ObjectInputStream. 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 the response'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.

Upvotes: 3

Related Questions