1

我已经提出申请。如果我只上传图像,那么事情就做得很好,但是当我使用文本框作为另一个输入,然后在我尝试将两个数据上传到数据库中之后,图像存储在里面,但文本框数据甚至没有传递到 servlet 文件。

这是我的jsp页面代码:

<body>
<form action="UploadPhoto" method="post" enctype="multipart/form-data">
<p>

<input type="file" name="ImageFile" id="ImageFile" />
</p>

<br>
<br>
<input type="text" name="txtname" placeholder="Name">
<p>
<input type="submit" name="submit" value="Upload" />
</p>

</form>
</body>

这是我的 servlet 文件:

package com.crime.servlet;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.*;

import sun.misc.BASE64Encoder;

import java.io.*;
import java.util.Iterator;
import java.util.List;
import java.io.File;


public class UploadPhoto extends HttpServlet {
    private static final long serialVersionUID = 1L;


    public UploadPhoto() {
        super();        
    }


    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

    }


    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

            try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        String name=request.getParameter("txtname");

        Connection con = null;
        String url = "jdbc:mysql://localhost:3306/AppDb";
        try {
            con = DriverManager.getConnection(url, "root", "root");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if (con != null) {

            System.out.println("Connection Succesful");
        } else {
            System.out.println("connection Failed!!!");
        }
//      ============connection endd===============



        PreparedStatement psmnt = null;

        try {
        //  String ImageFile = "";
            String itemName = "";
            boolean isMultipart = ServletFileUpload.isMultipartContent(request);
            if (!isMultipart) {
            } else {
                FileItemFactory factory = new DiskFileItemFactory();
                ServletFileUpload upload = new ServletFileUpload(factory);
                List items = null;
                try {
                    items = upload.parseRequest(request);
                } catch (FileUploadException e) {
                    e.getMessage();
                }
                System.out.println(items);
                Iterator itr = items.iterator();
                while (itr.hasNext()) {
                    FileItem item = (FileItem) itr.next();
                    if (!item.isFormField()) {

                        try {

                                itemName = item.getName();
                            //  String str = getServletContext().getRealPath("/")
                                    //  + "Example\\" + itemName;
                                    //  String str="D:\\images + itemName";
                                System.out.println("============");
                                System.out.println(itemName);
                                System.out.println(item.getContentType());
                                System.out.println(item.getSize());
                                System.out.println(name);
                                System.out.println("============");
                                File savedFile = new File(itemName);
                                item.write(savedFile);

                                System.out.println("beforee image=========");

                                String imageString = null;

                                BufferedImage originalImage = ImageIO.read(new File(itemName));
                                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                                ImageIO.write( originalImage, "jpg", baos );
                                baos.flush();
                                byte[] imageInByte = baos.toByteArray();
                                baos.close();

                                BASE64Encoder encoder = new BASE64Encoder();
                                imageString = encoder.encode(imageInByte);
                                imageString = "data:image/jpeg;base64,"+imageString;
                                String query = "insert into test(name,image) values (?,?)";
                                psmnt = con.prepareStatement(query);

                                //psmnt.setString(1, null);
                                psmnt.setString(1, name);
                                psmnt.setBytes(2, imageString.getBytes());
                                int s = psmnt.executeUpdate();

                                response.sendRedirect("retrivephoto.jsp");
                            } catch (Exception e) {
                                System.out.println("Error" + e.getMessage());
                            }

                    }


                    }
                }

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

    }

}

任何人都可以请帮我解决这个问题......


4

1 回答 1

0

在 if 条件之后

if (!item.isFormField()) {
      ......
      ......
 }

添加以下代码

else if (item.isFormField()) {
String name = item.getFieldName();
String value = item.getString();
...
}

这将正常工作。

参考:Apache Commons 文档

于 2013-10-17T03:39:57.147 回答