【问题标题】:Upload an image into the MySql database using Java MVC [duplicate]使用Java MVC将图像上传到MySql数据库[重复]
【发布时间】:2016-02-16 21:11:23
【问题描述】:

我想将图像上传到数据库中,但遇到了问题。这是我的代码, Pojo类

public class Pojo {
    String fnm;
    String lnm;
    InputStream b;
    public String getFnm() {
        return fnm;
    }
    public void setFnm(String fnm) {
        this.fnm = fnm;
    }
    public String getLnm() {
        return lnm;
    }
    public void setLnm(String lnm) {
        this.lnm = lnm;
    }
    public InputStream getB() {
        return b;
    }
    public void setB(InputStream b) {
        this.b = b;
    }

}

BL 经理

public class BusinessLogic {
    public boolean insert(Pojo bean)
    {
        boolean check_connection=false;

        try
        {     
            System.out.println("inside Registration");
            Connection conn= (Connection) DBConnection.connect();

            String sql = "INSERT INTO image1 (first_name, last_name, photo) values (?, ?, ?)";
           PreparedStatement ps = (PreparedStatement) conn.prepareStatement(sql);
            ps.setString(1,bean.getFnm());
            ps.setString(2,bean.getLnm());
            ps.setBlob(3, bean.getB());
            ps.executeUpdate();
            check_connection =true;
            conn.close();
        }
       catch(Exception e)
       {
           System.out.println(""+e);
       }
        return check_connection;
    }
}  

Servlet 文件

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String firstName = request.getParameter("firstName");
        String lastName = request.getParameter("lastName");
        InputStream inputStream = null; // input stream of the upload file
        Part filePart = request.getPart("photo");
        if (filePart != null) {
            // prints out some information for debugging
            System.out.println(filePart.getName());
            System.out.println(filePart.getSize());
            System.out.println(filePart.getContentType());

            // obtains input stream of the upload file
            inputStream = filePart.getInputStream();
        }
        Pojo p=new Pojo();
        p.setFnm(firstName);
        p.setLnm(lastName);
        if (inputStream != null) {
            // fetches input stream of the upload file for the blob column
            p.setB(inputStream);
        //  statement.setBlob(3, inputStream);
        }
        BusinessLogic bl=new BusinessLogic();
        boolean b=bl.insert(p);
        if(b)
        {
            System.out.println("success");
        }
        else {
            System.out.println("success");
        }
}

数据库连接文件...

public class DBConnection {

    public static Connection con;
    public static Connection connect()
    {
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            con=DriverManager.getConnection("jdbc:mysql://localhost:3306/img","root","");
            System.out.println("connected!!");
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
        return con;
    }
    public static void close() throws SQLException
    {
        if(con!=null)
        {
            con.close();
        }
    }
}

当我尝试将图像插入数据库时​​,出现以下错误:

java.lang.AbstractMethodError: 方法 com/mysql/jdbc/PreparedStatement.setBlob(ILjava/io/InputStream;)V 是 摘要于 com.mysql.jdbc.PreparedStatement.setBlob(PreparedStatement.java) 在 com.image.model.BusinessLogic.insert(BusinessLogic.java:22)

请帮我解决这个问题...

【问题讨论】:

    标签: java mysql


    【解决方案1】:

    名为 cos.jar 的 JAR 文件包括 com.oreilly.servlet 和 com.oreilly.servlet.multipart 包。这些包包括几个类,例如所有以“Multipart”开头的 Java 类,它们可用于处理 servlet 中的文件上传。你可以找到它的jar,它很容易集成。

    MultipartRequest m=new MultipartRequest(request,"d:/new");  
    out.print("successfully uploaded");  
    

    【讨论】:

    • 这应该如何解决有关异常的问题?
    • 它将管理您的文件上传机制。然后您只需将图像名称插入数据库。就是这样。
    猜你喜欢
    • 2016-05-26
    • 1970-01-01
    • 2013-04-22
    • 2013-05-15
    • 1970-01-01
    • 2018-08-10
    • 2015-01-01
    • 2017-10-28
    • 2017-05-14
    相关资源
    最近更新 更多