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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:14:07+00:00 2026-06-16T02:14:07+00:00

I am using html,ajax and struts 2 to show image on UI. I am

  • 0

I am using html,ajax and struts 2 to show image on UI. I am returning response as a byte[] of image from action and when I attach it with the image source then it shows some garbled values.

ajax call I am making from script is

$.ajax({  
    type: "POST",  
    url:url,  
    contentType: "image/png",  
    success: function(data){  
        $('.logo').html('<img src="data:image/png;base64,' + data + '" />');  
    }
} );

and action from where I am returning array of image byte is like this

public void execute(ActionInvocation invocation) throws Exception {  
    HttpServletResponse response = ServletActionContext.getResponse();  
    response.setContentType(action.getCustomContentType());
    response.getOutputStream().write(action.getCustomImageInBytes());
}

public byte[] getCustomImageInBytes() { 
    System.out.println("imageId" + imageId); 
    BufferedImage originalImage;
    try {
      originalImage = ImageIO.read(getImageFile("C:\\temp\\Desert.jpg"));
      // convert BufferedImage to byte array
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      ImageIO.write(originalImage, "png", baos);
      baos.flush();
      imageInByte = baos.toByteArray();
      baos.close();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } 
    return imageInByte;
  }
  • 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-16T02:14:09+00:00Added an answer on June 16, 2026 at 2:14 am

    I’ve recreated your problem. It does appear to be the base64 encoding, although it works fine in the eclipse local preview without.

    Use these two lines instead of response.getOutpuStream().write(…)

    String encoded = javax.xml.bind.DatatypeConverter
                    .printBase64Binary(action.getCustomImageInBytes());
    response.getOutputStream().print(encoded);
    

    My full solution:

    HTML

    <!DOCTYPE html>
    <html>
    <head>
    <title>Dynamic image test - stackoverflow issue 13946908</title>
    <script type="text/javascript" src="jquery-1.8.3.min.js"></script>
    </head>
    <script>
      $(document).ready(function() {
        $.ajax({
          type : "GET",
          url : "/Test/ImageServer",
          contentType : "image/png",
          success : function(data) {
            $('.logo').html('<img src="data:image/png;base64,' + data + '" />');
          }
        });
      });
    </script>
    <body>
      <div class="logo"></div>
    </body>
    </html>
    

    Servlet

    public class ImageServer extends HttpServlet {
    
        protected void doGet(HttpServletRequest request,
                HttpServletResponse response) throws ServletException, IOException {
            response.setContentType("image/jpeg");
            byte[] data = getCustomImageInBytes(request.getServletContext()
                    .getResource("/horse.jpg"));
            String encoded = DatatypeConverter.printBase64Binary(data);
            response.getOutputStream().print(encoded);
        }
    
        private byte[] getCustomImageInBytes(URL url) throws IOException {
            BufferedImage originalImage = ImageIO.read(url);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(originalImage, "jpg", baos);
            baos.flush();
            byte[] imageInByte = baos.toByteArray();
            baos.close();
            return imageInByte;
        }
    }
    

    Tested

    • Chrome Version 23.0.1271.97 OSX 10.7.5
    • Firefox 16.0.2 OSX 10.7.5
    • Safari 6.0.2 OSX 10.7.5
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to load an html document using ajax and then perform a
I'm using ajax to get some data then based on the data use html()
I want to retrieve HTML and Javascript from the same page using AJAX calls.
How to send looping data from html page to php page using ajax. I
Code: <% using (Ajax.BeginForm(GetResourcesByProject, CreateRequest, new AjaxOptions { UpdateTargetId = ResourceListDiv})) { Response.Write(Html.DropDownList(SelectProject, Model.ProjectList,
I'm trying to load up the estimated world population from http://www.census.gov/ipc/www/popclockworld.html using AJAX, and
Fires the action when I'm using Html.BeginForm but not Ajax.BeginForm. Anyone know why?
I'm loading from DB some html (using ajax post request), it looks like: <div
I'm Using AJAX to dynamically fetch data from my php page (which contains html)
I'm trying to make a login page using html, ajax & ASP.NET.The data is

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.