Struts文件上传--2.多文件上传
来源:互联网 发布:银行招数据库管理吗 编辑:程序博客网 时间:2024/05/15 12:46
.多文件上传
步骤如下:
(1)将commons-io-x.x.x.jar包和commons-fileupload-x.x.x.jar包导入到项目的WEB-INF/lib目录下;
(2)把form表单的enctype类型设为multipart/form-data,如下:
<form action="${pageContext.request.contextPath}/uploadImage.do" method="post" enctype="multipart/form-data"><input type="file" name="image"/><br><input type="file" name="image"/><br><input type="submit" value="submit"/></form>
(3)在Action类中添加以下属性:
1)File[] filename:文件名要和表单中提交的name属性相同;
2)String[] filenameContentType:得到文件的类型(文件名+ContentType是固定格式);
3)String[] filenameFileName:得到文件的名称(文件名+FileName是固定格式)。
Action类中需要使用FileUtils.copyFile(srcFile,destFile),用来复制上传的文件到项目中。
示例如下:
struts.xml中关键代码:
<action name="uploadImage" class="com.chen.action.SubmitForm" method="upload"><!-- 为Action的 imagePath属性注入值--><param name="imagePath">/images</param><result name="success">/sayMessage.jsp</result><result name="error">/sayMessage.jsp</result></action>
Action类的代码:
public class SubmitForm {//返回信息public String message=null; //设置上传文件的保存文件路径public String imagePath=null;//文件private File[] image;//文件类型private String[] imageContentType;//文件名private String[] imageFileName;public String upload(){//获取真实路径String realPath=ServletActionContext.getServletContext().getRealPath(imagePath);File file=new File(realPath);if(!file.exists()){file.mkdirs();}try {for(int i=0;i<image.length;i++){FileUtils.copyFile(image[i], new File(file,imageFileName[i]));}message="save OK";return "success";} catch (IOException e) {message="save error";return "error";}}public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}public String getImagePath() {return imagePath;}public void setImagePath(String imagePath) {this.imagePath = imagePath;}public File[] getImage() {return image;}public void setImage(File[] image) {this.image = image;}public String[] getImageContentType() {return imageContentType;}public void setImageContentType(String[] imageContentType) {this.imageContentType = imageContentType;}public String[] getImageFileName() {return imageFileName;}public void setImageFileName(String[] imageFileName) {this.imageFileName = imageFileName;}}
注:通过Struts的常量可以配置上传文件的大小。
<constant name="struts.multipart.maxSize" value="10701096"/>
0 0
- Struts文件上传--2.多文件上传
- struts 多文件上传
- struts多文件上传
- Struts多文件上传
- 2.struts上传多个文件
- struts实现多文件上传
- struts 动态多文件上传
- struts 2多文件上传
- Struts实现多文件上传
- struts多个文件上传。
- struts的多文件上传
- Struts多个文件上传
- Struts的文件上传
- struts文件上传要点
- Struts的文件上传
- struts文件上传要点
- Struts的文件上传
- Struts的文件上传
- 算法提高 12-1三角形
- hdu 2092 整数解
- 基于AsyncTask的带进度条的下载功能Demo实现
- Linux常用命令(附视频教学)
- 商品倒计时打折js实现
- Struts文件上传--2.多文件上传
- PAT B1013. 数素数 (20)
- idea报错Cannot compile Groovy files: no Groovy library is defined for module X'X
- 安卓的几种布局管理器(Layout)各有什么特点
- 注解 Annotation 详解
- 本周计划
- 【趣解】工厂模式之简单工厂、工厂方法模式
- CPU-内存-硬盘-指令集
- CentOS7 更换yum源