我想测试一个用于文件上传的示例程序。但它显示错误“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 存在于那里。但我得到一个相同的文件未找到异常。请检查提供的代码并恢复。