【发布时间】:2015-02-18 14:03:55
【问题描述】:
我正在尝试将图像作为 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();
}
}
}
【问题讨论】: