我需要使用JSP将Blob文件上传到MySQL数据库。我能够做到这一点,但我无法存储实际文件的名称。这非常重要,因为下一步将是上传文件的列表。这是我到目前为止所得到的:
@MultipartConfig
public class FileUploadServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
DBConnection DBC = new DBConnection();
// this is the database connection method
Connection con = DBC.connection();
InputStream inputFile = null;
Part file = request.getPart("file");
if(file != null){
inputFile = file.getInputStream();
}
Date utilDate = new Date();
Date sqlDate = new java.sql.Date(utilDate.getTime());
try{
String fileUpload = "insert into uploads(file,uploaded_date) values('"+inputFile+"','"+sqlDate+"')";
Statement st = con.createStatement();
int insertFile = st.executeUpdate(fileUpload);
response.sendRedirect("ok.jsp");
}catch(SQLException e){
}
}
所以,如果我上传一个名为picture.jpg的文件,我想将picture.jpg存储在一个字符串中,而不是java.io.FileInputStream@55c3ece1这样的流中。 提前谢谢!