gpt4 book ai didi

java - 上传图像时输入未存储在数据库中

转载 作者:行者123 更新时间:2023-11-29 21:54:41 25 4
gpt4 key购买 nike

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

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

最佳答案

如文档中所述

Returns the value of a request parameter as a String, or null if the parameter does not exist http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getParameter(java.lang.String)

您需要检查您的请求对象

关于java - 上传图像时输入未存储在数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33281499/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com