java - 尝试将图像从 jsp 发送到 servlet,然后再发送到 DB2 数据库
问题描述
我正在使用连接池将图像从 jsp 发送到 servlet,然后再发送到 DB2。我的表单 enctype 是 multipart/form-data。
Servlet 中的代码:protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("**IN SaveNoteContent POST**");
int id = Integer.parseInt(request.getParameter("ID"));
System.out.println("Note ID: " + id);
String noteText = request.getParameter("noteText");
System.out.println(noteText);
Part filePart = request.getPart("file");
String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString();
System.out.println(filePart);
System.out.println("FileName: " + fileName);
InputStream fileContent = filePart.getInputStream();
ConnectionPool pool = ConnectionPool.getInstance("jdbc/cmmill26");
connection = pool.getConnection();
cn = new CreateNote(connection);
cn.add_content(noteText,fileContent,id);
}
单独的java类。
public void add_content(String body, InputStream image, int id) throws IOException {
System.out.println("Beginning of adding note content...");
String query = "INSERT INTO note_content(body, image, audio, note_id_content) VALUES (?,?,?,?)";
try
{
System.out.println(image);
PreparedStatement ps;
ps = dbConnection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
ps.setString(1,body);
ps.setBinaryStream(2,image);
// I was going to attempt to also send audio file after successfully uploading the image to blob.
ps.setBinaryStream(3, image);
ps.setInt(4,id);
ps.executeUpdate();
}
catch (SQLException e)
{
System.out.println(e.getMessage());
} // end try catch
}
我试过 setBlob 和 setBinaryStream 收到这个错误:java.lang.AbstractMethodError: Method com/ibm/db2/jcc/am/zo.setBinaryStream(ILjava/io/InputStream;)V is abstract at com.ibm.db2.jcc .am.zo.setBinaryStream(未知来源)
解决方案
推荐阅读
- javascript - 顺序拼接数组中的值
- laravel - 如何在 laravel 中进行多重过滤查询?
- spring-boot - 在 OpenApi 3 中展平 RequestParam 对象
- android - 搜索栏的自定义进度可绘制在棒棒糖设备中不起作用
- javascript - 加载处理程序上的谷歌地图标记?
- c - 添加功能后的奇怪问题
- promise - Promises 是否有计算表达式构建器?
- android - Android:检测触摸事件何时完成
- google-cloud-firestore - 使用 firestore 配置 Swagger openAPI 3.0 身份验证时出现问题
- arangodb-foxx - ArangoDB 微服务依赖于其他微服务