Retrieving images using Jquery and servlet produces HTTP Status 500 error

非 Y 不嫁゛ 提交于 2019-12-29 02:05:26

问题


I need to retrieve images from my database. For this I used jquery and servlet to retrieve all images stored in a table. But when i run the code it produces HTTP Status 500 - class oracle.jdbc.driver.OracleBlobInputStream declares multiple JSON fields named maxPosition I'm a newbie in Jquery I don't know how to use JSON for images.

My Servlet is:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String uname;// = request.getParameter("countryCode");
        uname="shyam";
        PrintWriter out = response.getWriter();
        response.setContentType("text/html");
        response.setHeader("Cache-control", "no-cache, no-store");
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Expires", "-1");

        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST");
        response.setHeader("Access-Control-Allow-Headers", "Content-Type");
        response.setHeader("Access-Control-Max-Age", "86400");

        Gson gson = new Gson();
        JsonObject myObj = new JsonObject();

        ArrayList<ImageFileInfo> imageInfo = getInfo(uname);
        ImageFileInfo info = new ImageFileInfo();
        JsonElement imageObj = gson.toJsonTree(imageInfo);

        boolean nonNullElemExist= false;
        for (ImageFileInfo s: imageInfo) {
          if (s != null) {
             nonNullElemExist = true;
             break;
          }
        }
        if(nonNullElemExist==true){
            myObj.addProperty("success", false);
        }
        else {
            myObj.addProperty("success", true);
        }
        myObj.add("imageInfo", imageObj);
        out.println(myObj.toString());
        out.close();

}
 private ArrayList<ImageFileInfo> getInfo(String uname) {

     ArrayList<ImageFileInfo> imageFileList = new ArrayList<ImageFileInfo>();
         Connection conn = null;           
        PreparedStatement stmt = null;    

        try {     
            conn=prepareConnection();

            StringBuilder sb=new StringBuilder(1024);
            sb.append("select * from ").append(uname.trim()).append("image");
            String sql=sb.toString();

            stmt = conn.prepareStatement(sql);
            ResultSet rs = stmt.executeQuery();

            while(rs.next()){
                ImageFileInfo info = new ImageFileInfo();
                info.setName(rs.getString("imagename").trim());
                info.setDisc(rs.getString("imagedisc").trim());
                info.setImageid(rs.getInt("imageid"));
                info.setalbumid(rs.getInt("albumid"));

                byte imageData[] = rs.getBytes("imagethumb");
                String encoded = DatatypeConverter.printBase64Binary(imageData);
                info.setThumb(encoded);

                byte image1Data[] = rs.getBytes("imagethumb");
                String encoded1 = DatatypeConverter.printBase64Binary(image1Data);

                info.setFull(encoded1);
            }                                                                        

            rs.close();                                                              
            stmt.close();                                                            
            stmt = null;                                                             


            conn.close();                                                            
            conn = null;                                                  

        }                                                              
        catch(Exception e){ System.out.println( "Error --> " + displayErrorForWeb(e));;}                     

        finally {                                                      

            if (stmt != null) {                                           
                try {                                                        
                    stmt.close();                                               
                } catch (SQLException sqlex) {                               
                    // ignore -- as we can't do anything about it here          
                }                                                            

                stmt = null;                                           
            }                                                       

            if (conn != null) {                                     
                try {                                                  
                    conn.close();                                         
                } catch (SQLException sqlex) {                         
                    // ignore -- as we can't do anything about it here    
                }                                                      

                conn = null;                                           
            }                                                       
        }             

        return imageFileList;

    }   

And The ImageFileInfo.java file is:

package skypark;
import java.io.InputStream;
public class ImageFileInfo 
{
String name = null;
String disc = null;
int imageid=0;
int albumid=0;
InputStream thumbarray;
InputStream fullarray;

public void setName(String name) 
{
    this.name = name;
}
public String getName() {
    return name;
}
public void setDisc(String disc) 
{
    this.disc = disc;
}
public void setImageid(int Imageid) 
{
    this.imageid = Imageid;
}
public void setalbumid(int albumid) 
{
    this.albumid = albumid;
}
public void setThumb(InputStream inputStream) 
{
    this.thumbarray = inputStream;
}
public void setFull(InputStream binaryStream) {
    this.fullarray = binaryStream;

}
}

And Stack trace is:

java.lang.IllegalArgumentException: class oracle.jdbc.driver.OracleBlobInputStream declares multiple JSON fields named maxPosition
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:122)
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:72)
com.google.gson.Gson.getAdapter(Gson.java:353)
com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:55)
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89)
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:195)
com.google.gson.Gson.toJson(Gson.java:586)
com.google.gson.Gson.toJsonTree(Gson.java:479)
com.google.gson.Gson.toJsonTree(Gson.java:458)
skypark.RetriveIm.doGet(RetriveIm.java:66)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

I don't know what this error tells. Please anyone help me to solve this...... thanks.....


回答1:


You included two InputStream variables in your class, which are getting set to instances of OracleBlobInputStream, which your GSON provider cannot serialize. You probably want to store the image content as bytes instead (or as a (URL encoded) string).

public class ImageFileInfo implements Serializable {
   // Other class variables
   private byte[] thumbarray;
   private byte[] fullarray;

   // Constructors, Getters/Setters
}

ImageFile.setThumb(rs.getBytes("imagethumb"));
ImageFile.setFull(rs.getBytes("imagefull"));

On a side tangent, it looks like you are trying to return JSON content, but you have incorrectly specified your Content-Type as text/html, instead of application/json.



来源:https://stackoverflow.com/questions/14777159/retrieving-images-using-jquery-and-servlet-produces-http-status-500-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!