struts2中的文件上传
来源:互联网 发布:编程语言的发展趋势 编辑:程序博客网 时间:2024/05/16 20:31
一:导入需要的jar包commons-fileupload和commons-io必不可少
二:编写页面,以上传照片为例
<form action="file_upload.action" method="post" enctype="multipart/form-data">请选择要上传的照片:<input type="file" name="images"/> <br><input type="submit" value="提交"/> </form>
注意:文件上传时表单内一定要有enctype,并且enctype一定为multipart/form-data
三:编写action类
package com.action;import java.io.File;import java.text.SimpleDateFormat;import java.util.Date;import org.apache.commons.io.FileUtils;import org.apache.struts2.ServletActionContext;public class FileAction {//file对象,必须与file控件的name相同private File images;//上传的文件的名称,格式:控件名FileNameprivate String imagesFileName;//上传文件的类型 image/png,格式:控件名ContentType (可选)private String imagesContentType;//每天创建一个文件夹private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");public File getImages() {return images;}public void setImages(File images) {this.images = images;}public String getImagesFileName() {return imagesFileName;}public void setImagesFileName(String imagesFileName) {this.imagesFileName = imagesFileName;}public String getImagesContentType() {return imagesContentType;}public void setImagesContentType(String imagesContentType) {this.imagesContentType = imagesContentType;}/** * 文件的上传 * 如果是多文件上传的话,则需要把上面的3个属性都变成数组,然后循环copy. * @return * @throws Exception */public String upload() throws Exception{//在服务器上建立文件夹images,path为该文件夹在服务器上的路径String path = ServletActionContext.getServletContext().getRealPath("/images");System.out.println(path);//以当前系统时间在path下创建一个文件夹(根据上面的格式为每天创建一个文件夹)File pf = new File(path, format.format(new Date()));//如果该文件夹不存在,则创建该文件夹if(!pf.exists()){pf.mkdir(); }//让名称不重复Long time = System.currentTimeMillis();//获取图片的后缀String ext = imagesFileName.substring(imagesFileName.lastIndexOf("."));String name = time+ext;//把上传的文件写入path下的images文件夹中File file = new File(pf,name);FileUtils.copyFile(images, file);return "success";}}
四:在struts.xml中配置action
<struts><package name="file" namespace="/" extends="struts-default"><action name="file_*" method="{1}" class="com.action.FileAction"><result>show.jsp</result></action></package></struts>
五:在web.xml文件中配置struts2的过滤器
<filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
六:结果
0 0
- Struts2中的文件上传
- struts2中的文件上传
- struts2中的上传文件
- Struts2中的文件上传
- Struts2中的文件上传
- struts2中的文件上传
- Struts2中的文件上传
- struts2中的文件上传
- 使用struts2中的文件上传
- struts2中的文件上传实现
- struts2中的文件上传与下载
- struts2中的文件上传和下载
- Struts2中的文件上传与下载
- Struts2中的文件上传和文件下载
- struts2上传文件(三) struts2上传文件
- [Struts2]Struts2的文件上传
- struts2文件上传中的struts.multipart.saveDir提示信息
- struts2文件上传中的struts.multipart.saveDir提示信息
- 隐式的类类型转换
- 一道DP
- java反射
- Unity 加载 失败 解决办法
- MyEclipse10.7下配置tomcat6.0.32
- struts2中的文件上传
- 轻松弄清JavaScript中的offset、scroll、client
- WIFI调试
- 翻翻git之---给传统的搜索增添友好动画 JJSearchViewAnim
- 跳台阶
- [leetcode-331]Verify Preorder Serialization of a Binary Tree
- 使用TinyXml对XML进行遍历
- poj2249 Binomial Showdown 求组合数
- 引入unittest和HTMLTestRunner来进行自动化回归