package com.gtlsoftware;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import java.sql.PreparedStatement;
@MultipartConfig(fileSizeThreshold=1024*1024*2, // 2MB
maxFileSize=1024*1024*10, // 10MB
maxRequestSize=1024*1024*50) // 50MB
public class MenuServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public MenuServlet()
{
super();
// TODO Auto-generated constructor stub
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
Connection con;
ResultSet rs;
PreparedStatement pst;
//INSERT INTO DATABASE
PrintWriter pw=response.getWriter();
pw.print("hello");
String name=request.getParameter("name");
pw.println(name);
System.out.println(name);
String description = request.getParameter("description");
String image = request.getParameter("image");
String category=request.getParameter("category");
String unit=request.getParameter("unit");
String units=request.getParameter("units");
String price=request.getParameter("price");
byte[] b=null;
try
{
con=MyConnection.getConnection();
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload sfu = new ServletFileUpload(factory);
List items = sfu.parseRequest(request);
Iterator iter = items.iterator();
while (iter.hasNext())
{
FileItem item = (FileItem) iter.next();
if (!item.isFormField())
{
b = item.get();
}
}
pst=con.prepareStatement("insert into menu(menu_name,description,image,category,unit,units,price) values(?,?,?,?,?,?,?)");
pst.setString(1, name);
pst.setString(2, description);
pst.setBytes(3,b);
pst.setString(4, category);
pst.setString(5, unit);
pst.setString(6,units);
pst.setString(7,price);
pst.executeUpdate();
System.out.println("inserted successfully");
}
catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (FileUploadException e)
{
System.out.println(e.getMessage());
// TODO: handle exception
}
}
}
我插入了编码图像。但是当我使用表单时enctype =" multipart / form-data然后空值转到下一页我还添加了commons日志和文件上传jar,如果我删除enctype然后值插入但图像未插入在longblob中拍摄图像
和jsp页面是menu.jsp
<body>
<center>
<div>
<table border="0" cellspacing="10" cellpadding="5" style="text-align: left">
<tr>
<td><%@ include file="header.jsp" %></td>
</tr>
<tr>
<td>
<div class="main_div">
<fieldset>
<form name="menuFrm" action="MenuServlet" method="post" onsubmit="reurn(validate())" enctype="multipart/form-data" >
<table cellspacing="7" cellpadding="2" align="center" border="0" style="font-family: sans-serif;font-weight: normal;font-size: medium">
<tr>
<td colspan="2" style="background-color: black;color: white;font-family: Centaur;font-size: 20px;text-align: center;">
<h4>Menu</h4>
</td>
</tr>
<tr>
<td>Menu Id :</td>
<td><input type="text" name="menuId" id="MenuId" value="<%=++cnt %>" tabindex="1" size="30%" placeholder="enter menu name"></td>
</tr>
<tr>
<td>Name :</td>
<td><input type="text" name="name" id="name" value="hdsuidd" tabindex="1" size="30%" placeholder="enter menu name"></td>
</tr>
<tr>
<td>Description:</td>
<td><textarea rows="6" cols="30" name="description" size="30%" tabindex="4" id="description" size="30%" placeholder="enter description"></textarea></td>
</tr>
<tr>
<td>Image:</td>
<td><input type="file" class="txtset" id="image" name="image" tabindex="5"/></td>
</form>
</tr>
<tr>
<td>Category:</td>
<td>
<select id="category" class="txtset" name="category">
<option selected="selected">select category</option>
<option value="veg">Veg</option>
<option value="non-veg">Non-Veg</option>
</select>
</td>
</tr>
<tr>
<td>Units:</td>
<td><input type="text" name="unit" id="unit" class="txtset" tabindex="6" width="10%" onkeyup="myNum()"/>
<select id="units" name="units">
<option selected="selected">select units</option>
<option value="kg">KG</option>
<option value="gram">GRAM</option>
<option value="unit">Unit</option>
</select>
</td>
</tr>
<tr>
<td>Price:</td>
<td><input type="text" name="price" class="txtset" id="price" onkeyup="myNum()" tabindex="7" width="30%" height="10%"> /-Rs.</td>
</tr>
<tr>
<td><font color='white'> <DIV id="une" style="background-color: red;font-weight: bold;"> </DIV> </font></td>
</tr>
<tr>
<td><input type="submit" value="Add" style="font-family: sans-serif;font-size: large;text-align: center;width" onclick="valid();check()"/></td>
<td><input type="reset" value="Cancel" style="font-family: sans-serif;font-size: large;text-align: center;width" onclick="reset()"/></td>
</tr>
<tr>
<td colspan="2">
<br>
<br>
<a href="edit.jsp" style="color: black;text-align:center;font-family: Centaur;font-size: 20px;">
<u>Edit</u></a>
<a href="update.jsp" style="color: black;text-align:center;font-family: Centaur;font-size: 20px;">
<u>Update</u></a>
<a href="delete.jsp" style="color: black;text-align:center;font-family: Centaur;font-size: 20px";>
<u>Delete</u></a>
</td>
</tr>
</table>
</form>
<%
con.close();
%>
</fieldset>
</div>
</td>
</tr>
<tr>
<td><%@ include file="footer.jsp" %></td>
</tr>
</table>
</div>
</center>
</body>
答案 0 :(得分:-1)
您使用错误的代码在servlet中接收和插入图像。图像始终作为多部分数据发送到servlet,因此使用form enctype="multipart/form-data
是正确的。但是在你的servlet中,你接收的图像是错误的参数。图像应作为零件数据接收,如下所示: -
Part filePart=request.getPart("image");
然后我们从零件数据中检索文件路径,如下所示: -
String filePath = filePart.getSubmittedFileName();
从路径中检索图像文件后,将其转换为文件输入流,以便将其作为二进制数据插入: -
File image = new File(filePath);
FileInputStream fis = new FileInputStream(image);
之后插入图像的正确语句是: -
pst.setBinaryStream (3, fis, (int) image.length() );
此处3是表格中longblob数据的列值。在servlet中进行这些修改后,我确信所有表单数据都将成功插入数据库中。是的,从代码中删除以下几行: -
pw.print("hello");
pw.println(name);
System.out.println("inserted successfully");
创建两个名为SuccessPage.html和FailurePage.html的Html页面,它们将成功和失败消息打印到控制台并编码最后一行插入操作,如下所示: -
int rowCount=pst.executeUpdate();
if(rowCount>0){
response.sendRedirect("SuccessPage.jsp");
}
else{
response.sendRedirect("FailurePage.jsp");
}
P.S。 :替换你的mulitpart配置行: -
@MultipartConfig(fileSizeThreshold=1024*1024*2, // 2MB
maxFileSize=1024*1024*10, // 10MB
maxRequestSize=1024*1024*50) // 50MB
由此: -
@MultipartConfig(location="/tmp", fileSizeThreshold=1024*1024*2, maxFileSize=1024*1024*10, maxRequestSize=1024*1024*50)
如果此答案对您有帮助,请点击左上角的右侧标记接受该答案。如果没有,请告诉我。祝一切顺利 :) 这是您修改过的JSP: -
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<center>
<div>
<table border="0" cellspacing="10" cellpadding="5" style="text-align: left">
<%--<tr>
<td><%@ include file="header.jsp" %></td>
</tr>--%>
<tr>
<td>
<div class="main_div">
<fieldset>
<form name="menuFrm" action="SOQ52" method="post" onsubmit="reurn(validate())" enctype="multipart/form-data" >
<table cellspacing="7" cellpadding="2" align="center" border="0" style="font-family: sans-serif;font-weight: normal;font-size: medium">
<tr>
<td colspan="2" style="background-color: black;color: white;font-family: Centaur;font-size: 20px;text-align: center;">
<h4>Menu</h4>
</td>
</tr>
<tr>
<td>Menu Id :</td>
<td><input type="text" name="menuId" id="MenuId" tabindex="1" size="30%" placeholder="enter menu name"></td>
</tr>
<tr>
<td>Name :</td>
<td><input type="text" name="name" id="name" tabindex="1" size="30%" placeholder="enter menu name"></td>
</tr>
<tr>
<td>Description:</td>
<td><textarea rows="6" cols="30" name="description" size="30%" tabindex="4" id="description" size="30%" placeholder="enter description"></textarea></td>
</tr>
<tr>
<td>Image:</td>
<td><input type="file" class="txtset" id="image" name="image" tabindex="5"/></td>
</form>
</tr>
<tr>
<td>Category:</td>
<td>
<select id="category" class="txtset" name="category">
<option selected="selected">select category</option>
<option value="veg">Veg</option>
<option value="non-veg">Non-Veg</option>
</select>
</td>
</tr>
<tr>
<td>Units:</td>
<td><input type="text" name="unit" id="unit" class="txtset" tabindex="6" width="10%" onkeyup="myNum()"/>
<select id="units" name="units">
<option selected="selected">select units</option>
<option value="kg">KG</option>
<option value="gram">GRAM</option>
<option value="unit">Unit</option>
</select>
</td>
</tr>
<tr>
<td>Price:</td>
<td><input type="text" name="price" class="txtset" id="price" onkeyup="myNum()" tabindex="7" width="30%" height="10%"> /-Rs.</td>
</tr>
<tr>
<td><font color='white'> <DIV id="une" style="background-color: red;font-weight: bold;"> </DIV> </font></td>
</tr>
<tr>
<td><input type="submit" value="Add" style="font-family: sans-serif;font-size: large;text-align: center;width" onclick="valid();check()"/></td>
<td><input type="reset" value="Cancel" style="font-family: sans-serif;font-size: large;text-align: center;width" onclick="reset()"/></td>
</tr>
<tr>
<td colspan="2">
<br>
<br>
<a href="edit.jsp" style="color: black;text-align:center;font-family: Centaur;font-size: 20px;">
<u>Edit</u></a>
<a href="update.jsp" style="color: black;text-align:center;font-family: Centaur;font-size: 20px;">
<u>Update</u></a>
<a href="delete.jsp" style="color: black;text-align:center;font-family: Centaur;font-size: 20px";>
<u>Delete</u></a>
</td>
</tr>
</table>
</form>
</fieldset>
</div>
</td>
</tr>
<%--<tr>
<td><%@ include file="footer.jsp" %></td>
</tr>--%>
</table>
</div>
</center>
</body>
</html>
这是你的servlet: -
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
//import org.apache.commons.fileupload.FileItem;
//import org.apache.commons.fileupload.FileUploadException;
//import org.apache.commons.fileupload.disk.DiskFileItemFactory;
//import org.apache.commons.fileupload.servlet.ServletFileUpload;
import java.sql.PreparedStatement;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.Part;
@MultipartConfig(fileSizeThreshold=1024*1024*2, // 2MB
maxFileSize=1024*1024*10, // 10MB
maxRequestSize=1024*1024*50) // 50MB
public class SOQ52 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
//Connection con;
ResultSet rs;
PreparedStatement pst;
//INSERT INTO DATABASE
PrintWriter out=response.getWriter();
//pw.print("hello");
String name=request.getParameter("name");
//pw.println(name);
System.out.println(name);
String description = request.getParameter("description");
//String image = request.getParameter("image");
String category=request.getParameter("category");
String unit=request.getParameter("unit");
String units=request.getParameter("units");
String price=request.getParameter("price");
//byte[] b=null;
Part filePart=request.getPart("image");
String filePath = filePart.getSubmittedFileName();
File image = new File(filePath);
FileInputStream fis = new FileInputStream(image);
/*
try
{
//con=MyConnection.getConnection();
//DiskFileItemFactory factory = new DiskFileItemFactory();
//ServletFileUpload sfu = new ServletFileUpload(factory);
//List items = sfu.parseRequest(request);
//Iterator iter = items.iterator();
//while (iter.hasNext())
{
//FileItem item = (FileItem) iter.next();
//if (!item.isFormField())
{
//b = item.get();
}
}
pst=con.prepareStatement("insert into menu(menu_name,description,image,category,unit,units,price) values(?,?,?,?,?,?,?)");
pst.setString(1, name);
pst.setString(2, description);
pst.setBytes(3,b);
pst.setString(4, category);
pst.setString(5, unit);
pst.setString(6,units);
pst.setString(7,price);
pst.executeUpdate();
System.out.println("inserted successfully");
}
//catch (SQLException e)
{
// TODO Auto-generated catch block
//e.printStackTrace();
}
//catch (FileUploadException e)
{
//System.out.println(e.getMessage());
// TODO: handle exception
}*/
response.setContentType("text/html;charset=UTF-8");
try {
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet TestServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1> Name : " +name+"</h1>");
out.println("<h1> Desc : " +description+"</h1>");
out.println("<h1> Category : " +category+"</h1>");
out.println("<h1> Unit : " +unit+"</h1>");
out.println("<h1> Units : " +units+"</h1>");
out.println("<h1> Price : " +price+"</h1>");
out.println("<h1> Image filePath : " +filePath+"</h1>");
out.print("");
out.println("</body>");
out.println("</html>");
}catch(Exception ex){
}
}
}
运行它并自己检查。没有什么是空的。