使用 Jquery 和 servlet 检索图像会产生 HTTP 状态 500 错误 [英] Retrieving images using Jquery and servlet produces HTTP Status 500 error

查看:30
本文介绍了使用 Jquery 和 servlet 检索图像会产生 HTTP 状态 500 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从我的数据库中检索图像.为此,我使用 jquery 和 servlet 来检索存储在表中的所有图像.但是当我运行代码时它会产生 HTTP Status 500 - class oracle.jdbc.driver.OracleBlobInputStream 声明多个名为 maxPosition 的 JSON 字段我是 Jquery 的新手我不知道如何使用 JSON图片.

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.

我的 Servlet 是:

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;

    }   

ImageFileInfo.java 文件是:

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;

}
}

而堆栈跟踪是:

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.....

推荐答案

你在你的类中包含了两个 InputStream 变量,它们被设置为 OracleBlobInputStream 的实例,其中您的 GSON 提供程序无法序列化.您可能希望将图像内容存储为字节(或作为(URL 编码的)字符串).

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"));

从侧面看,您似乎正在尝试返回 JSON 内容,但您错误地将 Content-Type 指定为 text/html,而不是 <代码>应用程序/json.

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.

这篇关于使用 Jquery 和 servlet 检索图像会产生 HTTP 状态 500 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆