Java 我在jsp页面中进行了编码,但下一页它给出了空值

Java 我在jsp页面中进行了编码,但下一页它给出了空值,java,image,jsp,servlets,Java,Image,Jsp,Servlets,我没有编码插入图像。但当我在表单enctype=“multipart/form data”中使用时,空值会转到下一页,我还添加了commons日志记录和文件上载jar,如果我删除了enctype,则值插入但图像不插入我在longblob中拍摄图像 jsp页面是menu.jsp package com.gtlsoftware; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; im

我没有编码插入图像。但当我在表单enctype=“multipart/form data”中使用时,空值会转到下一页,我还添加了commons日志记录和文件上载jar,如果我删除了enctype,则值插入但图像不插入我在longblob中拍摄图像

jsp页面是menu.jsp

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
        }


    }

}

菜单
菜单Id:
姓名:
说明:
图片:
类别:
选择类别
蔬菜
非素食者
单位:
选择单位
公斤
克
单位
价格:
/-卢比。



您使用错误的代码在servlet中接收和插入图像。图像始终作为多部分数据发送到servlet,因此使用
form enctype=“multipart/form data
是正确的。但在servlet中,您将图像作为参数接收,这是错误的。图像应作为以下部分数据接收:-

<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%">&nbsp;&nbsp;/-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>
                                            &nbsp;   &nbsp; &nbsp;
                                            <a href="update.jsp" style="color: black;text-align:center;font-family: Centaur;font-size: 20px;">
                                            <u>Update</u></a>
                                            &nbsp;   &nbsp; &nbsp;
                                            <a href="delete.jsp" style="color: black;text-align:center;font-family: Centaur;font-size: 20px";>
                                            <u>Delete</u></a>
                                            &nbsp;   &nbsp; &nbsp;

                                    </td>
                                </tr>
                            </table>


                        </form>
                            <%
                                con.close();
                            %>

                    </fieldset>
                    </div>

                </td>
            </tr>
            <tr>
                <td><%@ include file="footer.jsp" %></td>
            </tr>   
        </table>
        </div>
    </center>




</body>
然后我们从零件数据中检索文件路径,如下所示:-

Part filePart=request.getPart("image");
从path检索图像文件,然后将其转换为文件输入流,以便将其作为二进制数据插入后:-

String filePath = filePart.getSubmittedFileName();
在此之后,插入图像的正确语句如下:-

File image = new File(filePath);
FileInputStream fis = new FileInputStream(image);
这里3是表中longblob数据的列值。在servlet中进行这些修改之后,我确信所有表单数据都将成功插入数据库。是的,请从代码中删除以下行:-

pst.setBinaryStream (3, fis, (int) image.length() );
创建两个名为SuccessPage.Html和FailurePage.Html的Html页面,将成功和失败消息打印到控制台,并对插入操作的最后一行进行编码,如下所示:-

pw.print("hello");
pw.println(name);
System.out.println("inserted successfully");
注意:替换您的mulitpart配置行:-

int rowCount=pst.executeUpdate();
if(rowCount>0){
            response.sendRedirect("SuccessPage.jsp");
        }
        else{
            response.sendRedirect("FailurePage.jsp");
        }
在此:

@MultipartConfig(fileSizeThreshold=1024*1024*2, // 2MB
maxFileSize=1024*1024*10,      // 10MB
maxRequestSize=1024*1024*50)   // 50MB
如果这个答案对您有帮助,请单击左上角的右标记接受它。如果没有,请告诉我。祝你一切顺利:) 这是您修改的JSP:-

@MultipartConfig(location="/tmp", fileSizeThreshold=1024*1024*2, maxFileSize=1024*1024*10, maxRequestSize=1024*1024*50)

JSP页面
菜单
菜单Id:
姓名:
说明:
图片:
类别:
选择类别
蔬菜
<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%">&nbsp;&nbsp;/-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>
                                            &nbsp;   &nbsp; &nbsp;
                                            <a href="update.jsp" style="color: black;text-align:center;font-family: Centaur;font-size: 20px;">
                                            <u>Update</u></a>
                                            &nbsp;   &nbsp; &nbsp;
                                            <a href="delete.jsp" style="color: black;text-align:center;font-family: Centaur;font-size: 20px";>
                                            <u>Delete</u></a>
                                            &nbsp;   &nbsp; &nbsp;

                                    </td>
                                </tr>
                            </table>


                        </form>

                    </fieldset>
                    </div>

                </td>
            </tr>
            <%--<tr>
                <td><%@ include file="footer.jsp" %></td>
            </tr>--%>   
        </table>
        </div>
    </center>
</body>
</html>
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){

        }    

    }

}