上传图像时遇到错误

时间:2014-10-04 14:31:42

标签: mysql image jsp upload

我正在尝试使用jsp将图像上传到我的数据库。我想将图像存储在名为Upload的文件夹中,并希望在数据库中插入图像名称。我使用servlet来做到这一点。 我使用单独的页面进行输入,操作和数据库连接,我解释下面的代码。

Photo.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body><form name="f" action="uploadimage" method="post" enctype="multipart/form-data">
            <table>
                <tr><td>
            Name</td><td><input type="text" name="name"/></td></tr>
                <tr><td> Address</td><td><textarea name="addr" rows="10" cols="10"></textarea></td></tr>
                <tr> <td><input type="file" name="f1"></td></tr>
                <tr><td colspan="2"><input type="submit" name="b1"></td></tr></table> </form>   
    </body>
</html>

我的数据库连接如下:

public int insert_file(String name, String addr, String img) 
{
    int i=0;
    System.out.println("inside insert_file");
    try
    {
        Connection con21=get_con();
        System.out.print(con21);
        Statement s21=con21.createStatement();
        String s11="insert into image(name,addr,image) values ('"+name+"','"+addr+"','"+img+"')";
        System.out.println("i"+s11);
        i=s21.executeUpdate(s11);
        System.out.println(i);
    }
    catch(Exception e)
    {
        System.out.println(e);
    }
    return i;
}  

这是我的servlet页面:

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.tomcat.util.http.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import DBConnection.MYConnection;

/**
 *
 * @author MONOOS
 */
@WebServlet(name = "uploadimage", urlPatterns = {"/uploadimage"})
public class uploadimage extends HttpServlet {
String name,addr,img;
//MYConnection obj=new MYConnection();


    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        MYConnection obj=new MYConnection();
        out.println(obj);
        out.println("haiiii");
        try {
            out.println("inside try");
            System.out.print("inside try");
        List fileItems;


        // Parsing field values
        DiskFileItemFactory factory = new DiskFileItemFactory();
        out.println(factory);
        factory.setSizeThreshold(10000000);

        // Create a new file upload handler
        ServletFileUpload upload = new ServletFileUpload(factory);
        // maximum file size to be uploaded.
        upload.setSizeMax(10000000);

        try {
            // Parse the request to get file items.
            out.println("inside nested try");
            fileItems = upload.parseRequest(request);

            // Process the uploaded file items
            Iterator i = fileItems.iterator();
            out.println(i);
            while (i.hasNext()) {

                FileItem fi = (FileItem) i.next();

                if (fi.isFormField()) {
                     if (fi.getFieldName().equals("name")) {                        
                        name = fi.getString();
                        out.println(name);;
                        System.out.println("name:"+name);
                    }
                      if (fi.getFieldName().equals("addr"))
                      {                        
                        addr = fi.getString();
                        System.out.println("address:"+addr);
                        out.println(addr);
                      }
                }

                    else 
                   {
                       out.println("inside else");
                   System.out.println("inside else");
                   img = fi.getName();
                   System.out.println("Image :"+img);
                    File outfile = new File("I:\\NetBeansProjects\\MGNREGA\\web\\Upload" + fi.getName());
                    fi.write(outfile);
                }
            }

            int j=obj.insert_file("nayana","gfg",img);
            System.out.println("j:" + j);

        }

        catch(Exception e)
        {
           System.out.print(e);
        }
     }
        finally {            
            out.close();
        }

    }// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}

我的问题是代码正在运行,但它没有按我的意愿行事。 try子句中的第一个print语句成功打印。

1 个答案:

答案 0 :(得分:0)

我解决了我的问题......我更改了包&#34; import org.apache.tomcat.util.http.fileupload.FileItem;&#34;进入&#34; import org.apache.commons.fileupload.FileItem;&#34;。 现在它运行良好....