随手笔记(十七)———后端多附件上传编辑处理

实体类中添加接收文件的字段,名称,id等等

     private List Files;

	//需要删除文件ID(数据库中没有该字段)
	private String deleteFileId;

	//需要删除文件名
	private String deleteFileName;

在service实现类中处理编辑的多份文件,处理分隔符,以及保存文件等等

	List files = MainDO.getFiles();
		List fName = Lists.newArrayList();
		List fId = Lists.newArrayList();
		if (files.size()>0)
			for (MultipartFile f : files) {
				if (f.getOriginalFilename().length()>0){
					String fileName =
							f.getOriginalFilename();
					fName.add(fileName);
					Integer fileId = DO.getId();
					fId.add(fileId.toString());
				}
			}
		String newFileNames = fName.toString().replaceAll("\\[","").replaceAll("\\]","");
		String newFileIds = fId.toString().replaceAll("\\[","").replaceAll("\\]","");
		//如果deleteFileId和deleteFileName发生更改,截取字符串,将之前的name和id做出相应修改
		String FileNameOld = MainDO.getFileName();
		String FileNameNew = "";
		if (MainDO.getDeleteFileName()!=null && !MainDO.getDeleteFileName().equals("")){
		FileNameNew = FileNameOld.replace(MainDO.getDeleteFileName(),"");
		}

		//分割字符串干掉多余d逗号
		if (FileNameNew!="" && !FileNameNew.equals("")) {
			char firstNameStr = FileNameNew.charAt(0);
			char lastNameStr = FileNameNew.charAt(FileNameNew.length() - 1);
			if (firstNameStr == ',') {
				FileNameNew = FileNameNew.substring(1);
			} else if (lastNameStr == ',') {
				FileNameNew = FileNameNew.substring(0, FileNameNew.length() - 1);
			} else {
				FileNameNew = FileNameOld;
			}

			if (newFileNames == null || newFileNames.equals("")) {
				MainDO.setFileName(FileNameNew);
			} else {
				MainDO.setFileName(newFileNames + "," + FileNameNew);
			}
		}else{
			if (newFileNames == null || newFileNames.equals("")) {
				MainDO.setFileName(FileNameOld);
			} else {
				MainDO.setFileName(newFileNames + "," + FileNameOld);
			}
		}
		String FileIdOld = MainDO.getFileId();
		String FukeIdNew = "";
		if (MainDO.getDeleteFileId()!=null && !MainDO.getDeleteFileId().equals("")){
			FukeIdNew = FileIdOld.replace(MainDO.getDeleteFileId(),"");
		}
		if (FukeIdNew!="" && !FukeIdNew.equals("")){
			char firstIdStr = FukeIdNew.charAt(0);
			char lastIdStr = FukeIdNew.charAt(FukeIdNew.length() - 1);
			if (firstIdStr == ',') {
				FukeIdNew = FukeIdNew.substring(1);
			} else if (lastIdStr == ',') {
				FukeIdNew = FukeIdNew.substring(0, FukeIdNew.length() - 1);
			} else {
				FukeIdNew = FileIdOld;
			}
			if (newFileIds== null || newFileIds.equals("")){
				MainDO.setFileId(FukeIdNew);
			}else{
				MainDO.setFileId(newFileIds+","+FukeIdNew);
			}
		}else{
			if (newFileIds== null || newFileIds.equals("")){
				MainDO.setFileId(FileIdOld);
			}else{
				MainDO.setFileId(newFileIds+","+FileIdOld);
			}
		}

 

你可能感兴趣的:(Java工作日记,Java工作日记)