0

我想测试一个用于文件上传的示例程序。但它显示错误“FileNotFoundException”。

   import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;



public class TestUpload {

    /**
     * @param args
     */
    public boolean handleFileUpload(){


        BufferedOutputStream bos = null;
        BufferedInputStream bis = null;
        boolean isFileUplodedCorrectly = true;
        try {
            bos = new BufferedOutputStream(new FileOutputStream(new File("D:\\vishu.jpeg")));
            bis = new BufferedInputStream(new FileInputStream(new File("D:\\vishuGreetings.jpeg")));


            byte[] b = new byte[1024];

            while (bis.read(b) != -1)
                bos.write(b);
            bos.flush();
        } catch (Exception e) {
            isFileUplodedCorrectly = false;
            e.printStackTrace();
            System.out.print("Exception in FileUpload Utils " + e);
        } finally {
            try {
                bos.close();

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
        return isFileUplodedCorrectly;

    }

public static void main(String[] args) {
        // TODO Auto-generated method stub
    TestUpload tu=new TestUpload();
    System.out.println("Status"+tu.handleFileUpload());

    }

}

实际上该文件存在于那里。请检查。

    java.io.FileNotFoundException: D:\vishuGreetings.jpeg (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:106)
    at TestUpload.handleFileUpload(TestUpload.java:23)
    at TestUpload.main(TestUpload.java:52)
Exception in FileUpload Utils java.io.FileNotFoundException: D:\vishuGreetings.jpeg (The system cannot find the file specified)Statusfalse

文件 D:/vishuGreetings.jpeg 存在于那里。但我得到一个相同的文件未找到异常。请检查提供的代码并恢复。

4

1 回答 1

0

我通过提供 .jpg 而不是 jpeg 解决了这个问题,发现它工作正常。当我检查文件的属性时,它是 jpeg。这就是为什么我在代码中的文件 nmae 中使用扩展名 jpeg 的原因。抱歉给您带来麻烦。

于 2013-05-31T12:30:31.420 回答