参数不能大于BLOB的长度

时间:2015-03-29 12:02:30

标签: mysql image jsp jdbc

我在尝试将blob图像显示到jsp页面时收到错误(" pos" +"长度"参数不能大于BLOB的长度)。我使用了两个不同的代码进行上传和检索。

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();*/

}
%> 

0 个答案:

没有答案