上传图片到mysql老是报java.io.FileNotFoundException:123.jpg (系统找不到指定的文件。)
jsp提交表单到servletservlet代码如下:
if (action.equals("addImageSubmit")) {
     //如果用户执行的是上传照片操作则执行此段代码
     //收取用户提交的参数
     String albumId=request.getParameter("albumId");
     String fileName=request.getParameter("path").trim();
     String photoName=request.getParameter("photoName");
     String stemp=Long.valueOf(System.currentTimeMillis()).toString();
     String photoId=stemp.substring(6,stemp.length());
     //调用JavaBean将图片存入数据库中
boolean b=AlbumDAOBean.insertImage(albumId,photoId,photoName,fileName);
if(b) {
this.forword(request,response,"/myalbum.jsp");
} else {
this.forword(request,response,"/error.jsp?msg=上传失败,请重试!");
}javabean代码如下:                    //上传照片
insertPhotoPS=con.prepareStatement(
"INSERT INTO photos(album_id,photo_id,photo_name,photo) VALUES(?,?,?,?)");public static boolean insertImage(String album_id,String photo_id,
   String photo_name,String path) {
boolean b=false;
FileInputStream fis=null;
try {
//读取照片
File f=new File(path);
int i=(int)f.length();
byte[] bb=new byte[i];
fis=new FileInputStream(f);
fis.read(bb);

insertPhotoPS.setString(1,album_id);
insertPhotoPS.setString(2,photo_id);
insertPhotoPS.setString(3,photo_name);
insertPhotoPS.setBytes(4,bb);
insertPhotoPS.execute();
b=true;
} catch (IOException ie) {
ie.printStackTrace();
b=false;
} catch (SQLException se) {
se.printStackTrace();
b=false;
} finally {//关闭流
try {
if(fis!=null) {
fis.close();
}
} catch (IOException ie) {
ie.printStackTrace();
}
}
return b;
}
之前调试可以上传,但是现在又不可以了,老是报java.io.FileNotFoundException:123.jpg (系统找不到指定的文件。)
文件时存在的!!

解决方案 »

  1.   

    java.io.FileNotFoundException:123.jpg 文件路径不对哦
      

  2.   

    能得到文件就怪了。你从客户端上传的文件,怎么能在服务器端以同一个文件去取呢?到哪儿去取?哪个目录去取??
    基本的http协议!!!!!!文件上传是以一个post请求的multipart方式上传到服务器的,服务器需要将这次请求中的文件,读取出来,并形成一个临时文件,这个临时文件才是你需要存到mysql数据库中的图片。
    建议使用,struts2 fileUploadInterceptor,或者一些比如 fileUpload的java组件来完成这个操作。
      

  3.   


    你是通过浏览选的文件,文件路径是你本地机器的。
    上传文件是上传文件数据给服务器
    服务器再在服务器端的硬盘创建一个新文件,把数据写入新文件
    因此只要看这几行//读取照片
    File f=new File(path);
    int i=(int)f.length();就知道你理解错了
      

  4.   

    java.io.FileNotFoundException:123.jpg (系统找不到指定的文件。)
    这个要图片的绝对路径,我很好奇,你为什么要把字节存入数据库呢?
    当你取这张图片时,你又要把字节写成文件,这不很麻烦吗?
    直接把上传的图片保存在服务器上,数据库中存储图片在服务器上的相对路径。
    当取图片时用request.getContextPath()+相对路径就可以显示了,这不简单一点吗?
    这是通常的做法,你的做法基本上是不可取的!
      

  5.   

    IE8加强了安全性,直接用request.getParameter('idfile')是获取不到
    File的真实路径的。要用滤镜来做。 你网上搜搜:file文件真实路径  IE8。 先获取到本机上传文件的完整路径再说。
      

  6.   

    你在保存的时候只是将文件的文件名字保存到数据库,而你的读取却把数据库保存的名字当文件读取了,你把这节改掉称普通文字读取就OK了,
    File f=new File(path);
    int i=(int)f.length();
    byte[] bb=new byte[i];
    fis=new FileInputStream(f);
    fis.read(bb);
      

  7.   

     try catch 一下
      

  8.   

    我上传文档时也总提示这个:java.io.FileNotFoundException: g123.txt (???????????)
    上学期还没有,暑假就坏了,哪也没动,郁闷,高手帮帮我~~jsp提交表单到servletservlet代码如下:        String filePath = new String(request.getParameter("filename").toString().getBytes("ISO8859_1"));
            filePath = filePath.replace('\\', '/');
            String filename = filePath.substring(filePath.lastIndexOf("/") + 1, filePath.length());
            String type = filePath.substring(filePath.indexOf(".") + 1, filePath.length());        String dir = "D:/upload/";
            File file = new File(dir + filename);
            if (!file.exists()) {
                file.createNewFile();
            }
            String path = dir + filename;        InputStream is = new FileInputStream(filePath);
            OutputStream os = new FileOutputStream(file);        byte buffer[] = new byte[1024];
            int lenght = 0;
            while (-1 != (lenght = is.read(buffer, 0, 1024))) {
                os.write(buffer, 0, lenght);
                os.flush();
            }
            os.close();
            is.close();        RandomAccessFile random = new RandomAccessFile(filePath, "rw");
            random.seek(0);
            String str = " ";
            String title = "";
            String author = "";
            String key = "";
            boolean first = true;
            while (true) {
                str = new String(random.readLine().getBytes("ISO8859_1"));
                if (first) {
                    title = str.trim();
                    first = false;
                }
                if (str.indexOf("作者") != -1) {
                    author = str.substring(str.indexOf("作者") + 3, str.length()).trim();
                }
                if (str.indexOf("关键字") != -1) {
                    key = str.substring(str.indexOf("关键字") + 4, str.length()).trim();
                    break;
                }
            }
            random.seek(0);
            random.close();        String url = "jdbc:mysql://localhost/upanddown";
            try {            Connection con = DriverManager.getConnection(url, "root", "123");
                Statement sql = con.createStatement();
                //str = "111";
                int mm = sql.executeUpdate("INSERT INTO document_info(file_name, file_type, file_path, , key_word,file_title, book_writer) VALUES ('" + filename + "','" + type + "','" + path + "','" + set + "', '" + key + "', '" + title + "', '" + author + "')");
                if (mm != 0) {
                    try {
                        RequestDispatcher dispatcher =
                                request.getRequestDispatcher("upload_docu.jsp");//转发
                        dispatcher.forward(request, response);
                    } catch (Exception ee) {
                    }            }
                con.close();
            } catch (Exception exp) {
            }
      

  9.   

    File f=new File(path);//楼主这是你写的
    上传文件是通过request.getInputStream();
    你又没用什么框架,所以必须是request.getInputStream();
    并将表单的提交方式设为method="post" 类型设为enctype="multipart/form-date"
    楼主可是试试上传一个文本文件来通过request.getInputStream();获得流里的内容打印并筛选
      

  10.   

    try {
    //读取照片
    File f=new File(path);
    在这个path没有找到123.jpg
    debug看看这个path到底是什么,可能是路径识别问题,不同服务器下面识别\和/两种都有可能,如C:/和C:\