【发布时间】:2012-09-11 19:16:43
【问题描述】:
谁能帮我解决以下问题? 我正在尝试通过 servlet 将图像插入 mysql 数据库中的 blob 列。 我通过 JSP 文件中的“HTML FILE INPUT TYPE”选择图像。因此选择了图像的完整路径。
我得到的错误是:
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();
谁能帮帮我?
【问题讨论】:
-
我强烈怀疑程序图像路径中的某些位置是硬编码的。
-
感谢 Nambari 抽出时间来回答。但正如我上面提到的,没有任何更改的相同程序可以在我朋友的计算机上运行。
-
尝试将
fis写入一个新文件,看看它是否被创建... -
@alfasin:我强烈建议您也阅读答案,因为您似乎根本不理解这个问题。
-
@BalusC 我在考虑你在#2(相对路径)中提到的同样的问题。我得承认我没有考虑你提到的其他事情。对于@KaR,顺便说一句,该文件必须与您的
JSP文件位于同一文件夹中 - 不在任何其他桌面/根文件夹中。
标签: java mysql filenotfoundexception