Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

189
Views
Create and download TXT file in a Java servlet

I found this code of Gourav. If I call the servlet from the browser it works but if I use a "POST" call with AJAX, it only recovers the text but does not automatically download the file..

Can anyone explain how I can do this? Thank you!

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     response.setContentType("text/plain");
     response.setHeader("Content-Disposition", "attachment; filename=\"myTxtFile.txt\"");

     StringBuilder sb = new StringBuilder();
     sb.append("Lorem ipsum dolor sit amet, consectetur elit.");
     sb.append(System.getProperty("line.separator"));
     sb.append("parturient montes, nascetur ridiculus mus.");
     sb.append(System.getProperty("line.separator"));
     sb.append("Duis magna neque, malesuada non condimentum posuere.");

     OutputStream outputStream = response.getOutputStream();

     String outputResult = sb.toString();
     outputStream.write(outputResult.getBytes());
     outputStream.flush();
     outputStream.close();

}

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!