【问题标题】:Inputs are not Storing in database while uploading an image上传图像时输入未存储在数据库中
【发布时间】:2016-01-21 18:14:13
【问题描述】:

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

下面是我的 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>

【问题讨论】:

  • title、key 和 mess 是表单域。更改您的 for-each 循环以包含 if formfield 然后获取标题、键和混乱,如果没有则获取文件。
  • 我需要同时获取文件和表单域。我需要将图像路径和表单字段存储在数据库中
  • 您是否在我发布的链接中查看了 BalusC 提供的解决方案?他展示了你需要使用的 for-each 循环。
  • 我试过但没用。我可以将图像名称输入数据库,但不能输入

标签: java mysql jsp jakarta-ee servlets


【解决方案1】:

如文档中所述

将请求参数的值作为字符串返回,如果是 参数不存在 http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getParameter(java.lang.String)

你需要检查你的请求对象

【讨论】:

  • 这是我的标记
  • 尝试更改操作link
猜你喜欢
  • 2011-06-30
  • 1970-01-01
  • 2021-04-07
  • 1970-01-01
  • 2017-02-15
  • 1970-01-01
  • 2013-10-22
  • 2018-07-28
  • 2018-11-22
相关资源
最近更新 更多