java - 不能转换为 java.sql.Blob
问题描述
小服务程序
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String firstname = request.getParameter("Firstname");
String lastname = request.getParameter("Lastname");
String gender = request.getParameter("gender");
String dob = request.getParameter("dob");
String email = request.getParameter("email");
String experience = request.getParameter("experience");
String password = request.getParameter("pwd");
String confirmpassword = request.getParameter("cpwd");
String address = request.getParameter("address");
String mobilenumber = request.getParameter("mobile");
Part file = request.getPart("fileUpload");
inputStream = file.getInputStream();
CandidatePojo pojo = new CandidatePojo(firstname, lastname, gender,dob,email,experience,password,confirmpassword,address,mobilenumber,file);
CandidateRegistrationDao dao = new CandidateRegistrationDao();
try {
pojo = dao.insertdata(pojo);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
response.sendRedirect("index.jsp");
}
道
public CandidatePojo insertdata(CandidatePojo pojo) throws SQLException { String sql = "INSERT INTO Candidateregist (firstname, lastname, gender,dob,email,experience,pwd,cpwd,address,mobile,resume) VALUES (?, ?, ? ,? , ? ,? ,? , ? ,? ,?, ?)"; 连接();
PreparedStatement statement = jdbcConnection.prepareStatement(sql);
statement.setString(1, pojo.getFirstname());
statement.setString(2, pojo.getLastname() );
statement.setString(3, pojo.getGender());
statement.setString(4, pojo.getDob());
statement.setString(5, pojo.getEmail());
statement.setString(6, pojo.getExperience());
statement.setString(7, pojo.getPassword());
statement.setString(8, pojo.getConfirmpassword());
statement.setString(9, pojo.getAddress() );
statement.setString(10, pojo.getMobilenumber());
statement.setBlob(11, (Blob) pojo.getFile());
int rowsInserted = statement.executeUpdate();
if (rowsInserted > 0) {
System.out.println("A new record was inserted successfully!");
}
//boolean rowInserted = statement.executeUpdate() > 0;
statement.close();
disconnect();
return pojo;
}
波乔
公共类候选人Pojo {
private String firstname;
private String lastname;
private String gender;
private String dob;
private String email;
private String experience;
private String password;
private String confirmpassword;
private String address;
private String mobilenumber;
private Part file;
public CandidatePojo(String firstname, String lastname, String gender, String dob, String email, String experience,
String password, String confirmpassword, String address, String mobilenumber,Part file) {
this.firstname = firstname;
this.lastname = lastname;
this.gender = gender;
this.dob = dob;
this.email = email;
this.experience = experience;
this.password = password;
this.confirmpassword = confirmpassword;
this.address = address;
this.mobilenumber = mobilenumber;
this.file = file;
}
嗨...我无法将 Blob 投射到我的编码中,请有人在此代码中找到什么错误..
在控制台中出现这样的错误
java.lang.ClassCastException:org.apache.catalina.core.ApplicationPart 在registration.servelt.RegistrationController.doGet(RegistrationController.Dao.CandidateRegistrationDao.insertdata(CandidateRegistrationDao.java:62) 处无法转换为java.sql.Blob。 java:75) 在 registration.servelt.RegistrationController.doPost(RegistrationController.java:48) 在 javax.servlet.http.HttpServlet.service(HttpServlet.java:660) 在 javax.servlet.http.HttpServlet.service(HttpServlet.java :741)
解决方案
问题是您要通过直接转换直接将a 转换org.apache.catalina.core.ApplicationPart
为 a Blob
:
statement.setBlob(11, (Blob) pojo.getFile());
问题是Part
不能以Blob
这种方式将 a 强制转换为 a。
解决此问题的一种方法是将 the 转换Part
为 anInputStream
并将其转换为 a ,这是由SerialBlobbyte[]
理解的,它可用作 Blob - 有点长的路线。
使用Commons IO。将以下内容添加到 pojo 以将 Part 转换为byte[]
:
public byte[] getFileAsByteA() {
try {
return IOUtils.toByteArray(file.getInputStream());
} catch (IOException ex) {
return null;
}
}
将以下内容添加到插入代码中:
javax.sql.rowset.serial.SerialBlob sb = new SerialBlob(pojo.getFileAsByteA());
statement.setBlob(11, sb);
注意:代码只是拼凑在一起。
推荐阅读
- javascript - 如何在 konva 画布上同步这些图像?
- swift - 如何访问参与者隔离的属性?
- python - django 应用程序如何与用户/客户端本地设备文件系统交互?
- android - 有条件的 Android Room DB 迁移
- java - 调整行高没有按预期工作
- api - 如何在 Flutter 中创建 Web API?
- javascript - 使用 SheetJS 将动态创建的表格内容保存在 excel 文件中
- python-typing - 如何使用一组特定的值在 Python 中输入
- javascript - Firebase 功能不是实时的(?)
- python - XGBoost:提供分类类型时,DMatrix 参数 `enable_categorical` 必须设置为 `True`