public Map<String,String> upload(Manuscript manuscript,MultipartFile manuscript_file,HttpServletRequest request, HttpServletResponse response ) throws IOException{ Map<String,String> ret = new HashMap<String,String>(); if(StringUtils.isEmpty(manuscript.getAuthor_name())) { ret.put("type","error"); ret.put("msg","作者姓名不能为空!"); return ret; } String author_name = request.getSession().getAttribute("author_name").toString(); if(!manuscript.getAuthor_name().equals(author_name)) { ret.put("type","error"); ret.put("msg","请输入正确的姓名!"); return ret; } if(StringUtils.isEmpty(manuscript.getTitle())) { ret.put("type","error"); ret.put("msg","稿件标题不能为空!"); return ret; } if(StringUtils.isEmpty(manuscript.getSummary())) { ret.put("type","error"); ret.put("msg","稿件摘要不能为空!"); return ret; } if(manuscript_file==null) { ret.put("type","error"); ret.put("msg","文件没有选择"); return ret; } if(manuscript_file.getSize() > 10485760){ //文件没有选择 ret.put("type", "error"); ret.put("msg", "文件大小超过10M!"); return ret; } String suffix = manuscript_file.getOriginalFilename().substring(manuscript_file.getOriginalFilename().lastIndexOf(".")+1,manuscript_file.getOriginalFilename().length()); if(!"docx,doc,pdf".contains(suffix)) { ret.put("type","error"); ret.put("msg","文件格式不正确!"); return ret; } String savePath = request.getServletContext().getRealPath("/")+"\upload\"; File savePathFile = new File(savePath); if(!savePathFile.exists()) { savePathFile.mkdir(); } String fileName = new Date().getTime()+"."+suffix; ret.put("src",request.getServletContext().getContextPath() + "/upload/" + fileName); manuscript_file.transferTo(new File(savePath+fileName)); manuscript.setFilePath(ret.get("src")); SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); String format = sf.format(new Date()); manuscript.setSubmit_time(format); manuscript.setId((Integer)request.getSession().getAttribute("id")); manuscript.setState("待审核"); if(authorService.add(manuscript)<=0) { ret.put("type","error"); ret.put("msg","稿件添加失败!"); return ret; } ret.put("type", "success"); ret.put("msg", "文件上传成功!"); return ret; }这段代码什么意思
时间: 2023-06-06 08:06:33 浏览: 133
使用io实现文件上传
这是一个Java语言编写的上传文件的函数,函数需要传入一个Manuscript对象、一个MultipartFile对象、一个HttpServletRequest对象和一个HttpServletResponse对象。函数使用了HashMap来返回处理结果,同时进行了参数校验和文件存储操作。如果参数不符合要求,函数会返回错误提示。如果文件大于10M、不是docx、doc或pdf格式,函数也会返回错误提示。最后,将稿件添加到数据库中,并返回成功提示。
阅读全文