使用Jquery和servlet检索图像会产生HTTP状态500错误

时间:2013-02-08 16:34:02

标签: java json exception web servlet-3.0

我需要从我的数据库中检索图像。为此,我使用jquery和servlet来检索存储在表中的所有图像。但是,当我运行代码时,它生成HTTP Status 500 - class oracle.jdbc.driver.OracleBlobInputStream declares multiple JSON fields named maxPosition我是Jquery的新手,我不知道如何使用JSON进行图像。

我的Servlet是:

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文件是:

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;

}
}

Stack跟踪是:

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)

我不知道这个错误告诉了什么。请有人帮我解决这个问题......谢谢......

1 个答案:

答案 0 :(得分:1)

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

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,而不是application/json