struts2 图片上传 详解
来源:互联网 发布:手机检索专利软件 编辑:程序博客网 时间:2024/06/05 16:26
struts2 的图片上传
(很少写博客,主要是不太明白怎么写出来才能让大家看的明白,so大家有什么意见就直说吧!!!)
这谁demo只是一个非常简单的 基于 struts2 的图片上传 (页面 -> action )就是这样
页面:
<form action="actionName" enctype="multipart/form-data" method="post" >图片:<input type="file" name="productImg" multiple="true" id="productImg" accept="image/*"><input type="submit" value="上传" /></form>注意:提交方式一点要post 要加上enctype="multipart/form-data" 图片的multiple=true 是可以选择多个文件 accrpt 限制了选择文件的类型
struts配置:
<action name="batUploadImg" class="xxx" method="batUploadImg"><param name="path">/upLoad</param><result>XXX</result></action>这里就不多讲了 XXX代表随便你怎么写
java代码:
首先声明图片 的一些变量
private File productImg; // 上传的文件private String productImgFileName; // 文件名称private String productImgContentType; // 文件类型private String path;
注意他们的名称格式 这个很重要 还有 get set方法
方法:
public String batUploadImg() {//根据今天日期yyyyMMdd得出个字符串String path = DateUtils.format(new Date(), "yyyyMMdd");//根据path获取绝对路径String realpath = ServletActionContext.getServletContext().getRealPath(getPath())+ "/"+ path + "/";//如果图片不为空if (productImg != null) { //获取个随机数int sun = (int) (Math.random() * 1000);//文件名 拼接来的productImgFileName = DateUtils.formats(new Date()) + sun + ".jpg";//创建文件 File savefile = new File(new File(realpath), poductImgFileName);//判断你需要保存图片的路径是否存在if (!savefile.getParentFile().exists())//如果不存在就新建savefile.getParentFile().mkdirs();try {//根据org.apache.commons.io.FileUtils 复制文件 到保存路径FileUtils.copyFile(productImg, savefile);} catch (IOException e) {return ERROR;}//获取到返回值 是路径+文件名 String result = getSavePath() + "/" + path + "/" + productImgFileName;}return SUCCESS;}
可能是我复制会出现点小问题 SO 请大家多多照顾
===============================================================================
图片的批量上传呢 其实和上面代码相差不大 就是把变量全部用数组 或者集合保存
然后在方法体里面 是循环保存
好了 !!!
0 0
- struts2 图片上传 详解
- struts2 上传 图片
- struts2上传图片
- struts2上传图片
- Struts2上传图片
- struts2图片批量上传
- struts2上传图片
- struts2批量上传图片
- struts2 不能上传图片
- struts2 上传图片
- Struts2上传图片
- struts2上传图片
- struts2上传图片
- struts2 上传图片
- struts2 spring4 图片上传
- struts2图片上传
- struts2 ajax图片上传
- Struts2上传图片实例
- QGraphicsView框架容易忽视的坐标问题-“画布”QgraphicsScene到底放在了哪儿?
- ArrayList、Vector
- JQuery this和$(this)的区别及获取$(this)子元素对象的方法
- 185.User SCOTT executes the following command on the EMP table but has not issued COMMIT, ROLLBACK,
- cocoapods:常见错误总结+Mac OS 10.11
- struts2 图片上传 详解
- 生成一个集合的所有子集 Subset
- 关于解决android studio各种疑难杂症系列---- 工程的geadle版本跟当前环境不匹配
- TextView去掉自身自带的内边距
- [leetcode] 215. Kth Largest Element in an Array
- jquery 对 Json 的各种遍历
- 186.User A executes the following command to drop a large table in your database: SQL> DROP TABLE tr
- POJ 1141 Brackets Sequence(区间DP)
- MATLAT2012B安装license checkout failed