上传图像时,输入不存储在数据库中

时间:2015-10-22 12:52:00

标签: java mysql jsp java-ee servlets

我正在尝试将数据和图像路径插入数据库。但只有图像名称和路径存储在数据库中,其余列为空 能帮帮我吗

下面的

是我的servlet,是jsp和servlet的新手

    protected void ProcessRequest(HttpServletRequest request,HttpServletResponse response)
        throws ServletException, IOException {
        throws ServletException, IOException {
    String Title = request.getParameter("title");
    String Keyword = request.getParameter("key");
    String Message = request.getParameter("mess");


            if(ServletFileUpload.isMultipartContent(request)){

                try {

                    List<FileItem> multiparts = new ServletFileUpload(new   DiskFileItemFactory()).parseRequest(request);



                    for(FileItem item : multiparts){

                        if(!item.isFormField()){

                            String name = new File(item.getName()).getName();

                            item.write( new File(UPLOAD_DIRECTORY + File.separator + name));
                            String Path = "Upload/" + name;

                            Connection con =  connection.useConnection();
                           // Statement st = con.createStatement();
                           // String q = "insert into image_details(Title,Keyword,Description,ImageName,ImagePath) values('" + Title + "','" + Keyword + "','" + Message + "','" + name + "','" + Path + "')";
                           // System.out.println(q);
                           // st.executeUpdate(q);
                           String Query =  "insert into image_details(Title,Keyword,Description,ImageName,ImagePath) values (?,?,?,?,?)";                              
                           PreparedStatement ps = con.prepareStatement(Query);
                           ps.setString(1, Title);
                           ps.setString(2, Keyword);
                           ps.setString(3, Message);
                           ps.setString(4, name);
                           ps.setString(5, Path);
                           ps.executeUpdate();
                           System.out.println(ps);

                        }

                    }                                                     
                   //File uploaded successfully

                   //request.setAttribute("message", "File Uploaded Successfully");

                } catch (Exception ex) {

                   request.setAttribute("message", "File Upload Failed due to " + ex);

                } 




           }
           // response.sendRedirect("Admin_FileUpload.jsp?msg=1");
            request.getRequestDispatcher("/Admin_FileUpload.jsp").forward(request, response);


}

@Override
 protected void doGet(HttpServletRequest request,HttpServletResponse response)
        throws ServletException, IOException {
       ProcessRequest(request,response);
 }
 @Override
 protected void doPost(HttpServletRequest request,HttpServletResponse response)
        throws ServletException, IOException {
       ProcessRequest(request,response);
 }

}

这是我的JSP页面

  <form action="ImageUpload" method="post" enctype="multipart/form-data">
  <h3 style="font-family:Raleway,sans-serif;">UPLOAD YOUR CONTENTS AND IMAGES</h3>
  <hr/>
  <div class="form-group">
    <h4 style="font-family:Raleway,sans-serif">Title:</h4>
    <input type="text" class="form-control" name="title" placeholder="Enter the Title" required="">
  </div>
  <div class="form-group">
    <h4 style="font-family:Raleway,sans-serif">Keyword:</h4>
  <input type="text" class="form-control" required="" placeholder="Enter the Keyword" name="keyword">
  </div>
  <div class="form-group">
    <h4 style="font-family:Raleway,sans-serif">Description:</h4>
    <textarea class="form-control" name="mess" required=""  rows="5" cols="50"></textarea>
  </div>
   <div class="form-group">
    <h4 style="font-family:Raleway,sans-serif">Image Upload:</h4>
   <input type="file" name="Image" style="font-family:Raleway,sans-serif;font-size:17px" class="btn btn-default" required="" >
   </div>
   <div class="form-group">
   <input type="submit" class="btn btn-success" name="Upload" value="Upload" style="width:150px;height:45px">
   </div>
   </form>

1 个答案:

答案 0 :(得分:0)

如文档中所述

  

以String形式返回请求参数的值,如果是,则返回null   参数不存在   http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getParameter(java.lang.String)

您需要检查您的请求对象