我需要从数据库中检索图像。为此,我使用了jquery和servlet来检索存储在表中的所有图像。但是,当我运行代码产生的代码时,HTTP Status 500 - class oracle.jdbc.driver.OracleBlobInputStream declares multiple JSON fields named maxPosition我是Jquery中的新手,我不知道如何为图像使用JSON。
HTTP Status 500 - class oracle.jdbc.driver.OracleBlobInputStream declares multiple JSON fields named maxPosition
我的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)
我不知道该错误说明了什么。请任何人帮我解决这个问题......谢谢.....
您InputStream在类中包括了两个变量,这些变量被设置为的实例OracleBlobInputStream,您的GSON提供程序无法实例化这些实例。您可能希望将图像内容存储为字节(或作为(URL编码的)字符串)。
InputStream
OracleBlobInputStream
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-Typeas text/html而不是指定为application/json。
Content-Type
text/html
application/json