今天在学习文件上传,但是在批量上传包含图片的文件时,程序报错。
package com.control;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.bean.Student;
import com.daoImpl.StuManagerDaoImpl;
import com.opensymphony.xwork2.ActionSupport;
public class AddAllStuAction extends ActionSupport{
private List image;
private List imageFileName;
private List imageContentType;
//重写父类的execute方法
public String execute(){
StuManagerDaoImpl dao=new StuManagerDaoImpl();
HttpServletRequest request=ServletActionContext.getRequest();
//因为批量添加传过来的信息很多,所有需要定义一个数组进行接收
String [] sid=request.getParameterValues("sid");
String [] sname=request.getParameterValues("sname");
String [] sage=request.getParameterValues("sage");
//得到上传路径
String path=ServletActionContext.getServletContext().getRealPath("/stuImage");
System.out.println(path);
for(int i=0;i
//设置目标路径
File file=new File(path,imageFileName.get(i));
try {
//文件拷贝
FileInputStream fis=new FileInputStream(image.get(i));
FileOutputStream fos=new FileOutputStream(file);
//定义一个数组
byte [] b=new byte[1024];
int len;
while((len=fis.read(b))>0){
fos.write(b,0, len);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//现在实例化学生对象
Student stu=new Student();
stu.setSid(Integer.parseInt(sid[i]));
stu.setSname(sname[i]);
stu.setSage(Integer.parseInt(sage[i]));
stu.setSphoto("stuimage"+"\\"+imageFileName.get(i));
dao.toSaveStu(stu);
}
return SUCCESS;
}
public List getImage() {
return image;
}
public void setImage(List image) {
this.image = image;
}
public List getImageFileName() {
return imageFileName;
}
public void setImageFileName(List imageFileName) {
this.imageFileName = imageFileName;
}
public List getImageContentType() {
return imageContentType;
}
public void setImageContentType(List imageContentType) {
this.imageContentType = imageContentType;
}
}
浏览器访问的时候,点击提交,报空指针异常。哪位大神可以指导下我啊?跪求