上传到一个文件夹下
在struts2.0下
private File upload;//上传文件域内容,比如图片
private String uploadFileName;//上传文件名,图片的名字,get,set方法
File file=new File(相对路径,名字)
FileUtils.copyFile(upload, file);
jsp页面有文件上传这个域和普通的文字域
调试,如果带有图片不报错,不带图片老是报空指针异常,也就是下面的“名字”那个地方
File file=new File(相对路径,名字)
在struts2.0下
private File upload;//上传文件域内容,比如图片
private String uploadFileName;//上传文件名,图片的名字,get,set方法
File file=new File(相对路径,名字)
FileUtils.copyFile(upload, file);
jsp页面有文件上传这个域和普通的文字域
调试,如果带有图片不报错,不带图片老是报空指针异常,也就是下面的“名字”那个地方
File file=new File(相对路径,名字)
解决方案 »
- struts2+hibernate3+spring2出现java.lang.NullPointerException
- 如何测试程序是否线程安全
- 高分请教一个简单问题
- 菜鸟问题:怎么样输出类似1+2=3这样的式子?
- 妹子求助一道编程题,想了半天完全没思路
- 高手请指点指点新手啊
- 怎样完全手工部署一个servlet?看看我错在哪,解决就送100分。
- weblogic建立的server,用startWebLogic.cmd启动后要求输入用户名和密码,system/security输入后自动跳出
- 100分求Core J2EE Patterns(Best Practices and Design Strategies) PDF
- 在weblogic怎么判断DataSource配置是否成功-在线
- 天有不测风云
- 用户名,手机等不能重复是放在一个方法里面处理还是分别处理?
private String fileFileName;
private String fileContextType; @SuppressWarnings("deprecation")
@Override
public String execute() throws Exception {
InputStream is = new FileInputStream(file);
String path = ServletActionContext.getRequest().getRealPath("/upload");
File myfile = new File(path,this.fileFileName);
OutputStream os = new FileOutputStream(myfile);
byte[] buffer =new byte[400];
int length = 0;
while((length = is.read(buffer)) > 0){
os.write(buffer,0,length);
}
os.close();
is.close();
return SUCCESS;
}
private String uploadFileName;//上传文件名
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
@Override
public String execute() throws Exception {
String dstPath=ServletActionContext.getServletContext().getRealPath("/UserImage");
File dstFile=new File(dstPath,uploadFileName);
FileUtils.copyFile(upload, dstFile);
VO.setFile(图片名字);
DAO.insertUser(urvo);
return success
}
需要在D:\tomcat-6.0.20\webapps\dmg下建一个文件夹UserImagestruts.xml配置
<struts>
<package name="struts-job" extends="struts-default">
<action name="UserRegisterAction" class="com.dmg.action.UserRegisterAction">
<param name="savePath">/upload</param>
<result name="success" type="redirectAction">UserRegisterAction!getUserById</result>
</action>
</package>
</struts>