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 7875125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:55:50+00:00 2026-06-03T02:55:50+00:00

I wrote a servlet (with .groovy extension) which should return xml in Base64 encoding

  • 0

I wrote a servlet (with .groovy extension) which should return xml in Base64 encoding

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    String fileName=(String) request.getParameter("fileName")

    if (fileName == null || fileName.equals(""))throw new ServletException("Invalid or non-existent file parameter in SendXml servlet.")
    if (fileName.indexOf(".xml") == -1)fileName = fileName + ".xml"

    System.out.println(fileName)

    try {

      String relativeWebPath = "/WEB-INF/classes/com/abc/csm/xml/"+fileName
      String absoluteDiskPath = getServletContext().getRealPath(relativeWebPath)

      String fileContents=new File(absoluteDiskPath).text
      response.setContentType("text/xml")
      response.addHeader("Content-Disposition", "attachment filename="+ fileName)

      XmlHandler xm=new XmlHandler()

      PrintWriter out = response.getWriter()
      String enxml=xm.encodeBase64(fileContents)
      response.setContentLength((int) enxml.length)

      out.println(enxml)
      out.close()
      out.flush()
    } catch (Exception e) {       println e     } 
}

XmlHandler encodeBase64 method

def encodeBase64(String text) {
    return new String(Base64.encodeBase64(text.getBytes()))
}

But I guess something is missing. Please help

Update

Also please comment is it proper way to access files from package?

  • 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-06-03T02:55:51+00:00Added an answer on June 3, 2026 at 2:55 am

    You can replace:

      String enxml=xm.encodeBase64(fileContents)
    

    with

      String enxml = fileContents.bytes.encodeBase64()
    

    Or, better to pass an encoding to the call to getBytes:

      String enxml = fileContents.getBytes( 'UTF-8' ).encodeBase64()
    

    Also, I believe you’re missing a semicolon after attachment in your header… The line should read:

      response.addHeader( "Content-Disposition", "attachment; filename=$fileName" )
    

    Edit, and encoding and decoding example:

    Encode:

    String encoded = "tim_yates".getBytes( 'UTF-8' ).encodeBase64()
    

    Decode:

    String original = new String( encoded.decodeBase64(), 'UTF-8' )
    assert original == 'tim_yates'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i wrote a little test programm which queries the status servlet of a tomcat
I wrote custom servlet in Liferay and want to know which user page calls
I wrote a sample html which retrieves the JSON from servlet and displays. But
I should write servlet that receive File from request. I want to store this
I try to implement a servlet which should be called either through POST or
I wrote some code to read a file in my Java Servlet class. (I'm
I need to write a servlet that will return to the user a csv
I'm trying to write a custom servlet (for AJAX/JSON) in which I would like
I wrote servlet that writes an image to the root directory of my app
I wrote a RESTful servlet, and the UI developer wants to save the logged-in

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.