请教个问题~谢谢啦!

l3eta28 l3eta28 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
SSH2中配合注解的方式如何上传文件?

package chinaas.member.action.userPhoto;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.stereotype.Controller;

import chinaas.member.action.BaseAction;
import chinaas.member.entity.Photo;
import chinaas.member.service.userPhoto.UserPhotoService;

@Controller
@Namespace("/")
@ParentPackage(value = "struts-default")
@Results(@Result(name = "fail", location = "/failed.jsp"))
//@InterceptorRefs( { @InterceptorRef(value = "fileUploadStack"),@InterceptorRef(value="defaultStack")})
public class UserPhotoAction extends BaseAction {


// 用户管理Service
@Resource(name = "userPhotoServiceImpl")
private UserPhotoService userPhotoService;

// 上传文件
@SuppressWarnings("static-access")

@Action(value = "/upload", results = { @Result(name = SUCCESS, location = "/success.jsp") },interceptorRefs={@InterceptorRef(value="fileUploadStack")})
public String uploadPhoto() throws Exception {
System.out.println("upload");
//User u = (User) this.s2Util.getSession().getAttribute("user");
System.out.println(">>>>>>>>>>>>>>>>>>"+ServletActionContext.getRequest().getRealPath("/upload"));
System.out.println("tmpFile>>>>>>>"+file);
for(int i=0;i<file.size();i++){
InputStream is=new FileInputStream(file.get(i));
//String root=this._resourcePath+"user/";
String root=this._resourcePath+"user/";
File destFile=new File(root,this.getFileFileName().get(i));
OutputStream os=new FileOutputStream(destFile);
byte[] buffer=new byte[1024*1024*5];
int length=0;
while((length=is.read(buffer))>0){
os.write(buffer, 0, length);
}
/*Photo p = new Photo();
p.setAddTime(UtilTools.getDate());
p.setUploaderId(1);
p.setPhotoName((this.getTmpFileFileName().get(i)));
p.setDescribes(po.getDescribes());
userPhotoService.addPhoto(p);*/
is.close();
os.close();
}

msg="用户上传图片成功!";
return SUCCESS;
}


private String msg;
private Photo po;
private List<File> file;//用户上传的文件
private List<String> fileFileName;//后缀是固定的,struts2自动将值填充
private List<String> fileContentType;
public Photo getPo() {
return po;
}
public List<File> getFile() {
return file;
}
public void setFile(List<File> file) {
this.file = file;
}
public List<String> getFileFileName() {
return fileFileName;
}
public void setFileFileName(List<String> fileFileName) {
this.fileFileName = fileFileName;
}
public List<String> getFileContentType() {
return fileContentType;
}
public void setFileContentType(List<String> fileContentType) {
this.fileContentType = fileContentType;
}
public void setPo(Photo po) {
this.po = po;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}

}
结果包空指针~~~~
java.lang.NullPointerException
chinaas.member.action.userPhoto.UserPhotoAction.uploadPhoto(UserPhotoAction.java:46)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:440)
com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:279)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:93)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:306)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:468)
org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:76)


...全文

相关推荐

评论 26
收藏 已收藏
分享
返回顶部
l3eta28
Java EE 67,535 成员 225,843 内容