当前位置: 代码迷 >> Java Web开发 >> java长传有关问题,输入输出流
  详细解决方案

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

热度:4581   发布时间:2013-02-25 21:18:20.0
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);
....}
  相关解决方案