package com.landtool.lanbase.modules.res.controller; import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import java.util.UUID; import org.apache.commons.io.FileUtils; 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.github.pagehelper.Page; import com.github.pagehelper.PageHelper; import com.landtool.lanbase.config.SysTemPropertyConfig; import com.landtool.lanbase.modules.api.utils.DeleteFileUtils; import com.landtool.lanbase.modules.api.utils.PageBean; import com.landtool.lanbase.modules.res.entity.Res_Files; import com.landtool.lanbase.modules.res.service.ResFilesService; import com.landtool.lanbase.modules.res.service.ResMainInfoService; /** * @Author: xiaoqi * @Date: 2018-04-19 14:49 * @Description:11系统资源扩展(附件管理)(Res_Files) * */ @Controller @RequestMapping("/res") public class ResFilesController { @Autowired private ResFilesService resFilesService; // @Autowired // public org.springframework.core.env.Environment env; @Autowired private SysTemPropertyConfig sysConfig; @Autowired private ResMainInfoService resMainInfoService; private Integer MainInfoId=null; //当前选中的资源ID /** * 访问附件管理数据图层信息页面 */ @RequestMapping("/ResManage/ResRegister/ResFiles") public String ResFiles(int resMainInfoId,Model datamodel) { MainInfoId=resMainInfoId; //获取当前资源ID List List = resFilesService.selectDataListForResourceid(resMainInfoId); String result = "["; for (Res_Files model : List) { result += "{ filename:" + model.getFilename() + "},"; } if (List.size() > 0) { result = result.substring(0, result.length() - 1); } result += "]"; return "ResManage/ResRegister/ResFiles"; } /** * 保存文件数据 */ @ResponseBody @RequestMapping("/resFiles/insertFileData") public int insertFileData(Res_Files record, double savedsizenum,String filetype) { record.setFilesize(savedsizenum); record.setFiletype(filetype); TiHuanLuJin(record.getServerurl()); record.setServerurl(record.getServerurl().replace("temp/","")); return resFilesService.insertSelective(record); } //替换路径 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); } /** * 删除文件数据 */ @ResponseBody @RequestMapping("/resFiles/deleteFileData") public int deleteFileData(int fileid) { Res_Files resFiles=resFilesService.selectByPrimaryKey(fileid); if(resFiles==null){ return 0; }else { String path = sysConfig.getUploadPath() + resFiles.getServerurl(); DeleteFileUtils.deletefile(path); return resFilesService.deleteByPrimaryKey(fileid); } } /** * 文件上传 */ @ResponseBody @RequestMapping("/resFile/uploadfile") public String uploadfile(@RequestParam("myFile") MultipartFile file, String resourceid) { File desFile = null; if (file.isEmpty()) { return "No File"; } String uuid = UUID.randomUUID().toString().replaceAll("-", ""); String name = file.getOriginalFilename(); String fileType = name.substring(name.lastIndexOf(".")); String Filename = uuid + fileType; String size = FileUtils.byteCountToDisplaySize(file.getSize()); String webpath=""; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM"); String timePath = sdf.format(new Date().getTime()); timePath = timePath.replace("-", ""); Filename = uuid + fileType; String path = null; path = sysConfig.getUploadPath() + "temp\\FileSource\\wenjian\\"+timePath + "\\resfile\\"; File filePath = new File(path); // 创建对应的年月文件夹 if (!filePath.exists()) { filePath.mkdirs(); } //文件网络地址 webpath ="temp/FileSource/wenjian/"+ timePath + "/resfile/" + Filename; // 类型为文件指向文件 desFile = new File(path + Filename); String info = desFile.getAbsolutePath(); System.out.println("info:" + info); try { FileUtils.copyInputStreamToFile(file.getInputStream(), desFile); return "{'result':'1','Path':'"+webpath+"','Filename':'"+name.substring(name.lastIndexOf("\\")+1,name.lastIndexOf("."))+"','size':'"+size.substring(0,size.lastIndexOf(" "))+"','unit':'"+size.substring(size.lastIndexOf(" ")+1,size.length())+"','FileType':'"+fileType+"'}"; } catch (IOException e) { e.printStackTrace(); return "{'result':'0'}"; } } /** *加载Ext数据 */ @ResponseBody @RequestMapping("/resFiles/Getfiledata") public String Getfiledata(Integer resMainInfoId, Model datamodel, PageBean pageBean) { Page page = PageHelper.startPage(pageBean.getPage(), pageBean.getLimit()); //alter Xxx 20181212 List List = resFilesService.selectDataListForResourceid(resMainInfoId); int countNums = (int) ((Page) List).getTotal(); PageBean pageData = new PageBean<>(pageBean.getPage(), pageBean.getLimit(), countNums); pageData.setItems(List); String result = "{"; result += "\"Count\":" + countNums + ",\"list\":["; for (Res_Files model : List) { String SizeUnit=""; double Filesize=model.getFilesize(); if(Filesize>0&&Filesize<1000){ if(Filesize<1){SizeUnit=Filesize+"KB";} else{ SizeUnit=Filesize+""; SizeUnit=SizeUnit.substring(0,SizeUnit.length()-2)+"KB"; } } else if((Filesize/1000/1000)<1){ SizeUnit=(Filesize/1000)+""; SizeUnit=SizeUnit.substring(0,SizeUnit.length()-2)+"MB"; } else if((Filesize/1000/1000)>1){ SizeUnit=(Filesize/1000/1000)+""; SizeUnit=SizeUnit.substring(0,SizeUnit.length()-2)+"GB"; } else {SizeUnit="0";} result += "{ \"filename\":"+"\""+ model.getFilename() +"\""+ ","+"\"fileid\":"+"\""+ model.getFileid() +"\""+","+"\"filetype\":"+"\""+ model.getFiletype() +"\""+","+"\"filesize\":"+"\""+ SizeUnit +"\""+","+"\"serverurl\":"+"\""+ model.getServerurl() +"\""+"},"; } if (List.size() > 0) { result = result.substring(0, result.length() - 1); } result += "]}"; datamodel.addAttribute("pubzyWebRoot", sysConfig.getPubzyWebRoot()); return result; } }