Java 如何使用blob objcet将servlet文件上传到数据库中
我想上传一些文件,把我的网站扔进数据库。在上传页面中,我使用了如下字段:Java 如何使用blob objcet将servlet文件上传到数据库中,java,servlets,file-upload,Java,Servlets,File Upload,我想上传一些文件,把我的网站扔进数据库。在上传页面中,我使用了如下字段: <form method = "post" action="addResumeSvl"> <input type = "file" name="file1"> <input type = "file" name="file2"> </form> 但是在addResumeSvl中,我如何区分file1和file2,比如request.getParameter(
<form method = "post" action="addResumeSvl">
<input type = "file" name="file1">
<input type = "file" name="file2">
</form>
但是在addResumeSvl中,我如何区分file1和file2,比如
request.getParameter()
方法,然后将它们放入带有blob对象的数据库中,它们必须是multipart
<form action="addResumeSvl" method="post" enctype="multipart/form-data">
现在要存储在DB中,请使用下面的代码段
InputStream inputStream = null; // input stream of the upload file
// obtains the upload file part in this multipart request
Part filePart = request.getPart("file1");
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();
}
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
conn = DriverManager.getConnection(dbURL, dbUser, dbPass);
// constructs SQL statement
String sql = "INSERT INTO tablename(columnname) values (?)";
PreparedStatement statement = conn.prepareStatement(sql);
if (inputStream != null) {
// fetches input stream of the upload file for the blob column
statement.setBlob(1, inputStream);
}
// sends the statement to the database server
int row = statement.executeUpdate();
if (row > 0) {
message = "File uploaded and saved into database";
}
提交请求之前,请确保将
enctype=“multipart/form data
作为属性添加到
标记中