你好,我是jsp编程的新手。我试图用字节字段在PostgreSQL数据库中插入图像。到目前为止,这是我的代码。
jsp代码
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Test image</h1>
<form action="Upload" method="post" enctype="multipart/form-data">
<input type="text" name="description" />
<input type="file" name="file" />
<input type="submit" />
</form>
</body>
</html>
servlet代码
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
InputStream inputStream = null;
// obtains the upload file part in this multipart request
Part filePart = request.getPart("file");
if (filePart != null) {
// debug messages
System.out.println(filePart.getName());
System.out.println(filePart.getSize());
System.out.println(filePart.getContentType());
// obtains input stream of the upload file
inputStream = filePart.getInputStream();
}
try {
Class.forName("org.postgresql.Driver");
Connection con = DriverManager.getConnection("jdbc:postgresql://localhost:8084/hgcirequestproject", "appdev2", "Hgc@dm1nD3v310p3r2k14");
PreparedStatement ps = con.prepareStatement("insert into tbl_testimage (test) values (?)");
ps.setBinaryStream(1, inputStream);
ps.executeQuery();
out.println("success");
con.close();
ps.close();
} catch (Exception e) {
System.out.println(e.toString());
}
}
}
我收到错误.setBinaryStream(int,InputStream)尚未实现。 任何帮助将不胜感激。感谢
答案 0 :(得分:0)
您必须使用the 3-argument setBinaryStream
variant来提供流内容的字节长度。
PostgreSQL有线协议要求在数据自身之前发送长度,因此PgJDBC必须知道长度。
Newer versions of PgJDBC support the 2-argument form by buffering the stream first