今天在学习文件上传,但是在批量上传包含图片的文件时,程序报错。
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<File> image;
private List<String> imageFileName;
private List<String> 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<image.size();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<File> getImage() {
return image;
}
public void setImage(List<File> image) {
this.image = image;
}
public List<String> getImageFileName() {
return imageFileName;
}
public void setImageFileName(List<String> imageFileName) {
this.imageFileName = imageFileName;
}
public List<String> getImageContentType() {
return imageContentType;
}
public void setImageContentType(List<String> imageContentType) {
this.imageContentType = imageContentType;
}
}
浏览器访问的时候,点击提交,报空指针异常。哪位大神可以指导下我啊?跪求
在使用之前要setImage
代码里面写了setImage()方法的,但是一直报错。
@忽而今夏颖: 要调用呀