点击上传按钮进行上传附件操作
//上传附件js
function uploadFile(){
$.ajaxFileUpload({
url : "/returnToWork/uploadfile?id=0",
type:"post",
secureuri:false,
fileElementId:"file",
dataType:"JSON",
success : function(data) // 服务器成功响应处理函数
{
var obj = $.parseJSON(data.replace(/<.*?>/ig,""));
$('#file').val('');
var html = "",ids="";
if(obj.status == 200){
$.each(obj.fileBean, function(i,item){
html += ""+item.filename+"";
ids += item.id +",";
});
$("#imgHtml").html(html);
$("#fileids").val(ids);
}else{
layer.alert(obj.msg);
}
},
error : function(data)// 服务器响应失败处理函数
{
var obj = $.parseJSON(data.replace(/<.*?>/ig,""));
layer.alert(obj.msg);
}
});
}
调用后台url
@RequestMapping(value = "/returnToWork/uploadfile", method = RequestMethod.POST, consumes = "multipart/*")
public ResponseEntity uploadfile(MultipartHttpServletRequest request, String id) throws FileUploadException,IOException, BiffException {
return FileUtils.uploadFile1(request, FileMessage.FILE_SIZES, FileMessage.FILE_GS, "file", FileMessage.FILE_PATH, Accessory.NCOVENTERPRISE_TABLE_NAME,Integer.parseInt(id));
}
//上传附件方法
public final static ResponseEntity uploadFile1(MultipartHttpServletRequest request,Long size,String Gs,
String filename,String path,String table,Integer itemid)
throws IOException{
MultiValueMap map = request.getMultiFileMap();// 为了获取文件,这个类是必须的
UserBelong sysUsers=(UserBelong) request.getSession().getAttribute("USER");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(new MediaType("text", "plain", Charset.forName("UTF-8")));
List list = map.get(filename);// 获取到文件的列表
FileMessage fm = new FileMessage();//提示对象
//验证用户session
if(sysUsers == null) {
fm.setStatus(FileMessage.FILE_STATUS_ERROR);
fm.setMsg("登录用户已过期!");
return new ResponseEntity(fm, headers, HttpStatus.OK);
}
//1.验证文件格式和大小 1K = 102400B
for (MultipartFile mFile : list) {
//判断图片大小
if(mFile.getSize() > size){
fm.setStatus(FileMessage.FILE_STATUS_ERROR);
fm.setMsg("上传文件大小不能超过"+(size/1024/1024) + "M!");
return ResponseEntity.ok(fm);
}
//判断文件格式 1.截取文件格式 字符
String filetype = mFile.getOriginalFilename().substring(mFile.getOriginalFilename().lastIndexOf(".")+1).toUpperCase();
if(Gs.toUpperCase().indexOf(filetype)<0){
fm.setStatus(FileMessage.FILE_STATUS_ERROR);
fm.setMsg("只能上传("+Gs + ")格式的文件!");
return ResponseEntity.ok(fm);
}
}
//2.上传文件值服务器文件夹内
//创建upload文件夹
File file = new File(path);
if(!file.exists()) {
file.mkdirs();
}
//附件保存至集合中
List fileNameList = new ArrayList();
//将图片进行存储上传本地服务器
for (MultipartFile mFile : list) {
String originalFileName= mFile.getOriginalFilename();//获取文件名称
String UUID = java.util.UUID.randomUUID().toString();
FileBean bean = new FileBean();
bean.setFilename(originalFileName);
bean.setUuid(UUID);
byte[] bytes = mFile.getBytes();//获取字节数组
String fileBname = UUID+"."+mFile.getOriginalFilename().substring(mFile.getOriginalFilename().lastIndexOf(".")+1);
bean.setType(mFile.getOriginalFilename().substring(mFile.getOriginalFilename().lastIndexOf(".")+1));
bean.setUuid(fileBname);
bean.setAdduser(sysUsers.getName());
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
bean.setFiledate(sdf.format(d));
String filePath= path+ File.separator+ fileBname;
bean.setFilepath(filePath);
FileOutputStream fos= new FileOutputStream(new File(filePath)); //写出到文件
bean.setSize(mFile.getSize());
fileNameList.add(bean);
fos.write(bytes);
fos.flush();
fos.close();
}
//3.把文件地址保存在数据库中
Object o = ApplicationUtil.getBean("accessoryService");
AccessoryService accessoryService = (AccessoryService) o;
//String FILEUUID = java.util.UUID.randomUUID().toString();
for (FileBean fileBean : fileNameList) {
Accessory bean =new Accessory();
bean.setFilename(fileBean.getFilename());
bean.setNewname(fileBean.getUuid());
bean.setFilePath(PATH+bean.getNewname());
bean.setSize(String.valueOf(fileBean.getSize()));
bean.setTablename(table);
bean.setItemid(itemid);
bean.setFiletype(fileBean.getType());
bean.setCreateuser(sysUsers.getId());
accessoryService.createAccessory(bean);
fileBean.setId(bean.getId());
}
//4.返回上传后的文件信息
fm.setStatus(FileMessage.FILE_STATUS_SUCCESS);
fm.setMsg("文件上传成功!");
fm.setFileBean(fileNameList);
return ResponseEntity.ok(fm);
}
//上传完回显之后的效果