请教个问题~谢谢啦!
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)
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)
...全文
相关推荐
发现和明确收纳问题!~!
发现和明确收纳问题!~!
asp中 select top 问题!~
sql=”select top 8 * from Musiclist where “& sql2 &” order by hits desc” 选择前八条无效!~ 显示出来的是所有新闻!~为什么? 改为下面的就可以 sql=”select top 8 * from Musiclist where “& sql2 &” order by hits,ID desc”注意id是自动增加的
serv_u 14.0.1.0 白金破解,个人亲测没问题!~
个人安装正常使用的软件哦。给大家分享了。 破解的时候要停掉服务,将补丁放入安装目录重启即可。
百度新闻插件 for Discuz! x1.5~2
百度新闻插件适用于Discuz! X1.5和Discuz! X2,与百度新闻同步,不需要更新,也不需要采集!小偷源码类型!
mac 安装omyzsh后不执行~/.bash_profile、~/.bashrc的完美解决办法
mac 安装 omyzsh 后, terminal瞬间逼格飙升,但是,terminal init的时候并不会执行~/.bash_profile、~/.bashrc等脚本了,什么原因呢?下面小编给大家分享解决办法,一起看看吧


