簡體   English   中英

無法使用servlet將映像插入mysql數據庫

[英]Unable to Insert image to mysql database using servlet

誰能幫我解決以下問題? 我正在嘗試通過servlet將圖像插入mysql數據庫中的blob列。 我通過JSP文件中的“ HTML文件輸入類型”選擇圖像。因此,選擇了圖像的完整路徑。

我一直得到的錯誤是:

HTTP Status 500 - Desert.jpg (The system cannot find the file specified)

type Exception report

message Desert.jpg (The system cannot find the file specified)

description The server encountered an internal error (Desert.jpg (The system cannot find the file specified)) that prevented it from fulfilling this request.

exception

    java.io.FileNotFoundException: Desert.jpg (The system cannot find the file specified)
        java.io.FileInputStream.open(Native Method)
        java.io.FileInputStream.<init>(Unknown Source)
        Image.ImgInsert.doGet(ImgInsert.java:51)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

note The full stack trace of the root cause is available in the Apache Tomcat/7.0.28 logs.
Apache Tomcat/7.0.28

其中“ Desert.jpg”是我的桌面上的圖像。 該程序可在我的朋友計算機上使用。 這是servlet代碼:

    String s_id = request.getParameter("myid");
    int id = Integer.parseInt(s_id);
    //IMAGE ACQUIRED FROM THE FROM THE JSP PAGE
    String img = request.getParameter("myimg");

    File f = new File(img);
    FileInputStream fis = new FileInputStream(f);

    String query="insert into images.imageinsert(id,image) values(?,?)";
    try
    {
        PreparedStatement pStatement = conn.prepareStatement(query);

        pStatement.setInt(1, id);
        pStatement.setBinaryStream(2, fis);
        int result = pStatement.executeUpdate();

        if(result != 0)
        {
            response.sendRedirect("ImageHome.html");
        }
        pStatement.close();

有人可以幫我嗎?

這里至少存在兩個嚴重的概念錯誤。

  1. 您似乎認為擁有客戶端本地磁盤文件系統路徑足以在服務器端獲取整個文件內容。 這是不可能的,因為客戶端和服務器不會共享相同的磁盤文件系統(除非它們都恰好在同一台計算機上運行,​​而這在現實世界中當然不會發生)。

  2. 您依賴於java.io相對路徑。 它相對於所謂的“當前工作目錄”相對應,后者是在啟動Web服務器的那一刻打開的文件夾。 絕對不是Web應用程序直接位於其中的文件夾。例如,這是C:\\path\\to\\tomcat\\bin 當然,上載的文件不是神奇地放置在其中。

至於為什么它可以在您的“朋友”的計算機上工作,那無疑是因為他正在使用Internet Explorer,它具有一個安全漏洞,其中完整的文件路徑而不是僅文件名作為請求參數發送到<input type="file"> <form> <input type="file">字段,不帶enctype="multipart/form-data" 再次回答,這種方法肯定不能在實際的生產環境中使用。

通過仔細閱讀以下答案,可以理解和解決您的具體問題。

暫無
暫無

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

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