刚学Struts2,按照书上原模原样的敲的代码,本来可以上传文件的,后来我加了手动文件过滤功能,后来又注释掉了,然后就不行了,报空指针异常! 这是显示的错误
这是Action里的代码
public class UploadAction extends ActionSupport {
private String title; //封装上传文件标题的成员变量
private File upload; //封装上传文件域的成员变量
private String uploadContentType; / /封装上传文件类型的成员变量
private String uploadFileName;// 封装上传文件名字的属性
private String savePath;
// 接受struts.xml 文件配值的方法
public void setSavePath(String value) {
this.savePath = value;
}
// 获取上传文件的保存位置
private String getSavePath() throws Exception {
return ServletActionContext.getServletContext().getRealPath(savePath);
}
public String getTitle() {
return title;
}/
public void setTitle(String title) {
this.title = title;
}
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
@Override
public String execute() throws Exception {
FileOutputStream fos=new FileOutputStream(getSavePath()+"\\"+getUploadFileName());
FileInputStream fis=new FileInputStream(getUpload());
byte[] buffer= new byte[1024];
int len=0;
while((len=fis.read(buffer))>0){
fos.write(buffer, 0, len);
}
return SUCCESS;
}
}这是strusts.xml里的代码
<struts> <package name="lee" extends="struts-default" > <action name="upload" class="action.UploadAction"> <param name="savePath">/uploadFiles</param> <!-- <param name="allowTypes">image/png,image/bmp,image/gif,image/jpeg</param> --> <result> /success.jsp</result> <!-- <result name="input">/upload.jsp</result>--> </action> </package>
谢谢各位了!!!!在线等
拉拉拉
路人丶丨
随时随地看视频慕课网APP
相关分类