标签:file
首先引入文件jquery.js,jquery.form.js,jqueryMultiFile.js
JSP页面中增加:
$(document).ready(function(){
$(‘#myfile‘).MultiFile({
accept:‘jpg|png|bmp|gif‘,
max:10,
STRING: {
remove:‘移除‘,
selected:‘已经选择了: $file‘,
denied:‘对不起,不支持: $ext!‘,
duplicate:‘文件重复:\n$file!‘
},
autoIntercept:[‘ajaxForm‘]
});
}
$("#editRoomForm").ajaxForm({
url : "<%=basePath%>/smart/zone/updateRoom",
dataType : "json",
success:function(data){
if(data == -1){
},
error:function() {
}
});
Form中增加
<inputtype="file" name="myfile" id="myfile" ></input>
修改类型:enctype="multipart/form-data"
Java中
@RequestMapping(value = "/updateRoom")
@ResponseBody
public int updateRoom(@ModelAttribute("editRoomForm") MeetingRoomDTO room, String[] editUserGroupSelected,
@RequestParam MultipartFile[] myfile, HttpServletRequest request, HttpSession session) {
try {
ZoneDTO zone = zoneService.getZoneById(room.getZoneID());
if (myfile != null && myfile.length > 0) {
// 图片存储路径
String path = request.getRealPath("/") + "resources\\images\\room\\imageName\\";
// 数据库存储路径
String dbpath = "resources/images/room/imageName/";
// 图片名称
String imageName = "";
String pathName = "";
for (MultipartFile file : myfile) {
String fileName = file.getOriginalFilename();
Random r = new Random();
Long Name = System.currentTimeMillis();
Name = Name + r.nextInt(900000) + 100000;
try {
FileUtils.copyInputStreamToFile(file.getInputStream(), new File(path + zone.getId() + "_"
+ Name + fileName.substring(fileName.indexOf("."))));
} catch (IOException e) {
LOGGER.error("会议室实景图片上传失败" + e);
e.printStackTrace();
}
imageName += zone.getId() + "_" + fileName + ",";
pathName += dbpath + zone.getId() + "_" + Name + fileName.substring(fileName.indexOf(".")) + ",";
}
zone.setNavigationMap(pathName);
zone.setImageName(imageName);
}
if ("".equals(room.getCapacityStr())) {
room.setCapacity(0);
} else {
room.setCapacity(Integer.valueOf(room.getCapacityStr()));
}
room.setAudit(room.getEditAudit());
room.setIsValid(0);
zone.setIsValid(0);
zoneService.updateMeetingRoom(zone, room, editUserGroupSelected);
return 1;
} catch (ZoneExistException e) {
LOGGER.error("会议室已存在", e);
return -2;
} catch (WSMException e) {
// TODO Auto-generated catch block
LOGGER.error("修改会议室信息失败" + e);
return -3;
}
}
标签:file
原文地址:http://tianjian.blog.51cto.com/3549910/1666002