将映像文件插入oracle数据库会插入空值

时间:2015-02-16 11:44:24

标签: java oracle jsp

我试图将图像作为BLOB插入到oracle数据库中,我使用jsp文件上传所需的图像。提交表单后,将调用“UploadServlet”。 以下是将图像插入表中的servlet代码。 但是当我看到表值时,它只在表的列中显示“NULL”。 如果我错了,请查看我的代码和建议。谢谢

UploadExample.jsp

<body>
<form action="UploadServlet" method="post" enctype="multipart/form-data" >
    <center>
    <label id="l_file" for="upload">[File Upload]</label>
    <input type="file" placeholder="Upload file" name="uploadFile"/>
    <input type="submit" value="Upload"/>
    </center>
</form>

UploadServlet.java

@WebServlet("/UploadServlet")
@MultipartConfig(maxFileSize=16177215)
public class UploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    //check request content type
    System.out.println("Content type of request is " +  request.getContentType());

    Connection currentCon = ConnectionManager.getConnection();

    InputStream inputStream = null; // input stream of the upload file

    Part filePart = request.getPart("uploadFile");
      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();
        }

     String sql = "insert into photo values(?)";

     try {
            PreparedStatement ps = currentCon.prepareStatement(sql);
            ps.setBlob(1, inputStream);
            int row = ps.executeUpdate();
            if(row>0)
            {
                System.out.println("The photo has been uploaded successfully");
            }

    } catch (SQLException e) {
        e.printStackTrace();
    }

}

}

1 个答案:

答案 0 :(得分:0)

尝试使用此代替setBlob

ps.setBinaryStream(1, inputStream, inputStream.available());