struts2教程:14、文件上传和多文件上传
来源:互联网 发布:小女孩白丝袜淘宝 编辑:程序博客网 时间:2024/06/15 11:28
文件上传
第一步:在WEB-INF/lib下加入commons-fileupload-1.2.1.jar、commons-io-1.3.2.jar。这两个文件可以从http://commons.apache.org/下载。
第二步:把form表的enctype设置为:“multipart/form-data“,如下:
<form enctype="multipart/form-data" action="${pageContext.request.contextPath}/xxx.action" method="post">
<input type="file" name="uploadImage">
</form>
第三步:在Action类中添加以下属性,属性红色部分对应于表单中文件字段的名称:
public class HelloWorldAction{
private File uploadImage;//得到上传的文件
private String uploadImageContentType;//得到文件的类型
private String uploadImageFileName;//得到文件的名称
//这里略省了属性的getter/setter方法
public String upload() throws Exception{
String realpath =ServletActionContext.getServletContext().getRealPath("/images");
File file = new File(realpath);
if(!file.exists())file.mkdirs();
FileUtils.copyFile(uploadImage, new File(file,uploadImageFileName));
return "success";
}
}
多文件上传
第一步:在WEB-INF/lib下加入commons-fileupload-1.2.1.jar、commons-io-1.3.2.jar。这两个文件可以从http://commons.apache.org/下载。
第二步:把form表的enctype设置为:“multipart/form-data“,如下:
<form enctype="multipart/form-data" action="${pageContext.request.contextPath}/xxx.action" method="post">
<input type="file" name="uploadImages">
<input type="file" name="uploadImages">
</form>
第三步:在Action类中添加以下属性,属性红色部分对应于表单中文件字段的名称:
public class HelloWorldAction{
private File[] uploadImages;//得到上传的文件
private String[] uploadImagesContentType;//得到文件的类型
private String[] uploadImagesFileName;//得到文件的名称
//这里略省了属性的getter/setter方法
public String upload() throws Exception{
String realpath =ServletActionContext.getServletContext().getRealPath("/images");
File file = new File(realpath);
if(!file.exists())file.mkdirs();
for(inti=0 ;i<uploadImages.length;i++){ File uploadImage = uploadImages[i];
FileUtils.copyFile(uploadImage, new File(file,uploadImagesFileName[i]));
}
return "success";
}}
- struts2教程:14、文件上传和多文件上传
- Struts2教程:上传任意多个文件
- Struts2.0学习笔记---上传文件和多文件上传
- struts2-14文件上传
- Struts2多文件上传
- struts2多文件上传
- struts2多文件上传
- struts2 多文件上传
- struts2多文件上传
- struts2多文件上传
- struts2多文件上传
- struts2多文件上传
- Struts2多文件上传
- Struts2多文件上传
- Struts2 多文件上传
- struts2多文件上传
- struts2 多文件上传
- Struts2多文件上传
- C#方法(1)
- shell命令与变量
- Infoworld编程领域的十一大发展趋势
- VS2008安装失败解决案例一则
- vs2008安装Qt,提示丢失QtGuid4.dll的解决方法。
- struts2教程:14、文件上传和多文件上传
- 工控界面设计收集
- 图的应用---知行合一,连连看小游戏
- 关于 asp.net的网站图片盗链、 、 、
- 杂记之C语言之结构(二)
- GIT和SVN区别
- 3*3魔法阵
- Android下TabActivity的使用(1)
- STM32之工程模板