SSH+easyUI实现文件上传
2015-09-14 20:04
441 查看
step1.思路
参考文献:http://wenku.baidu.com/view/3f4ce83e580216fc700afdd7.html
step 2.引包
1、commons-io-2.0.1.jar
2、commons-fileupload-1.2.2.jar
step3.easyUI前端
<form id="registerForm" method="post" action="register_assureUI.action" enctype="multipart/form-data"><table>
<tr>
<td style="background-color: #EFEFEF;">
<font>营业执照:</font>
</td>
<td>
<input class="easyui-filebox" name="source" id="source_sign" style="width:100%"/>
</td>
</tr>
</table>
</form>
注意:此处运用了filebox组件,然后对于表单的上传格式采用了enctype="multipart/form-data",从而数据上传过后文件是以二进制文件的形式提交过去的。
step4.model
由于本例只需要完成文件夹之间的复制,而不是将文件存入数据库。所有定义pageModel即可:
public class UpLoad implements Serializable {
private File source;
//...getter()和setter()
}
当表单提交到action中是会被封装为File类型
step5.baseAction
/*======================保存营业执照图片的字段==================*//**
* 以源文件类型来保存上传的文件,并返回为文件存储的全路径
* @param upload
* @param uploadFileName
* @param key
* @return imageName 上传图片的名字
* @throws IOException
*/
protected String saveFile(String key, File upload, String uploadFileName)
throws IOException {
/*生成15位随机数作为图片名imageName*/
StringBuffer image=new StringBuffer();
for(int i=0;i<randomLength;i++){
image.append((int)Math.round((Math.random())*10));
}
String imageName=image.toString();
if (upload != null&&upload.isFile()) {
//其实如果项目不需要的话没必要用配置文件区路径,可以自己直接获取
// 1.设置保存上传的文件全部路径
String uploadPath = ServletActionContext.getServletContext()
.getRealPath("/") + key;
/*String uploadPath="F:\\work\\trunk\\src\\myDDI\\WebRoot\\uploadImage\\";*/
// 判断上传文件名是否有扩展名
int index = uploadFileName.lastIndexOf('.');
if (index != -1) {
imageName = imageName + uploadFileName.substring(index);
/*imageName = imageName + ".jpg";*/
}
// 如果文件夹不存在,就创建
File dir = new File(uploadPath);
if (!dir.exists()) {
dir.mkdirs();
}
File destFile = new File(uploadPath, imageName);
FileUtils.copyFile(upload, destFile);
return imageName;
}
return null;
}
/*======================保存营业执照图片的字段==================*/
step6.Action
/*上传图片*/
try{
String key = "upLoadImage/";
bl_path=this.saveFile(key,model.getSource(), model.getSource().getName()); //调用basicAction方法
} catch (Exception e) {
e.printStackTrace();
}
相关文章推荐
- Standard Series Values in a Decade for Resistances and Capacitances E24 E48 E96
- UIday1501:NSString的几种常用方法
- Duilib中Webbrowser事件完善,使其支持判断页面加载完毕
- poj3913 Gnome Sequencing
- Android 中使用rawquery方法查询指定数据的一些方法总结
- Redrain duilib中的名字空间问题
- duilib lib库静态变量初始化问题
- 处理美国时间 Tue May 31 17:46:55 +0800 2011
- What is an intuitive explanation of the relation between PCA and SVD?
- JSP 请求对象 request
- HDU1159Common Subsequence DP
- JAVA-有趣的GuessBirthday
- 可以输出自己的源程序代码(quine)
- Android SystemUI中HOME key的处理
- IOS开发模块总结(二)后台运行程序(2)Task completion-UIBackgroundTaskIdentifier
- 【EGit】The current branch is not configured for pull No value for key branch.master.merge found in config
- UITableView headView固定效果实现
- 使用 Arduino 和 LM35 温度传感器监测温度
- quick2.2与quick3.3的区别(4)
- could not find implicit value for parameter extractor 问题的解决