Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7538107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:01:58+00:00 2026-05-30T07:01:58+00:00

I create a servlet to download a specific text which the client post with

  • 0

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…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T07:01:59+00:00Added an answer on May 30, 2026 at 7:01 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to Java and have been asked to create an applet/servlet (not sure
I'm trying to create a servlet which loads FirstJSP.jsp in WEB-INF/jsp/FirstJSP.jsp I'm having a
I am pretty new to jruby and java and want to create a servlet
Is there a way to create the Servlet (page display - form) and process
I am developing an web application which can upload/download a file from client to
I am trying to create a servlet which can obtain the parameter of the
I created one GWT webapplication project.Inthat i want to create servlet program,but in that
I want to create a JSP page or servlet that will work in 2
Create a flat text file in c++ around 50 - 100 MB with the
Create a file called Valid[File].txt and stick some text in it. Start powershell and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.