java使用servlet从jsp表单使用setBlob向数据库插入图像
在使用setBlob()时,我不断遇到抽象方法错误
我也在这个论坛上搜索过,但没有一个答案解决了我的问题
我正在使用:
jdk 1.7
甲骨文11g
ApacheTomcat 8.0
FileUploadDBServlet。爪哇
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
@WebServlet("/uploadServlet")
@MultipartConfig(maxFileSize = 16177215) // upload file's size up to 16MB
public class FileUploadDBServlet extends HttpServlet {
// database connection settings
String dbURL="jdbc:oracle:thin:@localhost:1521: XE";
String userId="system";
String password="moon";
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// gets values of text fields
String id= request.getParameter("i1");
String name= request.getParameter("i2");
String brand= request.getParameter("i3");
String price= request.getParameter("i4");
String desc= request.getParameter("i5");
String sid= request.getParameter("i6");
String d= request.getParameter("i7");
InputStream inputStream = null; // input stream of the upload file
// obtains the upload file part in this multipart request
Part filePart = request.getPart("i8");
if (filePart != null) {
// prints out some information for debugging
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();
}
Connection conn = null; // connection to the database
String message = null; // message will be sent back to client
try {
// connects to the database
conn= DriverManager.getConnection(dbURL, userId, password);
// constructs SQL statement
String sql = "INSERT INTO item (itemid,name,brand,price,sid,img,dt,description) values (?, ?, ?,?,?,?,?,?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1,id);
ps.setString(2,name);
ps.setString(3,brand);
ps.setString(4,price);
ps.setString(5,desc);
ps.setString(6,d);
ps.setString(7,desc);
if (inputStream != null) {
// fetches input stream of the upload file for the blob column
ps.setBlob(8, inputStream);
}
// sends the statement to the database server
int row = ps.executeUpdate();
if (row > 0) {
message = "File uploaded and saved into database";
}
} catch (SQLException ex) {
message = "ERROR: " + ex.getMessage();
ex.printStackTrace();
} finally {
if (conn != null) {
// closes the database connection
try {
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
// sets the message in request scope
request.setAttribute("Message", message);
// forwards to the message page
getServletContext().getRequestDispatcher("/Message.jsp").forward(request, response);
}
}
}
# 1 楼答案
您应该使用
setBinaryStream
方法,而不是setBlob
方法或您应该在参数中使用
Blob
类型,而不是InputStream
类型由于
一,
public void setBinaryStream(int parameterIndex, InputStream stream, int length)
二,
public void setBlob(int parameterIndex,Blob value)
如果选择第一种方法,请考虑
length
参数