struts2学习之文件上传
来源:互联网 发布:怎么在淘宝卖视频会员 编辑:程序博客网 时间:2024/05/23 01:29
上传html页面:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>写心情</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body style="font-size:14px;"> <h2>写微博 <a href="index.jsp">返回首页</a></h2> <form action="upload" method="post" enctype="multipart/form-data"> <textarea name="sayMessage" rows="4" cols="50"></textarea><br> 发文件<input name="upload" type="file"/><input type="submit" value="发微博"> </form> </body></html>
上传action:
package com.gifer.action;import java.io.File;import java.io.IOException;import java.util.List;import org.apache.commons.io.FileUtils;import org.apache.log4j.Logger;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class FileUpLoadAction extends ActionSupport {private static final long serialVersionUID = -1232100382633301276L;private static final Logger log = Logger.getLogger(FileUpLoadAction.class);// 微博内容private String sayMessage;// 上传多个文件的集合文本private List<File> upload;// 多个上传文件的类型集合private List<String> uploadContextType;// 多个上传文件的文件名集合private List<String> uploadFileName;// 文件存放路径根目录private String savePath;public String getSayMessage() {return sayMessage;}public void setSayMessage(String sayMessage) {this.sayMessage = sayMessage;}public List<File> getUpload() {return upload;}public void setUpload(List<File> upload) {this.upload = upload;}public List<String> getUploadContextType() {return uploadContextType;}public void setUploadContextType(List<String> uploadContextType) {this.uploadContextType = uploadContextType;}public List<String> getUploadFileName() {return uploadFileName;}public void setUploadFileName(List<String> uploadFileName) {this.uploadFileName = uploadFileName;}public void setSavePath(String savePath) {this.savePath = savePath;}@Overridepublic String execute() throws Exception {// 把上传的文件放到指定的路径下// savePath = ServletActionContext.getServletContext().getRealPath(// "/WEB-INF/uploadfiles");savePath = ServletActionContext.getServletContext().getRealPath(savePath);// 写到指定的路径中File file = new File(savePath);// 如果指定的路径没有就创建if (!file.exists()) {file.mkdirs();}// 把得到的文件的集合通过循环的方式读取并放在指定的路径下for (int i = 0; i < upload.size(); i++) {try {// list集合通过get(i)的方式来获取索引FileUtils.copyFile(upload.get(i),new File(file, uploadFileName.get(i)));} catch (IOException e) {log.error(e.getMessage(), e);}}return SUCCESS;}}
struts.xml配置:
<action name="upload" class="myFileUploadAction"><result name="success">/index.jsp</result><result name="input">/saymessage.html</result><!--struts2默认只能上传2M文件,类型不限。 配置拦截器来限制上传文件的类型和大小 --><interceptor-ref name="fileUpload"><!-- 限定文件上传类型 --><param name="allowedTypes">image/png,image/x-png,image/jpg,image/pjpeg,image/bmp,image/gif</param><!-- maximumSize指每个上传文件的大小上限,单位为:byte,这里设为30M,该值不能大于struts.properties中的struts.multipart.maxSize总大小--><param name="maximumSize">31457280</param><!-- 文件上传路径 --><param name="savePath">E:\\UploadFileDemo</param> </interceptor-ref><!-- 默认拦截器 --><interceptor-ref name="defaultStack"></interceptor-ref></action>
<interceptor-ref name="fileUpload">中name="fileUpload"的名字是固定的。
1、在struts.xml的中添加<constant name="struts.multipart.maxSize" value="209715200" />常量设置
2、在struts.xml同目录下,创建一个struts.properties文件,内容如下:
#文件上传保存路径struts.multipart.saveDir=E\:\\\\UploadFileDemo#文件上传大小上限(单位:byte)这里是200Mstruts.multipart.maxSize=209715200#更改上传文件类型不允许的提示信息 struts.messages.error.content.type.not.allowed=\u6587\u4EF6\u4E0A\u4F20\u5931\u8D25\uFF0C\u60A8\u4E0A\u4F20\u7684\u6587\u4EF6\u7C7B\u578B\u4E0D\u88AB\u5141\u8BB8#更改上传文件太大的提示信息 struts.messages.error.file.too.large=\u6587\u4EF6\u4E0A\u4F20\u5931\u8D25\uFF0C\u60A8\u4E0A\u4F20\u7684\u6587\u4EF6\u5927\u5C0F\u8D85\u8FC7\u4E0A\u9650\uFF0830M\uFF09#文件上传其它错误信息 struts.messages.error.uploading=\u6587\u4EF6\u4E0A\u4F20\u5931\u8D25\uFF0C\u4E0A\u4F20\u7A0B\u5E8F\u53D1\u751F\u5185\u90E8\u9519\u8BEF
我偿试使用第一种方法,结果运行没有效果(不知道是否是什么地方设置不对?)
使用第二种方法就OK了。
另外,在配置完struts.properties后,运行发现提示错误,说struts.multipart.saveDir 为空,所以我就加了这个配置。
<param name="allowedTypes">image/png,image/x-png,image/jpg,image/pjpeg,image/bmp,image/gif</param>
对于文件上传类型限制补充2点:
1、在使用image/x-png,上传png文件时,提示文件类型不允许,后来加入image/png就好了。
2、对于上传jpeg文件,需要image/pjpeg。
- struts2学习之文件上传
- struts2学习之---文件上传
- struts2学习笔记之文件上传
- Struts2学习笔记之文件上传
- Struts2学习笔记之文件上传
- struts2学习笔记之十:文件上传
- (Struts2学习篇) Struts2文件处理之文件上传
- 《struts2权威指南》学习笔记之struts2文件上传
- 《struts2权威指南》学习笔记之struts2文件上传
- struts2之文件上传
- struts2之文件上传
- Struts2之上传文件
- Struts2之文件上传
- Struts2之文件上传
- Struts2之文件上传
- Struts2之文件上传
- Struts2之文件上传
- Struts2之文件上传
- Hibernate3.6中用Annotation来实现实体类与数据库表的映射关系
- 實參 形參 指針 四種寫法
- 临时表和表变量
- 单调栈总结 来自poj
- Oracle创建简单的触发器练习
- struts2学习之文件上传
- google-logo-20120501
- 一台手机玩转你所有的社交生活(Android版)
- css中选择器的使用
- Myeclipse优化
- SQLServer 2008 R2在对象资源管理器中隐藏系统对象
- 3. Scilab程序设计
- JSON.parse(json)与eval的改变
- 实战:oracle 11g r2 LogMiner