首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

java长传有关问题,输入输出流

2012-06-12 
java长传问题,输入输出流java.lang.NullPointerExceptionat java.io.FileInputStream.init(FileInputStr

java长传问题,输入输出流
java.lang.NullPointerException
at java.io.FileInputStream.<init>(FileInputStream.java:103)
at com.mengfang.action.FileUploadAction.execute(FileUploadAction.java:49)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)


后台是这样的
String path = ServletActionContext.getRequest().getRealPath("/assignFile");
try {
File f = this.getFile();
FileInputStream inputStream = new FileInputStream(f);
FileOutputStream outputStream = new FileOutputStream(path + "/"+ this.getFileFileName());
byte[] buf = new byte[1024];
int length = 0;
while ((length = inputStream.read(buf)) != -1) {
outputStream.write(buf, 0, length);
}
inputStream.close();
outputStream.flush();

} catch (Exception e) {
e.printStackTrace();
}
System.out.println(path);
return "success";

[解决办法]
File f = this.getFile();

f 返回的是null
[解决办法]
this.getFile()为null,所以获取该文件的输入流的时候空指针异常
[解决办法]
正解!!!!

探讨
File f = this.getFile();

f 返回的是null

[解决办法]
java.lang.NullPointerException
at java.io.FileInputStream.<init>(FileInputStream.java:103)
你在
FileInputStream inputStream = new FileInputStream(f);
时f是null报空指针肯定的
你写个死的路径试试
[解决办法]
File f = this.getFile();
if(f!=null){
FileInputStream inputStream = new FileInputStream(f);
FileOutputStream outputStream = new FileOutputStream(path + "/"+ this.getFileFileName());
byte[] buf = new byte[1024];
int length = 0;
while ((length = inputStream.read(buf)) != -1) {
outputStream.write(buf, 0, length);
....}

热点排行