检索使用图像Jquery的和servlet产生HTTP状态500错误(Retrieving imag

2019-07-19 15:53发布

我需要从我的数据库检索图像。 为此,我使用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;

}
}

和堆栈跟踪:

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)

我不知道这是什么错误告诉。 请任何人能帮助我解决这个......谢谢.....

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



文章来源: Retrieving images using Jquery and servlet produces HTTP Status 500 error