Arguments can not be larger than the BLOB's le

2019-09-05 01:04发布

问题:

I get an error ("pos" + "length" arguments can not be larger than the BLOB's length) while trying to display an blob image into jsp page. I used two different codes for uploading and retrieving.

upload.jsp

<%@ page import="java.sql.*"%>

<%@ page import="java.io.*"%>

<%

Connection con=null;

ResultSet rs=null;

PreparedStatement psmt=null;

FileInputStream fis;

String url="jdbc:mysql://localhost/logindb";

try{

Class.forName("com.mysql.jdbc.Driver").newInstance();

con=DriverManager.getConnection("jdbc:mysql://localhost:3306/logindb","root","");

File image=new File("F:/logo.png");

psmt=con.prepareStatement("insert into inimage(name,city,image)"+"values(?,?,?)");

psmt.setString(1,"Barack Obama");

psmt.setString(2,"Wasington D.C.");

fis=new FileInputStream(image);

psmt.setBinaryStream(3, (InputStream)fis, (int)(image.length()));

int s = psmt.executeUpdate();

if(s>0) {

%>

<b><font color="Blue"> <% out.println("Image Uploaded successfully !"); %>

</font></b>

<%

}

else {

out.println("unsucessfull to upload image.");

}

con.close();

psmt.close();

}catch(Exception ex){

out.println("Error in connection : "+ex);

}

%>

retrieveimage.jsp

<%@ page import="java.sql.*"%>

<%@ page import="java.io.*"%>

<% Blob image = null;

Connection con = null;

byte[ ] imgData = null ;

Statement stmt = null;

ResultSet rs = null;

try {

Class.forName("com.mysql.jdbc.Driver");

con = DriverManager.getConnection("jdbc:mysql://localhost/logindb","root","");

stmt = con.createStatement();

rs = stmt.executeQuery("select * from inimage");

if (rs.next()) {

image = rs.getBlob(3);

imgData = image.getBytes(3,(int)image.length());

} else {

out.println("Display Blob Example");

out.println("image not found for given id>");

return;

}

// display the image

response.setContentType("image/gif");

OutputStream o = response.getOutputStream();

o.write(imgData);

o.flush();

o.close();

} catch (Exception e) {

out.println("Unable To Display image");

out.println("Image Display Error=" + e.getMessage());

return;

}finally {

/*try {

//rs.close();

//stmt.close();

//con.close();

} catch (SQLException e) {

e.printStackTrace();*/

}
%>