package com.landtool.lanbase.modules.res.controller; import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.LinkedHashMap; import java.util.List; import java.util.UUID; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.io.FileUtils; import org.apache.commons.lang.StringEscapeUtils; import org.apache.shiro.SecurityUtils; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.select.Elements; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestBody; 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.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.landtool.lanbase.common.annotation.LogAction; import com.landtool.lanbase.common.utils.HttpOperateUtils; import com.landtool.lanbase.config.SysTemPropertyConfig; import com.landtool.lanbase.modules.api.utils.DeleteFileUtils; import com.landtool.lanbase.modules.res.entity.Res_ExtDataSource; import com.landtool.lanbase.modules.res.entity.Res_ExtFileSource; import com.landtool.lanbase.modules.res.entity.Res_FileSource_Way; import com.landtool.lanbase.modules.res.entity.Res_MainInfo; import com.landtool.lanbase.modules.res.entity.Res_StorageConfig; import com.landtool.lanbase.modules.res.entity.Res_Storage_Relation; import com.landtool.lanbase.modules.res.entity.JSONModels.NcConfig; import com.landtool.lanbase.modules.res.entity.JSONModels.NcMessage; import com.landtool.lanbase.modules.res.entity.ViewModels.FileSourceList; import com.landtool.lanbase.modules.res.service.ResExtDataSourceService; import com.landtool.lanbase.modules.res.service.ResExtFileSourceService; import com.landtool.lanbase.modules.res.service.ResFileSourceWayService; import com.landtool.lanbase.modules.res.service.ResMainInfoService; import com.landtool.lanbase.modules.res.service.ResStorageConfigService; import com.landtool.lanbase.modules.res.service.ResStorageRelaTionService; import com.landtool.lanbase.modules.sys.service.SysFieldvalueService; import cn.hutool.json.JSONUtil; /** * @Author: lizhao * @Date: 2018-03-06 14:49 * @Description:11系统资源扩展(文档数据)(RES_EXTFILESOURCE) */ @Controller @RequestMapping("/res") public class ResExtFileSourceController { @Autowired private ResExtFileSourceService resExtFileSourceService; @Autowired private SysTemPropertyConfig sysConfig; @Autowired private ResMainInfoService resMainInfoService; @Autowired private ResFileSourceWayService resFileSourceWayService; @Autowired private SysFieldvalueService FieldUtils; @Autowired private ResStorageConfigService resStorageConfigService; @Autowired private ResStorageRelaTionService resStorageRelaTionService; @Autowired private ResExtDataSourceService resExtDataSourceService; private String wenjianjiaPath() { return sysConfig.getUploadPath() + "FileSource\\wenjianjia\\"; } private String wenjianPath() { return sysConfig.getUploadPath() + "FileSource\\wenjian\\"; } /** * 访问资源文档数据图层信息页面 */ @RequestMapping("/ResManage/ResRegister/ExtFileSource") public String ExtFileSource(int resMainInfoId, Model model) { String unit = ""; Res_ExtFileSource resExtFileSource = resExtFileSourceService.selectByPrimaryKey(resMainInfoId); if (resExtFileSource != null) { model.addAttribute("Res_ExtFileSource", resExtFileSource); model.addAttribute("resMainInfoId", resMainInfoId); model.addAttribute("uploadPath", resExtFileSource.getServerurl()); model.addAttribute("absolutePath", sysConfig.getUploadPath()+resExtFileSource.getServerurl()); //判断是否有大小 if (resExtFileSource.getFilesize() != null) { String unitConvert = resExtFileSource.getFilesize() < 1 ? "1" : (resExtFileSource.getFilesize() < 1024 ? "2" : (resExtFileSource.getFilesize() < 1024 * 1024 ? "3" : "4")); model.addAttribute("unitConvert", unitConvert); switch (unitConvert) { case "1": unit = "bytes"; break; case "2": unit = "KB"; break; case "3": unit = "MB"; break; case "4": unit = "GB"; break; } } else { String unitConvert = "5"; model.addAttribute("unitConvert", unitConvert); } Res_MainInfo res_mainInfo = resMainInfoService.selectByPrimaryKey(resMainInfoId); model.addAttribute("resMainInfo", res_mainInfo); // 文件夹 if (resExtFileSource.getSourcetype() != null && resExtFileSource.getSourcetype().equals("文件夹")) { if (resExtFileSource.getServerurl() != null && resExtFileSource.getStoragemode() != null && resExtFileSource.getStoragemode().equals("本地")) { File file = new File(sysConfig.getUploadPath() + resExtFileSource.getServerurl()); if (file.isDirectory()) { System.out.println(resExtFileSource.getServerurl()); String[] fileLists = file.list(); File[] files = file.listFiles(); List fileSourceLists = new ArrayList <>(); for (int i = 0; i < fileLists.length; i++) { File file1 = new File(fileLists[i]); File file2 = new File(files[i].getAbsolutePath()); String size = com.landtool.lanbase.common.utils.FileUtils.setSize(file2.length()); String path2 = wenjianjiaPath() + file1.getName(); // 文件映射路径 FileSourceList sourceList = new FileSourceList(); sourceList.setName(file1.getName()); sourceList.setPath(path2); sourceList.setSize(size); Res_Storage_Relation res_Storage_Relation = new Res_Storage_Relation(); res_Storage_Relation.setResourceid(resMainInfoId); res_Storage_Relation.setFilename(file1.getName()); Res_Storage_Relation resStorageRelation = resStorageRelaTionService.selectByResourceidAndFileName(res_Storage_Relation); if(resStorageRelation!=null && resStorageRelation.getIsstorage()==1){ sourceList.setStorageConfig("已入库"); }else{ sourceList.setStorageConfig("未入库"); } if(resStorageRelation!=null && resStorageRelation.getIsgeneration()==1){ sourceList.setNcjson("已生成"); }else{ sourceList.setNcjson("未生成"); } fileSourceLists.add(sourceList); } model.addAttribute("filelist", fileSourceLists); } } } } else { model.addAttribute("unitConvert", "5"); unit = "KB"; Res_ExtFileSource resExtFileSource1 = new Res_ExtFileSource(); model.addAttribute("Res_ExtFileSource", resExtFileSource1); model.addAttribute("resMainInfo", new Res_MainInfo()); } model.addAttribute("unit", unit); // 获取文档类型列表 LinkedHashMap filetypelist = FieldUtils.getFieldListByKey("FileType"); model.addAttribute("filetypelist", filetypelist); if (SecurityUtils.getSubject().isPermitted("org_user_admin")) { //判断当前用户是否是管理员,是管理员或是未提交的资源才可以修改资源相关信息 model.addAttribute("admin", true); } else { model.addAttribute("admin", false); } model.addAttribute("ncSyncStorageUrl", sysConfig.getNcStorageUrl().substring(0, sysConfig.getNcStorageUrl().length()-9)+"jobs/"); model.addAttribute("readNcFileURL", sysConfig.getReadNcFileURL()); return "ResManage/ResRegister/ExtFileSource"; } /** * 数据文件下的 展现方式 */ @RequestMapping("/ResManage/ResRegister/ExtPresentationMode") public String ExtPresentationMode(int resMainInfoId, Model model) { model.addAttribute("resMainInfoId", resMainInfoId); List list = resFileSourceWayService.selectById(resMainInfoId); model.addAttribute("FileSourceWay", list); return "ResManage/ResRegister/ExtPresentationMode"; } /** * 添加或者更新 数据文件展现方式 */ @ResponseBody @RequestMapping("/ResManage/ResRegister/insertSelectiveAndUpdate") @LogAction("资源管理,资源发布,资源修改,修改|zy") public int insertSelectiveAndUpdate(Res_MainInfo resMainInfo, String extMapUrlStr) { // 删除id 对应的所有文件展示方式 try { if (resMainInfo.getResourceid() != null) { resFileSourceWayService.deleteByPrimaryKey(resMainInfo.getResourceid()); } if (!extMapUrlStr.equals("")) { String[] extMapUrlArray = extMapUrlStr.split("\\|"); // 插入资源支持协议与地址表 for (int i = 0; i < extMapUrlArray.length; i++) { String[] extMapUrlArray2 = extMapUrlArray[i].split(","); String Showway = extMapUrlArray2[0]; String url = extMapUrlArray2[1]; String remark = extMapUrlArray2[2]; Res_FileSource_Way resFileSourceWay = new Res_FileSource_Way(); resFileSourceWay.setShowway(Showway); resFileSourceWay.setUrl(url); resFileSourceWay.setRemark(remark); resFileSourceWay.setResourceid(resMainInfo.getResourceid()); resFileSourceWay.setCreatedate(new Date()); resFileSourceWayService.insertSelective(resFileSourceWay); } } return 1; } catch (Exception e) { return 0; } } /** * 根据id 删除文档数据列表功能 */ @ResponseBody @RequestMapping("/resExtFileSource/deleteByPrimaryKey") public int deleteByPrimaryKey(int resourceid) { // 删除数据库信息先把文件删除 Res_ExtFileSource res_extFileSource = resExtFileSourceService.selectByPrimaryKey(resourceid); String path = res_extFileSource.getServerurl(); if (res_extFileSource.getSourcetype().equals("文件夹")) { //alert by ykm 2019/08/30 有的路径已经包含了文件夹还是文件目录 if(path.indexOf("wenjianjia") == -1) { path = wenjianjiaPath() + path + "/"; } else { path = sysConfig.getUploadPath() + path + "/"; } } if (res_extFileSource.getSourcetype().equals("文件")) { //alert by ykm 2019/08/30 有的路径已经包含了文件夹还是文件目录 if (path.indexOf("wenjian") == -1) { path = wenjianPath() + path; } else { path = sysConfig.getUploadPath() + path; } } DeleteFileUtils.deletefileUtils(path); return resExtFileSourceService.deleteByPrimaryKey(resourceid); } /** * 插入文档数据列表功能(所有元素不能为空) */ @ResponseBody @RequestMapping("/resExtFileSource/insert") public int insert(Res_ExtFileSource record) { return resExtFileSourceService.insert(record); } /** * 插入文档数据列表功能(可以只填写必填字段) */ @ResponseBody @RequestMapping("/resExtFileSource/insertSelective") public int insertSelective(Res_ExtFileSource record) { return resExtFileSourceService.insertSelective(record); } /** * 插入文档数据列表功能(可以只填写必填字段) */ @ResponseBody @RequestMapping("/resExtFileSource/insertSelectiveAndUpdate") public int insertSelectiveAndUpdate(Res_ExtFileSource record, double savedsizenum) { record.setFilesize(savedsizenum); record.setServerurl(StringEscapeUtils.unescapeHtml(record.getServerurl())); Res_ExtFileSource resExtFileSource = resExtFileSourceService.selectByPrimaryKey(record.getResourceid()); if (resExtFileSource == null) { // 添加 if (record.getDownloadmodel().equals("FTP")) { // 把不属于FTP的字段设置为空 record.setGettype(null); } else if (record.getDownloadmodel().equals("HTTP")) { record.setFtpusername(null); record.setFtppwd(null); } if (record.getSourcetype().equals("文件")) { TiHuanLuJin(record.getServerurl()); record.setServerurl(record.getServerurl().replace("temp/", "")); } return resExtFileSourceService.insertSelective(record); } else { // 更新 if (record.getSourcetype().equals("文件")) { TiHuanLuJin(record.getServerurl()); if (!record.getServerurl().equals("") && !resExtFileSource.getServerurl().equals("") && resExtFileSource.getStoragemode().equals("本地")) { String path = resExtFileSource.getServerurl(); path = sysConfig.getUploadPath() + path; File file = new File(path); if (file.exists()) { DelectEquealFile(record.getServerurl(), resExtFileSource.getServerurl()); } } record.setServerurl(record.getServerurl().replace("temp/", "")); } if (record.getDownloadmodel().equals("FTP")) { record.setGettype(null); } else if (record.getDownloadmodel().equals("HTTP")) { record.setFtpusername(null); record.setFtppwd(null); } // 如果文件资源类型改变则把原有的文件删除 if (!record.getSourcetype().equals(resExtFileSource.getSourcetype()) || record.getStoragemode().equals("远程")) { String path = resExtFileSource.getServerurl(); if (resExtFileSource.getSourcetype().equals("文件夹") && resExtFileSource.getStoragemode() != null && resExtFileSource.getStoragemode().equals("本地")) { path = sysConfig.getUploadPath() + path; File file = new File(path); if (file.exists()) { DeleteFileUtils.deletefileUtils(path); } } if (resExtFileSource.getSourcetype().equals("文件") && resExtFileSource.getStoragemode() != null && resExtFileSource.getStoragemode().equals("本地")) { path = sysConfig.getUploadPath() + path; File file = new File(path); if (file.exists()) { DeleteFileUtils.deletefileUtils(path); } } } return resExtFileSourceService.updateByPrimaryKey(record); } } //删除相同文件 private void DelectEquealFile(@RequestBody String NEWString, String OLDString) { String oldourl = OLDString.substring(OLDString.lastIndexOf("/"), OLDString.length()); String newurl = NEWString.substring(NEWString.lastIndexOf("/"), NEWString.length()); if (!oldourl.equals(newurl)) { File OLDFile = new File(sysConfig.getUploadPath() + OLDString.replace("/", "\\")); System.out.println(OLDFile); OLDFile.delete(); } } //替换路径 private void TiHuanLuJin(@RequestBody String lujin) { String oldFileUrl = sysConfig.getUploadPath() + lujin.replace("/", "\\"); File oldFile = new File(oldFileUrl); String NewFileUrl = sysConfig.getUploadPath() + lujin.replace("temp", "").replace("/", "\\"); NewFileUrl = NewFileUrl.substring(0, NewFileUrl.lastIndexOf("\\")) + "\\"; File NewFile = new File(NewFileUrl); if (!NewFile.exists()) { // 当前地址不为空,判断该路径是否存在,不存在则创建新的文件夹 File newfilePath = new File(NewFile + "\\"); // 创建对应的年月文件夹 newfilePath.mkdirs(); } com.landtool.lanbase.common.utils.FileUtils.moveTotherFolders(oldFileUrl, NewFileUrl); } /** * 根据id查询文档数据列表 */ @ResponseBody @RequestMapping("/resExtFileSource/selectByPrimaryKey") public Res_ExtFileSource selectByPrimaryKey(int resourceid) { return resExtFileSourceService.selectByPrimaryKey(resourceid); } /** * 根据id更新文档数据列表(更新部分数据) */ @ResponseBody @RequestMapping("/resExtFileSource/updateByPrimaryKeySelective") public int updateByPrimaryKeySelective(Res_ExtFileSource record) { return resExtFileSourceService.updateByPrimaryKeySelective(record); } /** * 根据id更新文档数据列表(更新所有数据) */ @ResponseBody @RequestMapping("/resExtFileSource/updateByPrimaryKey") public int updateByPrimaryKey(Res_ExtFileSource record) { return resExtFileSourceService.updateByPrimaryKey(record); } /** 文件上传 */ /** * @param sourcetype 文件资源类型 * @return resourceid 资源ID * @Description: * @author ykm * @date 2018/4/19 */ @ResponseBody @RequestMapping("/resExtFileSource/uploadfile") public String uploadfile(@RequestParam("myFile") MultipartFile[] file, String sourcetype, String resourceid) { String thisSize = ""; // 当前文件大小 String Filename = null;// 文件名称 List Filenames = new ArrayList();// 文件名称总和 List finasizes = new ArrayList(); String name = ""; // 文件全称(上传时候的文件名称) String res = ""; long size = 0; // 文件大小 long totalSize = 0; //批量上传文件大小和 for (int i = 0; i < file.length; i++) { if (file[i].isEmpty()) { return "No File"; } String uuid = UUID.randomUUID().toString().replaceAll("-", ""); name = file[i].getOriginalFilename(); String Fname = name.trim().substring(name.lastIndexOf("\\") + 1); String fileType = name.substring(name.lastIndexOf(".")); size = file[i].getSize(); totalSize += size; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM"); String timePath = sdf.format(new Date().getTime()); timePath = timePath.replace("-", ""); File desFile = null; String path2 = null; String webpath = null; // 映射地址 boolean rename = false; // 检查是否存在文件名相同的文件 //alter: Xxx 处理资源向导数据文件资源文件上传问题 20181226 Res_ExtFileSource resExtFileSource = new Res_ExtFileSource(); if(resourceid != null && !resourceid.isEmpty()) { resExtFileSource = resExtFileSourceService.selectByPrimaryKey(Integer.parseInt(resourceid)); } String path = null; if (sourcetype.equals("文件夹")) { Filename = Fname; if (resExtFileSource != null && resExtFileSource.getServerurl() != null) { // 判断数据库服务地址字段是否为空 path = sysConfig.getUploadPath() + resExtFileSource.getServerurl().replaceAll("/", "\\\\"); File filePath = new File(path); File[] files = filePath.listFiles(); if (filePath.isDirectory()) { String[] fileLists = filePath.list(); for (int j = 0; j < fileLists.length; j++) { File file1 = new File(fileLists[j]); if (Filename.equals(file1.getName())) { rename = true; break; } } } if (!rename) { if (!filePath.exists()) { // 当前数据库服务地址不为空,判断该路径是否存在,不存在则创建新的文件夹 path = sysConfig.getUploadPath() + "FileSource\\wenjianjia\\" + timePath + "\\" + resourceid + "\\"; File newfilePath = new File(path); // 创建对应的年月文件夹 newfilePath.mkdirs(); webpath = "FileSource/wenjianjia/" + timePath + "/" + resourceid; } else { webpath = "FileSource/wenjianjia/" + timePath + "/" + resourceid; // 类型为文件夹则指向到文件夹 } } } else { // 当前数据库服务地址为空,则直接创建新的文件夹 path = sysConfig.getUploadPath() + "FileSource\\wenjianjia\\" + timePath + "\\" + resourceid + "\\"; File newfilePath = new File(path); // 创建对应的年月文件夹 if (!newfilePath.exists()) { newfilePath.mkdirs(); } webpath = "FileSource/wenjianjia/" + timePath + "/" + resourceid; // 类型为文件夹则指向到文件夹 } // path2 = env.getProperty("webroot") + "wenjianjia/"; if (!rename) { path = sysConfig.getUploadPath() + "FileSource\\wenjianjia\\" + timePath + "\\" + resourceid + "\\"; desFile = new File(path + Filename); String info = desFile.getAbsolutePath(); System.out.println("info:" + info); } } if (sourcetype.equals("文件")) { Filename = uuid + fileType; // 当文件资源类型为文件时,判断是否该资源已经存在文件,如果有则先删除再执行上传新文件 if (resExtFileSource != null && resExtFileSource.getServerurl() != null) { String dirpath = resExtFileSource.getServerurl().substring(0, resExtFileSource.getServerurl().lastIndexOf("/") + 1); String oldfile = sysConfig.getUploadPath() + resExtFileSource.getServerurl().replaceAll("/", "\\\\"); String ofile = sysConfig.getUploadPath() + dirpath.replaceAll("/", "\\\\"); // DeleteFileUtils.deletefileUtils(oldfile); File file1 = new File(ofile); if (!file1.exists()) { path = sysConfig.getUploadPath() + "temp\\FileSource\\wenjian\\" + timePath + "\\"; File newfilePath = new File(path); // 创建对应的年月文件夹 newfilePath.mkdirs(); webpath = "temp/FileSource/wenjian/" + timePath + "/" + Filename; // 类型为文件指向文件 } else { path = sysConfig.getUploadPath() + "temp\\FileSource\\wenjian\\" + timePath + "\\"; //文件网络地址 webpath = "temp/FileSource/wenjian/" + timePath + "/" + Filename; // 类型为文件指向文件 } } else { path = sysConfig.getUploadPath() + "temp\\FileSource\\wenjian\\" + timePath + "\\"; File filePath = new File(path); // 创建对应的年月文件夹 if (!filePath.exists()) { filePath.mkdirs(); } //文件网络地址 webpath = "temp/FileSource/wenjian/" + timePath + "/" + Filename; // 类型为文件指向文件 } desFile = new File(path + Filename); String info = desFile.getAbsolutePath(); System.out.println("info:" + info); } if (!rename) { //当sourcetype.equals("文件") String finasize = ""; if (sourcetype.equals("文件")) { finasize = com.landtool.lanbase.common.utils.FileUtils.setSize(size); } else { final long total = com.landtool.lanbase.common.utils.FileUtils.getTotalSizeOfFilesInDir(new File(path)); finasize = com.landtool.lanbase.common.utils.FileUtils.setSize(total + size); } try { FileUtils.copyInputStreamToFile(file[i].getInputStream(), desFile); Filenames.add(Fname); finasizes.add(com.landtool.lanbase.common.utils.FileUtils.setSize(size)); res = "{'result':'1','finasizes':'" + finasizes + "','name':'" + Filenames + "','Path':'" + webpath + "','Filename':'" + name.substring(name.lastIndexOf("/") + 1, name.lastIndexOf(".")) + "','size':'" + finasize.substring(0, finasize.lastIndexOf(" ")) + "','unit':'" + finasize.substring(finasize.lastIndexOf(" ") + 1, finasize.length()) + "'}"; System.out.println(res); } catch (IOException e) { e.printStackTrace(); res = "{'result':'0'}"; } } else { res = "{'result':'3'}"; } } return res; } /** * @param filepath 文件路径 * @return * @Description: 文件删除 * @author ykm * @date 2018/4/19 16:38 */ @ResponseBody @RequestMapping("/resExtFileSource/deletefile") public String deletefile(String filepath, String sourcetype, String name, Res_ExtFileSource resExtFileSource) { String res = ""; String lujin = filepath; if (sourcetype.equals("文件夹")) { filepath = sysConfig.getUploadPath() + filepath.replaceAll("/", "\\\\") + "\\" + name; //读取删除之后文件夹大小 boolean flag = DeleteFileUtils.deletefileUtils(filepath); final long total = com.landtool.lanbase.common.utils.FileUtils.getTotalSizeOfFilesInDir(new File(sysConfig.getUploadPath() + lujin.replaceAll("/", "\\\\"))); String finasize = com.landtool.lanbase.common.utils.FileUtils.setSize(total); res = "{'result':'" + flag + "','size':'" + finasize.substring(0, finasize.lastIndexOf(" ")) + "','unit':'" + finasize.substring(finasize.lastIndexOf(" ") + 1, finasize.length()) + "'}"; //将大小写入 数据库 resExtFileSource.setFilesize((double) (total / 1000)); resExtFileSourceService.updateByPrimaryKeySelective(resExtFileSource); } if (sourcetype.equals("文件")) { filepath = sysConfig.getUploadPath() + filepath.replaceAll("/", "\\\\") + "\\" + name; boolean flag = DeleteFileUtils.deletefileUtils(filepath); res = "{'result':'" + flag + "','size':'null','unit':'null'}"; } return res; } /** * @Author: lizhao * @Date: 2018-04-25 16:23 * @param查询本地固定地址的文件 * @Description: */ @ResponseBody @RequestMapping("/resExtFileSource/OpenFileName") public String OpenFileName(Model model) { File file = new File(wenjianPath()); // 获取该目录下的所有文件大小 File[] files = file.listFiles(); // 获取文件名称 String[] fileLists = file.list(); StringBuilder rsb = new StringBuilder(); rsb.append("{'topics':["); for (Integer i = 0; i < files.length; i++) { // 大小 File file2 = new File(files[i].getAbsolutePath()); String size = FileUtils.byteCountToDisplaySize(file2.length()); // 名称 File file1 = new File(fileLists[i]); String name = file1.getName(); if (i != 0) { rsb.append(","); } rsb.append("{'name':'" + name + "'"); rsb.append(",'size':'" + size + "'}"); } rsb.append("]}"); return rsb.toString(); } @ResponseBody @RequestMapping("/resExtFileSource/FuWuDiZi") public String FuWuDiZi(String url) { String NeiRong = ""; try { NeiRong = HttpOperateUtils.httpGet(url); } catch (IOException e) { e.printStackTrace(); NeiRong = "失败"; } return NeiRong; } @RequestMapping("/resExtFileSource/download") public void download(HttpServletResponse response, String path, String Title, HttpServletRequest request) { System.out.println(sysConfig.getUploadPath() + path.replaceAll("/", "\\\\").replace("\\uploadPath", "")); System.out.println(Title); com.landtool.lanbase.common.utils.FileUtils.download(sysConfig.getUploadPath() + path.replaceAll("/", "\\\\").replace("\\uploadPath", ""), Title, response); } /** * /*jsoup只支持http、https协议连接,不支持ftp */ @ResponseBody @RequestMapping("/resExtFileSource/getUrlList") public String jsoupDetail(String url) { try { Document doc = Jsoup.connect(url).get(); StringBuilder sb = new StringBuilder(); //目前只考虑了目录结构为table和pre标签下的目录,即Tomcat和IIS Elements tableElements = doc.getElementsByTag("table"); //http://www.zhjg.com:8080/javapubzy/BootStrap4/ Elements preElements = doc.getElementsByTag("pre"); //http://10.1.4.27/shuqu/ if (tableElements.size() > 0) { Document tableDoc = Jsoup.parse(tableElements.toString()); int trSize = tableDoc.select("tr").size(); int tdSize = tableDoc.select("tr").first().select("td").size(); //获取第一行tr下的td数 String[][] tableList = new String[trSize][2]; int tr = -1; int td = -1; for (int i = 0; i < tdSize; i++) { tableList[0][i] = tableDoc.select("tr").get(0).select("td").get(i).text(); if (tableList[0][i].equals("Filename") && tr == -1) { tr = i; } if (tableList[0][i].equals("Size") && td == -1) { td = i; } if (tr != -1 && td != -1) { break; } } sb.append("["); for (int i = 1; i < trSize; i++) { if (i != 1) sb.append(","); tableList[i][0] = tableDoc.select("tr").get(i).select("td").get(tr).select("A").get(0).text(); tableList[i][1] = tableDoc.select("tr").get(i).select("td").get(td).select("tt").get(0).text(); sb.append("{"); sb.append("filename: '" + tableList[i][0] + "'"); sb.append(",size: '" + tableList[i][1] + "'"); sb.append("}"); } sb.append("]"); return sb.toString(); } else if (preElements.size() > 0) { int aSize = preElements.select("A").size(); //获取a标签个数 sb.append("["); for (int i = 1; i < aSize; i++) { if (i != 1) sb.append(","); String aName = preElements.select("A").get(i).text(); sb.append("{"); sb.append("filename: '" + aName + "'"); sb.append(",size: ''"); sb.append("}"); } sb.append("]"); return sb.toString(); } else { sb.append("[{"); sb.append("filename: '无法获取该地址下的目录!'"); sb.append(",size: ''"); sb.append("}]"); return sb.toString(); } } catch (IOException e) { e.printStackTrace(); return "[{filename:'无法获取该地址下的目录!',size:''}]"; } } @RequestMapping("/ResManage/ResRegister/NcFileConfig") public String NcFileConfig(int resourceType,int resourceid,Model model){ Res_ExtFileSource res_ExtFileSource = resExtFileSourceService.selectByPrimaryKey(resourceid); if(res_ExtFileSource.getNcconfig()!=null && !res_ExtFileSource.getNcconfig().equals("")){ JSONObject jsonObject = JSONObject.parseObject(res_ExtFileSource.getNcconfig()); JSONArray jsonArray = jsonObject.getJSONArray("ncconfig"); NcConfig ncConfig = JSONUtil.toBean(String.valueOf(jsonArray.get(0)), NcConfig.class); model.addAttribute("ncConfig", ncConfig); } model.addAttribute("pubzyWebRoot", sysConfig.getPubzyWebRoot()); model.addAttribute("ncFileConfigURL", sysConfig.getNcFileConfigURL()+"?resourceType="+resourceType+"&uploadPath="+sysConfig.getUploadPath()+res_ExtFileSource.getServerurl()); model.addAttribute("readNcFileURL", sysConfig.getReadNcFileURL()); model.addAttribute("res_ExtFileSource", res_ExtFileSource); model.addAttribute("resourceid", resourceid); model.addAttribute("resourceType", resourceType); model.addAttribute("uploadPath", res_ExtFileSource.getServerurl()); model.addAttribute("absolutePath", sysConfig.getUploadPath()+res_ExtFileSource.getServerurl()); return "ResManage/ResRegister/NcFileConfig"; } @RequestMapping("/ResManage/ResRegister/StorageConfig") public String StorageConfig(int resourceType,int resourceid,Model model){ Res_ExtFileSource res_ExtFileSource = resExtFileSourceService.selectByPrimaryKey(resourceid); if(res_ExtFileSource.getNcconfig()!=null && !res_ExtFileSource.getNcconfig().equals("")){ JSONObject jsonObject = JSONObject.parseObject(res_ExtFileSource.getNcconfig()); JSONArray jsonArray = jsonObject.getJSONArray("ncconfig"); NcConfig ncConfig = JSONUtil.toBean(String.valueOf(jsonArray.get(0)), NcConfig.class); model.addAttribute("ncConfig", ncConfig); } Res_StorageConfig resStorageConfig = resStorageConfigService.findStorageConfigByResourceid(resourceid); if(resStorageConfig!=null && resStorageConfig.getVariableparams()!=null && !resStorageConfig.getVariableparams().equals("")){ JSONArray jsonarray = JSONArray.parseArray(resStorageConfig.getVariableparams()); model.addAttribute("jsonarray", jsonarray); } if(resStorageConfig!=null && resStorageConfig.getUvparams()!=null && !resStorageConfig.getUvparams().equals("")){ JSONArray uvjson = JSONArray.parseArray(resStorageConfig.getUvparams()); model.addAttribute("uvjson", uvjson); } model.addAttribute("resStorageConfig", resStorageConfig); model.addAttribute("pubzyWebRoot", sysConfig.getPubzyWebRoot()); model.addAttribute("ncFileConfigURL", sysConfig.getNcFileConfigURL()+"?resourceType="+resourceType+"&uploadPath="+sysConfig.getUploadPath()+res_ExtFileSource.getServerurl()); model.addAttribute("readNcFileURL", sysConfig.getReadNcFileURL()); model.addAttribute("res_ExtFileSource", res_ExtFileSource); model.addAttribute("resourceid", resourceid); model.addAttribute("resourceType", resourceType); model.addAttribute("ncSyncStorageUrl", sysConfig.getNcStorageUrl().substring(0, sysConfig.getNcStorageUrl().length()-9)+"jobs/"); return "ResManage/ResRegister/StorageConfig"; } @RequestMapping("/ResManage/ResRegister/updateNcJsonPath") @ResponseBody public int updateNcJsonPath(Res_ExtFileSource res_ExtFileSource){ return resExtFileSourceService.updateByPrimaryKeySelective(res_ExtFileSource); } //保存入库配置 @RequestMapping("/ResManage/ResRegister/saveStorageConfig") @ResponseBody public int saveStorageConfig(Res_StorageConfig res_storageConfig){ Res_StorageConfig storage = resStorageConfigService.findStorageConfigByResourceid(res_storageConfig.getResourceid()); if(storage==null){ return resStorageConfigService.insertStorageConfig(res_storageConfig); }else{ return resStorageConfigService.updateStorageConfig(res_storageConfig); } } //NC配置 @RequestMapping("/ResManage/ResRegister/getNcConfig") @ResponseBody public String getNcConfig(int resourceid){ Res_ExtFileSource res_ExtFileSource = resExtFileSourceService.selectByPrimaryKey(resourceid); return res_ExtFileSource.getNcconfig(); } //入库配置 @RequestMapping("/ResManage/ResRegister/findParams") @ResponseBody public String findParams(int resourceid){ String params = ""; Res_StorageConfig res_storageConfig = resStorageConfigService.findStorageConfigByResourceid(resourceid); if(res_storageConfig!=null && res_storageConfig.getVariableparams()!=null && !res_storageConfig.getVariableparams().equals("")){ params = res_storageConfig.getVariableparams(); }else if(res_storageConfig!=null && res_storageConfig.getUvparams()!=null && !res_storageConfig.getUvparams().equals("")){ params = res_storageConfig.getUvparams(); } return params; } //清空配置 @RequestMapping("/ResManage/ResRegister/clearConfig") @ResponseBody public int clearConfig(int resourceid,int type){ int total=0; switch (type) { case 1: //nc配置清空 Res_ExtFileSource resExtFileSource = new Res_ExtFileSource(); resExtFileSource.setResourceid(resourceid); resExtFileSource.setNcconfig(""); total = resExtFileSourceService.updateByPrimaryKeySelective(resExtFileSource); break; case 2: //入库配置清空 total = resStorageConfigService.deleteByResourceid(resourceid); break; default: Res_ExtFileSource res_ExtFileSource = new Res_ExtFileSource(); res_ExtFileSource.setResourceid(resourceid); res_ExtFileSource.setNcconfig(""); res_ExtFileSource.setNcjsonattribute(""); res_ExtFileSource.setNcjsonpath(""); total = resExtFileSourceService.updateByPrimaryKeySelective(res_ExtFileSource); total+= resStorageConfigService.deleteByResourceid(resourceid); break; } return total; } //调用服务入库 @RequestMapping("/ResManage/ResRegister/saveStorage") @ResponseBody public String saveStorage(int resourceid,String fileName,String param){ Res_StorageConfig res_storageConfig = resStorageConfigService.findStorageConfigByResourceid(resourceid); if(res_storageConfig.getVariableparams()!=null && !res_storageConfig.getVariableparams().equals("")){ res_storageConfig.setVariableparams(param); }else{ res_storageConfig.setUvparams(param); } String databaseid =""; if(res_storageConfig.getVariableparams()!=null && !res_storageConfig.getVariableparams().equals("")){ databaseid = JSONObject.parseObject(res_storageConfig.getVariableparams()).get("databaseid").toString(); }else if(res_storageConfig.getUvparams()!=null && !res_storageConfig.getUvparams().equals("")){ databaseid = JSONObject.parseObject(res_storageConfig.getUvparams()).get("databaseid").toString(); } Res_ExtDataSource res_ExtDataSource = resExtDataSourceService.selectByPrimaryKey(Integer.valueOf(databaseid)); Res_ExtFileSource res_ExtFileSource = resExtFileSourceService.selectByPrimaryKey(res_storageConfig.getResourceid()); String ncFilePath = sysConfig.getUploadRootPath()+res_ExtFileSource.getServerurl(); String body =""; List jobids = new ArrayList(); NcMessage message = new NcMessage(); body = resStorageConfigService.invoke(res_storageConfig,res_ExtDataSource,ncFilePath+"/"+fileName); String jobId = (String) JSONObject.parseObject(body).get("jobId"); String messages = (String) JSONObject.parseObject(body).get("messages"); if(jobId!=null && !jobId.equals("")){ message.setCode(1); jobids.add(jobId); }else{ message.setCode(2); //返回包含result message 代表同步 if(messages!=null && !messages.equals("")){ JSONArray jsonArray = JSON.parseArray(messages); String description = jsonArray.getJSONObject(jsonArray.size()-1).get("description").toString(); if(description.contains("Succeeded")){ message.setMessage("success"); } } if(body.contains("error")){ message.setMessage("error"); } } message.setJobids(jobids); return JSON.toJSON(message).toString(); } //异步调用服务入库 @RequestMapping("/ResManage/ResRegister/syncSaveStorage") @ResponseBody public String syncSaveStorage(Res_StorageConfig res_storageConfig,int resourceType){ String databaseid =""; if(res_storageConfig.getVariableparams()!=null && !res_storageConfig.getVariableparams().equals("")){ databaseid = JSONObject.parseObject(res_storageConfig.getVariableparams()).get("databaseid").toString(); }else if(res_storageConfig.getUvparams()!=null && !res_storageConfig.getUvparams().equals("")){ databaseid = JSONObject.parseObject(res_storageConfig.getUvparams()).get("databaseid").toString(); } Res_ExtDataSource res_ExtDataSource = resExtDataSourceService.selectByPrimaryKey(Integer.valueOf(databaseid)); Res_ExtFileSource res_ExtFileSource = resExtFileSourceService.selectByPrimaryKey(res_storageConfig.getResourceid()); String ncFilePath = sysConfig.getUploadRootPath()+res_ExtFileSource.getServerurl(); String body =""; List jobids = new ArrayList(); NcMessage message = new NcMessage(); if(resourceType==1){ //文件 body = resStorageConfigService.invoke(res_storageConfig,res_ExtDataSource,ncFilePath); System.out.println(body); String jobId = (String) JSONObject.parseObject(body).get("jobId"); String messages = (String) JSONObject.parseObject(body).get("messages"); if(jobId!=null && !jobId.equals("")){ //返回包含jobId 代表异步 message.setCode(1); jobids.add(jobId); }else{ message.setCode(2); //返回包含result message 代表同步 if(messages!=null && !messages.equals("")){ JSONArray jsonArray = JSON.parseArray(messages); String description = jsonArray.getJSONObject(jsonArray.size()-1).get("description").toString(); if(description.contains("Succeeded")){ message.setMessage("success"); } } if(body.contains("error")){ message.setMessage("error"); } } }else{ //资源目录 File files = new File(sysConfig.getUploadPath().replace("\\", "/")+res_ExtFileSource.getServerurl()); File[] listFiles = files.listFiles(); if(listFiles.length>0){ for(File file : listFiles){ if(!file.isDirectory() && file.getName().contains(".nc")){ String path =ncFilePath+"/"+file.getName(); body = resStorageConfigService.invoke(res_storageConfig,res_ExtDataSource,path); System.out.println(body); String jobId = (String) JSONObject.parseObject(body).get("jobId"); String messages = (String) JSONObject.parseObject(body).get("messages"); if(jobId!=null && !jobId.equals("")){ message.setCode(1); jobids.add(jobId); }else{ message.setCode(2); if(messages!=null && !messages.equals("")){ JSONArray jsonArray = JSON.parseArray(messages); String description = jsonArray.getJSONObject(jsonArray.size()-1).get("description").toString(); if(description.contains("Succeeded")){ message.setMessage("success"); } } if(body.contains("error")){ message.setMessage("error"); } } } } } } message.setJobids(jobids); return JSON.toJSON(message).toString(); } @RequestMapping("/ResManage/ResRegister/saveStorageRelation") @ResponseBody public void saveStorageRelation(Res_Storage_Relation storageRelation){ Res_Storage_Relation res_Storage_Relation = resStorageRelaTionService.selectByResourceidAndFileName(storageRelation); if(res_Storage_Relation==null){ storageRelation.setIsstorage(1); resStorageRelaTionService.insert(storageRelation); }else{ storageRelation.setIsstorage(1); resStorageRelaTionService.update(storageRelation); } } @RequestMapping("/ResManage/ResRegister/insertStorageRelation") @ResponseBody public void insertStorageRelation(Res_Storage_Relation storageRelation){ Res_Storage_Relation res_Storage_Relation = resStorageRelaTionService.selectByResourceidAndFileName(storageRelation); if(res_Storage_Relation==null){ storageRelation.setIsgeneration(1); resStorageRelaTionService.insert(storageRelation); }else{ storageRelation.setIsgeneration(1); resStorageRelaTionService.update(storageRelation); } } }