mui 通过plus.uploader.createUpload实现多文件上传




	
		
		
		
		
		
	

	
		

编辑新闻

Controller层 

@PostMapping("subNews")
    public ResultData subNews(String title, String content, String type,Integer bigPhoto, String position,HttpServletRequest request){
        try {
            String bigPhoto1 = request.getParameter("bigPhoto");
            System.out.println(bigPhoto1);
            return newsService.subNews(request, title, content, type, bigPhoto, position);
        } catch (IOException e) {
            e.printStackTrace();
            log.error("上传文件出错");
            return null;
        }
    }

Service层

public ResultData subNews(HttpServletRequest request2, String title, String content, String type, Integer bigPhoto, String position) throws IOException {
        User user = (User) redisUtil.get(request.getSession().getId());
        ResultData resultData = new ResultData();
        News news = new News();
        news.setDate(new Date());
        news.setUserId(user.getId());
        news.setNickname(user.getNickname());
        news.setTitle(title);
        news.setContent(content);
        news.setFabulousCount(0);
        news.setBigPhoto(bigPhoto);
        news.setPosition(position);
        news.setType(type);
        news.setVisiteCount(0);
        news.setStatus(2);
        MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request2;
        if (multipartHttpServletRequest != null) {
            String path = "/data/app/news/newsPhoto";
            Iterator files = multipartHttpServletRequest.getFileNames();
            StringBuffer path2 = new StringBuffer();
            while (files.hasNext()) {
                MultipartFile multipartFile = multipartHttpServletRequest.getFile(files.next());
                String fileName = multipartFile.getOriginalFilename();
                String suffix = fileName.substring(fileName.lastIndexOf("."));
                File dir = new File(path);
                if (!dir.exists()) {
                    dir.mkdirs();
                }
                String uuid = UUID.randomUUID().toString();
                FileUtils.copyInputStreamToFile(multipartFile.getInputStream(), new File(path + "/" + uuid + suffix));
                path2.append("/newsPhoto/" + uuid + suffix + ";");
                System.out.println(path2);
            }
            news.setPhoto(path2.toString());
        }
        int flag = newsMapper.insert(news);
        if (flag > 0) {
            resultData.setSuccess(DataUtil.SUCCESS);
            resultData.setMessage("提交成功");
        } else {
            resultData.setSuccess(DataUtil.FAIL);
            resultData.setMessage("提交失败,请联系系统管理员。");
        }
        return resultData;
    }

 

 

 

你可能感兴趣的:(mui 通过plus.uploader.createUpload实现多文件上传)