我試圖將數據和圖像路徑插入到數據庫中。但是,只有圖像名稱和路徑獲得存儲在數據庫和剩餘列爲空 可以請你幫我出輸入不是在上傳圖像時存儲在數據庫中
下面是我的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>
標題,重點和食堂是formfields。改變你的for-each循環包括如果formfield然後得到標題,關鍵和混亂,如果沒有,然後獲取文件。 – rickz
http://stackoverflow.com/questions/2422468/how-to-upload-files-to-server-using-jsp-servlet?rq=1 – rickz
我需要獲取文件和表單字段。我需要將圖像路徑和表單域存儲在數據庫中 – Career