package com.landtool.lanbase.modules.res.controller; import java.io.File; import org.apache.commons.io.FileUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import com.landtool.lanbase.config.SysTemPropertyConfig; import com.landtool.lanbase.modules.sys.controller.AbstractController; /** * 出图模板上传服务 * * @author wq */ @Controller @RequestMapping("/api/restemplate") public class UploadTemplateController extends AbstractController { @Autowired private SysTemPropertyConfig sysConfig; /** * 模板上传 * * @param file * @return */ @ResponseBody @RequestMapping("/uploadfile") public String uploadfile(@RequestParam("file") MultipartFile file, String rtid, String rtname) { String message = ""; if (!file.isEmpty()) { // 上传目录 String thisPath = sysConfig.getUploadTemplatesPath(); String paths[] = thisPath.split("\\\\"); //创建文件夹 String dir = paths[0]; for (int i = 0; i < paths.length - 1; i++) { try { dir = dir + "/" + paths[i + 1]; File dirFile = new File(dir); if (!dirFile.exists()) { dirFile.mkdirs(); } } catch (Exception e) { e.printStackTrace(); } } //判断文件是否存在 String fileName = file.getOriginalFilename(); File thisfile = new File(thisPath + "\\" + fileName); if (rtid != null && !"".equals(rtid)) {//编辑更新状态,删除之前上传模板 File fi = new File(thisPath + "\\" + rtname + "." + file.getOriginalFilename().split("\\.")[1]); fi.delete(); } //修改文件名 for(int i=1;i<100;i++){ if (thisfile.exists()) { fileName = file.getOriginalFilename().split("\\.")[0] + "("+i+")." + file.getOriginalFilename().split("\\.")[1]; thisfile = new File(thisPath + "\\" + fileName); }else{ break; } } // String webpath = "template/" + fileName; try { FileUtils.copyInputStreamToFile(file.getInputStream(), thisfile); // message = "{'result':'1','name':'" + fileName + "','Path':'" + sysConfig.getUploadRootPath() + webpath + "'}"; message = "{'result':'1','name':'" + fileName + "','Path':'上传成功!'}"; } catch (Exception e) { e.printStackTrace(); message = "{'result':'0'}"; } } return message; } @RequestMapping("/deletefile") public void deletefile(String rtname){ System.out.println(rtname); // 文件目录 String filePath = sysConfig.getUploadTemplatesPath(); File file = new File(filePath+"\\"+rtname+".mxd"); if(file.exists()){ file.delete(); } } }