1

I want to send json from servlet..The servlet is getting data and multiple images from mysql database..now i want to put this data in json and send it to user..but when i run this SendJsonData.java it gives output like this..how to get Image..

where mysql user table has col--name,city,image1.image2....

[{"name":"rohit","city":"mumbai","image1":""com.mysql.jdbc.Blob@1b6d1381","image2":""com.mysql.jdbc.Blob@1b6d45881"}{...}{..}]

servlet: SendJsondata.java

package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONArray;
import org.json.JSONObject;

public class SendJsonData extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {


        PrintWriter out=response.getWriter();
        response.setContentType("application/json;charset=UTF-8");
        response.setHeader("Cache-Control", "no-cache");

        Connection conn=null;
        String dbPath = "";
        String username = "";
        String password = "";

        try{
            String name=null;
            String city=null;
            Blob image1;
            Blob image2

            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(dbPath, username, password);

           Statement st = conn.createStatement();
           ResultSet rs = st.executeQuery("select name,city,image1,image2 from user LIMIT 10");
           JSONArray jArray = new JSONArray();
           JSONObject json=new JSONObject();
           while(rs.next()){

                name=rs.getString("name");
                city=rs.getString("city");
                image1=rs.getBlob("image1");
                image2=rs.getBlob("image2");

                json.put("name", name);
                json.put("city", city);
                json.put("image1", image1);
                json.put("image2", image2);

                jArray.put(json);

           }
       out.println(jArray);
            conn.close();                            
        }

        catch(Exception e){            
            out.print(e);            
        }            
    }    
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
R K
  • 35
  • 1
  • 5
  • 1
    Did you consider converting your image to Base64 with further decoding on the remote side? – Anton Hlinisty Dec 12 '18 at 12:47
  • 1
    Take the byte[] from the blob using getBytes(...) https://docs.oracle.com/javase/7/docs/api/java/sql/Blob.html#getBytes(long,%20int) – Jens Dec 12 '18 at 12:54
  • Get the Byte[] out of the blob and then Base64Encode it, convert to string and send in the response. Base64Encoding is an added step we are recommending here, but check if you really want it. https://stackoverflow.com/questions/2418485/how-do-i-convert-a-byte-array-to-base64-in-java – A_C Dec 12 '18 at 12:59
  • imgData = rs.getBytes("image1"); String encode = Base64.getEncoder().encodeToString(imgData); using this I have encoded the image and now how to decode it using javascript / jquery..please give solution on this.. – R K Dec 12 '18 at 15:25
  • @RK this is a completely different question - which also has answers here on SO... Although, if it is js in a browser that has to process the json, you may want to send the textual and image data separately. In this latter case you can send the image data in binary format as opposed to base64 encoding you have to use with json. – Shadow Dec 12 '18 at 16:15

0 Answers0