我正在将照片上传到oracle数据库。我的AddPhotoServlet类的代码如下。
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class AddPhotoServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
// Apache Commons-Fileupload library classes
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload sfu = new ServletFileUpload(factory);
/*if (! ServletFileUpload.isMultipartContent(request)) {
System.out.println("sorry. No file uploaded");
return;
}*/
// parse request
List items = sfu.parseRequest(request);
FileItem id = (FileItem) items.get(0);
String photoid = id.getString();
FileItem title = (FileItem) items.get(1);
String phototitle = title.getString();
// get uploaded file
FileItem file = (FileItem) items.get(2);
// Connect to Oracle
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con =DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "cloud", "cloud");
con.setAutoCommit(false);
PreparedStatement ps = con.prepareStatement("insert into photos values(?,?,?)");
ps.setString(1, photoid);
ps.setString(2, phototitle);
// size must be converted to int otherwise it results in error
ps.setBinaryStream(3, file.getInputStream(), (int) file.getSize());
ps.executeUpdate();
con.commit();
con.close();
out.println("Proto Added Successfully. <p> <a href='listphotos'>List Photos </a>");
}
当我在上传时在服务器中运行html页面时会抛出错误: ORA-01460:请求未实现或不合理的转换。 有人可以帮我这个吗?
我创建了我的表格:
create table photos
(
id number(5) primary key,
title varchar2(50),
photo blob
);
答案 0 :(得分:0)
查看oracle jdbc开发人员使用streams和lobs的指南。关于如何处理具有不同性能/内存含义的数据有很多选择。 第二个doc(on lobs)的一部分通过使用Connection.createBlob()并将内容写入该对象然后在语句中设置该blob对象来涵盖临时lob对象的使用。