簡體   English   中英

使用setBlob從jsp表單使用servlet將映像插入數據庫

[英]Inserting image to database using servlet from jsp form using setBlob

使用setBlob()時我一直得到抽象方法錯誤

我也在這個論壇上搜索,但沒有一個答案解決了我的問題。

我在用 :

jdk 1.7

Oracle 11g

Apache Tomcat 8.0

FileUploadDBServlet.java

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

@WebServlet("/uploadServlet")
@MultipartConfig(maxFileSize = 16177215)    // upload file's size up to 16MB
public class FileUploadDBServlet extends HttpServlet {

// database connection settings
String dbURL="jdbc:oracle:thin:@localhost:1521: XE";
    String userId="system";
    String password="moon";


protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    // gets values of text fields
    String id= request.getParameter("i1");
    String name= request.getParameter("i2");
    String brand= request.getParameter("i3");
    String price= request.getParameter("i4");
    String desc= request.getParameter("i5");
    String sid= request.getParameter("i6");
    String d= request.getParameter("i7");


    InputStream inputStream = null; // input stream of the upload file

    // obtains the upload file part in this multipart request
    Part filePart = request.getPart("i8");
    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();
    }

    Connection conn = null; // connection to the database
    String message = null;  // message will be sent back to client

    try {
        // connects to the database
        conn= DriverManager.getConnection(dbURL, userId, password);

        // constructs SQL statement
        String sql = "INSERT INTO item (itemid,name,brand,price,sid,img,dt,description) values (?, ?, ?,?,?,?,?,?)";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1,id);
        ps.setString(2,name);
        ps.setString(3,brand);
        ps.setString(4,price);
        ps.setString(5,desc);
        ps.setString(6,d);
        ps.setString(7,desc);



        if (inputStream != null) {
            // fetches input stream of the upload file for the blob column
            ps.setBlob(8, inputStream);
        }

        // sends the statement to the database server
        int row = ps.executeUpdate();
        if (row > 0) {
            message = "File uploaded and saved into database";
        }
    } catch (SQLException ex) {
        message = "ERROR: " + ex.getMessage();
        ex.printStackTrace();
    } finally {
        if (conn != null) {
            // closes the database connection
            try {
                conn.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        // sets the message in request scope
        request.setAttribute("Message", message);

        // forwards to the message page
               getServletContext().getRequestDispatcher("/Message.jsp").forward(request, response);
        }
    }
}

您應該使用setBinaryStream方法而不是setBlob方法

或者您應該在參數中使用Blob類型而不是InputStream類型

由於

1. public void setBinaryStream(int parameterIndex, InputStream stream, int length)

2. public void setBlob(int parameterIndex,Blob value)

如果選擇第一種方法,請考慮length參數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM