struts2上传文件
来源:互联网 发布:世纪通信软件 编辑:程序博客网 时间:2024/04/30 11:31
单个文件上传:
1、加入jar包commons-fileupload-1.2.1.jar、commons-io-1.3.2.jar
2、设置form表单如下:
<form enctype="multipart/form-data" action="${pageContext.request.contextPath}/***.action" method="post">
<imput type="file" name="image">
</form>
3、在Action类中添加以下属性:
private File image;//得到上传的文件
private String imageContentType;//得到文件的类型
private String imageFileName;//得到文件的名称
...
public String upload() throws Exception{
String realpath = ServletActionContext.getServletContext().getRealPath("/images");
if(image!=null){
File savefile = new File(new File(realpath),imageFileName);
if(!savefile.getParentFile().exists()) savefile.getParentFile().mkdirs();
FileUtils.copyFile(image,savefile);
ActionContext.getContext().put("message","上传成功");
}
return "success";
}
或
String realpath = ServletActionContext.getServletContext().getRealPath("/images");
File file = new File(realpath);
if(!file.exists()) file.mkdirs();
FileUtils.copyFile(image,new File(file,imageFileName));
当有多个文件上传时:
文件1:<input type="file" name="image"><br/>
文件2:<input type="file" name="image"><br/>
文件3:<input type="file" name="image"><br/>
在Action类中:
private File[] image;
private String[] imageFileName;
...
public String upload() throws Exception{
String realpath = ServletActionContext.getServletContext().getRealPath("/images");
if(image!=null){
File savedir = new File(realpath);
if(!savedir.exists()) savedir.mkdirs();
for(int i = 0 ; i<image.length ; i++){
File savefile = new File(savedir, imageFileName[i]);
FileUtils.copyFile(image[i], savefile);
}
ActionContext.getContext().put("message", "上传成功");
}
return "success";
}
- struts2上传文件(三) struts2上传文件
- [Struts2]Struts2的文件上传
- Struts2的文件上传
- Struts2文件上传
- Struts2 上传文件实例
- struts2文件上传
- struts2文件上传
- Struts2的文件上传
- struts2-上传文件(1)
- struts2-上传文件(2)
- Struts2上传文件示例
- struts2文件上传
- Struts2实现文件上传
- struts2文件上传
- Struts2 文件上传
- ext + struts2 上传文件
- Struts2 文件上传
- Struts2的文件上传
- request、session、application范围访问添加属性
- oralce 中查询锁表记录以及解锁
- Android 4.1 - 如何使用systrace做性能分析
- oracle COALESCE 函数
- AVIFile
- struts2上传文件
- 在网站项目中添加“模块”
- SDUT——选夫婿2
- 第三周任务3:时间类
- struts2自定义拦截器
- 压力测试工具apache-ab讲解
- Java中Junit简单测试
- hibernate配置文件:hibernate.cfg.xml常用属性
- struts2校验方法