1.html中的代码:
Note: 记得设置form的 enctype="multipart/form-data"
<div id="tb">
<form id="fm" method="post" enctype="multipart/form-data">
<input style="width: 180px;" id="assetphotourl" type="file" name="assetphotourl" multiple/>
<a href="#" class="easyui-linkbutton" iconCls="icon-ok" onclick="AssetPhotosAdd($('#assetnum1').val())">添加</a>
</form>
</div>
2.js代码:
Note:如果文件不为空则将form表单提交。
function AssetPhotosAdd(assetnum) {
var files = document.getElementById("assetphotourl").files;
var length = files.length;
if(length != 0){
$("#fm5").form("submit", {
type:"post", //提交方式
url:getRealPath()+'/AssetPhotos/assetPhotosUpload/'+assetnum, //请求url
success:function(data)
{ //提交成功的回调函数
$.messager.alert("提示","附件上传成功","info");
},
error:function () {
$.messager.alert("提示","附件上传失败","info");
}
});
}
}
3.所请求的Controller层代码:
@ResponseBody
@RequestMapping("/assetPhotosUpload/{assetnum}")
/**
* @Description :资产附件上传功能
* @author : bjh
* @param : [assetnum, assetphotourl, request]
* @return : java.lang.Boolean
* @exception :
* @date : 2018/8/11 9:57
*/
public Boolean assetPhotosUpload(@PathVariable("assetnum") String assetnum,
@RequestParam(value ="assetphotourl", required = false) CommonsMultipartFile[] assetphotourl,
HttpServletRequest request) {
if(assetphotourl == null){
return false;
}
//上传文件保存位置
String pathString = request.getSession().getServletContext().getRealPath("/View/Resourse/AssetPhotos");
//判断文件夹是否存在
File file=new File(pathString);
if(!file.exists() && !file.isDirectory()) {
//创建文件夹
file.mkdirs();
}
//批量上传
for(int i = 0;i<assetphotourl.length;i++) {
if(!assetphotourl[i].getOriginalFilename().isEmpty()){//获取文件名
//避免出现重复文件名,采用UUID生成随机文件名
String fileName = assetphotourl[i].getOriginalFilename();
int indexOf = fileName.lastIndexOf(".");
String substring = fileName.substring(indexOf);
String path = pathString+"\\"+ UUID.randomUUID().toString().replaceAll("-","")+substring;
File newFile=new File(path);
try {
//上传资产附件图片
assetphotourl[i].transferTo(newFile);
assetPhotosService.assetPhotosAdd(assetnum,path);
} catch (IllegalStateException | IOException e) {
e.printStackTrace();
}
}else if(assetphotourl.length == 1) {
return false;
}
}
//返回上传成功
return true;
}
4.Service中的代码:
@Override
/**
* @Description :将资产附件进行添加
* @author : bjh
* @param : [assetnum, path]
* @return : void
* @exception :
* @date : 2018/8/11 9:57
*/
public void assetPhotosAdd(String assetnum, String path) {
int index = path.indexOf("\\View\\Resourse\\AssetPhotos");
String assetPhotosURL = path.substring(index);
assetPhotosDao.assetPhotosAdd(assetnum, assetPhotosURL);
}
5.Dao层代码:(采用Hibernate框架)
@Override
/**
* @Description :资产附件的添加
* @author : bjh
* @param : [assetnum, assetPhotosURL]
* @return : void
* @exception :
* @date : 2018/8/11 10:06
*/
public void assetPhotosAdd(String assetnum, String assetPhotosURL) {
Session session = getSession();
Transaction tx = session.beginTransaction();
try{
Assetphotos assetphotos = new Assetphotos();
assetphotos.setAssetnum(assetnum);
assetphotos.setAssetphotourl(assetPhotosURL);
assetphotos.setAssetphotoremarks("无");
session.save(assetphotos);
tx.commit();
}catch (Exception e){
tx.rollback();
e.printStackTrace();
}finally {
close(session);
}
}
6.图片上传成功,且成功将其在服务器端的路径保存到了数据库中。