FileController.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /**
  2. * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
  3. */
  4. package com.jeeplus.modules.sys.web;
  5. import com.google.common.collect.Lists;
  6. import com.jeeplus.common.config.Global;
  7. import com.jeeplus.common.json.AjaxJson;
  8. import com.jeeplus.common.utils.FileUtils;
  9. import com.jeeplus.common.utils.StringUtils;
  10. import com.jeeplus.core.web.BaseController;
  11. import com.jeeplus.core.web.Servlets;
  12. import com.jeeplus.modules.sys.entity.FileData;
  13. import com.jeeplus.modules.sys.entity.User;
  14. import com.jeeplus.modules.sys.security.SystemAuthorizingRealm;
  15. import com.jeeplus.modules.sys.utils.UserUtils;
  16. import org.apache.ibatis.annotations.Param;
  17. import org.apache.shiro.authz.annotation.RequiresPermissions;
  18. import org.springframework.stereotype.Controller;
  19. import org.springframework.ui.Model;
  20. import org.springframework.web.bind.annotation.RequestMapping;
  21. import org.springframework.web.bind.annotation.ResponseBody;
  22. import org.springframework.web.multipart.MultipartFile;
  23. import javax.servlet.http.HttpServletRequest;
  24. import javax.servlet.http.HttpServletResponse;
  25. import java.io.*;
  26. import java.util.*;
  27. /**
  28. * 文件管理Controller
  29. * @author liugf
  30. * @version 2018-01-21
  31. */
  32. @Controller
  33. @RequestMapping(value = "${adminPath}/sys/file")
  34. public class FileController extends BaseController {
  35. public void init() {
  36. SystemAuthorizingRealm.Principal principal = (SystemAuthorizingRealm.Principal) UserUtils.getPrincipal();
  37. FileUtils.createDirectory(Global.getAttachmentDir());
  38. FileUtils.createDirectory(Global.getMyDocDir());
  39. FileUtils.createDirectory(Global.getShareBaseDir());
  40. }
  41. /**
  42. * 文件管理列表页面
  43. */
  44. @RequiresPermissions("user")
  45. @RequestMapping(value = {"list", ""})
  46. public String list() {
  47. init();
  48. return "modules/sys/file/fileManager";
  49. }
  50. /**
  51. * 文件管理列表数据
  52. */
  53. @ResponseBody
  54. @RequiresPermissions("user")
  55. @RequestMapping(value = "data")
  56. public List<FileData> data(HttpServletRequest request, HttpServletResponse response, Model model) {
  57. SystemAuthorizingRealm.Principal principal = (SystemAuthorizingRealm.Principal) UserUtils.getPrincipal();
  58. if (principal == null){
  59. return null;
  60. }
  61. List <File> targetFiles = Lists.newArrayList();
  62. targetFiles.add(new File(Global.getAttachmentDir()));
  63. targetFiles.add(new File(Global.getMyDocDir()));
  64. targetFiles.add(new File(Global.getShareBaseDir()));
  65. return FileUtils.getFileList("files", Lists.newArrayList(targetFiles));
  66. }
  67. /**
  68. * 移动文件或文件夹
  69. */
  70. @ResponseBody
  71. @RequiresPermissions("user")
  72. @RequestMapping(value = "move")
  73. public List move(@Param("source") String source, @Param("target") String target) throws IOException{
  74. List list = Lists.newArrayList();
  75. String[] sourceArra = source.split(",");
  76. for(String s:sourceArra){
  77. String fileName = StringUtils.substringAfterLast(s.replace("\\","/"),"/");
  78. if(FileUtils.isFolder(s)){
  79. File targetFolder = FileUtils.getAvailableFolder(target+"/"+fileName, 0);
  80. FileUtils.moveDirectory(new File(s),targetFolder);
  81. Map map = new HashMap();
  82. map.put("id",targetFolder.getAbsolutePath());
  83. map.put("value", targetFolder.getName());
  84. list.add(map);
  85. }else{
  86. File targetFile = FileUtils.getAvailableFile(target+"/"+fileName, 0);
  87. FileUtils.moveFile(new File(s), targetFile);
  88. new File(s).deleteOnExit();
  89. Map map = new HashMap();
  90. map.put("id",targetFile.getAbsolutePath());
  91. map.put("value", targetFile.getName());
  92. list.add(map);
  93. }
  94. }
  95. return list;
  96. }
  97. /**
  98. * copy文件文件夹
  99. */
  100. @ResponseBody
  101. @RequiresPermissions("user")
  102. @RequestMapping(value = "copy")
  103. public List copy(@Param("source") String source, @Param("target") String target) {
  104. List list = Lists.newArrayList();
  105. String[] sourceArra = source.split(",");
  106. for(String s:sourceArra){
  107. String fileName = StringUtils.substringAfterLast(s.replace("\\","/"),"/");
  108. if(FileUtils.isFolder(s)){
  109. File targetFolder = FileUtils.getAvailableFolder(target+"/"+fileName, 0);
  110. if(FileUtils.copyDirectory(s, targetFolder.getAbsolutePath())){
  111. Map map = new HashMap();
  112. map.put("id",targetFolder.getAbsolutePath());
  113. map.put("value", targetFolder.getName());
  114. list.add(map);
  115. }
  116. }else{
  117. File targetFile = FileUtils.getAvailableFile(target+"/"+fileName, 0);
  118. if(FileUtils.copyFile(s,targetFile.getAbsolutePath())){
  119. Map map = new HashMap();
  120. map.put("id",targetFile.getAbsolutePath());
  121. map.put("value", targetFile.getName());
  122. list.add(map);
  123. }
  124. }
  125. }
  126. return list;
  127. }
  128. /**
  129. * 删除文件管理
  130. */
  131. @RequiresPermissions("user")
  132. @RequestMapping(value = "download")
  133. public void download(@Param("source") String source, HttpServletRequest request, HttpServletResponse response) throws Exception{
  134. AjaxJson j = new AjaxJson();
  135. File file = new File(source);
  136. if (file == null || !file.exists()) {
  137. request.setAttribute("exception", new FileNotFoundException("请求的文件不存在"));
  138. request.getRequestDispatcher("/webpage/error/404.jsp").forward(request, response);
  139. }
  140. OutputStream out = null;
  141. try {
  142. response.reset();
  143. response.setContentType("application/octet-stream; charset=utf-8");
  144. String agent = (String)request.getHeader("USER-AGENT");
  145. String fileName = file.getName();
  146. if(agent != null && agent.indexOf("MSIE") == -1) {
  147. // FF
  148. String enableFileName = "=?UTF-8?B?" + (new String(Base64.getEncoder().encode(fileName.getBytes("UTF-8")))) + "?=";
  149. response.setHeader("Content-Disposition", "attachment; filename=" + enableFileName); }
  150. else {
  151. // IE
  152. String enableFileName = new String(fileName.getBytes("GBK"), "ISO-8859-1");
  153. response.setHeader("Content-Disposition", "attachment; filename=" + enableFileName);
  154. }
  155. // response.setHeader("Content-Disposition", "attachment; filename=" + file.getName());
  156. out = response.getOutputStream();
  157. out.write(FileUtils.readFileToByteArray(file));
  158. out.flush();
  159. } catch (IOException e) {
  160. e.printStackTrace();
  161. } finally {
  162. if (out != null) {
  163. try {
  164. out.close();
  165. } catch (IOException e) {
  166. e.printStackTrace();
  167. }
  168. }
  169. }
  170. }
  171. /**
  172. * 删除文件管理
  173. */
  174. @ResponseBody
  175. @RequiresPermissions("user")
  176. @RequestMapping(value = "remove")
  177. public List delete(@Param("source") String source) {
  178. List list = Lists.newArrayList();
  179. //先删除文件
  180. String[] sourceArra = source.split(",");
  181. for(String s:sourceArra){
  182. FileUtils.delFile(s);
  183. Map map = new HashMap();
  184. map.put("id",s);
  185. map.put("value", StringUtils.substringAfterLast(s.replace("\\","/"),"/"));
  186. list.add(map);
  187. }
  188. return list;
  189. }
  190. @ResponseBody
  191. @RequiresPermissions("user")
  192. @RequestMapping(value = "createFolder")
  193. public Map create(@Param("source") String source, @Param("target") String target) {
  194. Map map = new HashMap();
  195. String targetFolderPath = target + "/" + source;
  196. File targetFolder = FileUtils.getAvailableFolder(targetFolderPath, 0);
  197. boolean result = FileUtils.createDirectory(targetFolder.getAbsolutePath());
  198. if(result){
  199. map.put("id", targetFolder.getAbsolutePath());
  200. map.put("value", targetFolder.getName());
  201. }
  202. return map;
  203. }
  204. @ResponseBody
  205. @RequiresPermissions("user")
  206. @RequestMapping(value = "rename")
  207. public Map rename(@Param("source") String source, @Param("target") String target) {
  208. Map map = new HashMap();
  209. File sourceFile = new File(source);
  210. File targetFile = new File(sourceFile.getParent()+"/"+target);
  211. if(sourceFile.isDirectory()){
  212. targetFile = FileUtils.getAvailableFolder(targetFile.getAbsolutePath(),0);
  213. }else{
  214. targetFile = FileUtils.getAvailableFile(targetFile.getAbsolutePath(), 0);
  215. }
  216. boolean result = sourceFile.renameTo(targetFile);
  217. if(result){
  218. map.put("id", targetFile.getAbsolutePath());
  219. map.put("value", targetFile.getName());
  220. }
  221. return map;
  222. }
  223. /**
  224. * 上传文件
  225. * @return
  226. * @throws IOException
  227. * @throws IllegalStateException
  228. */
  229. @RequiresPermissions("user")
  230. @RequestMapping(value = "upload")
  231. @ResponseBody
  232. public Map upload( HttpServletRequest request, HttpServletResponse response,MultipartFile upload) throws IllegalStateException, IOException {
  233. User currentUser = UserUtils.getUser();
  234. String realPath = request.getParameter("target");
  235. Map map = new HashMap();
  236. // 判断文件是否为空
  237. if (!upload.isEmpty()) {
  238. // 文件保存路径
  239. // 转存文件
  240. FileUtils.createDirectory(realPath);
  241. String name = upload.getOriginalFilename();
  242. String filePath = realPath +"/"+ name;
  243. File newFile = FileUtils.getAvailableFile(filePath,0);
  244. upload.transferTo(newFile);
  245. map.put("id", newFile.getAbsolutePath());
  246. map.put("value", newFile.getName());
  247. map.put("type", FileUtils.getFileType(newFile.getName()));
  248. }
  249. return map;
  250. }
  251. /**
  252. * 获取文件网络地址
  253. * @return
  254. * @throws IOException
  255. * @throws IllegalStateException
  256. */
  257. @RequiresPermissions("user")
  258. @RequestMapping(value = "getUrl")
  259. @ResponseBody
  260. public AjaxJson getUrl(@Param("dir") String dir) throws IllegalStateException, IOException {
  261. AjaxJson j = new AjaxJson();
  262. String url = Global.transDirToUrl(dir);
  263. String type = FileUtils.getFileType(dir);
  264. // 判断文件是否为空
  265. j.put("url", url);
  266. j.put("type",type);
  267. return j;
  268. }
  269. /**
  270. * 上传文件
  271. * @return
  272. * @throws IOException
  273. * @throws IllegalStateException
  274. */
  275. @RequiresPermissions("user")
  276. @RequestMapping(value = "webupload/upload")
  277. @ResponseBody
  278. public AjaxJson webupload( HttpServletRequest request, HttpServletResponse response,MultipartFile file) throws IllegalStateException, IOException {
  279. AjaxJson j = new AjaxJson();
  280. User currentUser = UserUtils.getUser();
  281. String uploadPath = request.getParameter("uploadPath");
  282. Calendar cal = Calendar.getInstance();
  283. int year = cal.get(Calendar.YEAR);
  284. int month = cal.get(Calendar.MONTH )+1;
  285. SystemAuthorizingRealm.Principal principal = (SystemAuthorizingRealm.Principal) UserUtils.getPrincipal();
  286. String fileUrl = Global.getAttachmentUrl()+uploadPath+"/"+year+"/"+month+"/";
  287. String fileDir = Global.getAttachmentDir()+uploadPath+"/"+year+"/"+month+"/";
  288. // 判断文件是否为空
  289. if (!file.isEmpty()) {
  290. // 文件保存路径
  291. // 转存文件
  292. FileUtils.createDirectory(fileDir);
  293. String name = file.getOriginalFilename();
  294. String filePath = fileDir + name;
  295. File newFile = FileUtils.getAvailableFile(filePath,0);
  296. file.transferTo(newFile);
  297. j.put("id", newFile.getAbsolutePath());
  298. j.put("url", fileUrl+ newFile.getName());
  299. // Map map = new HashMap();
  300. // map.put("fileUrl",fileDir);
  301. // HttpResponse.doPostFile2(Global.getZjjUrl()+"/upload/",map,newFile);
  302. }
  303. return j;
  304. }
  305. /**
  306. * 批量删除文件管理
  307. */
  308. @ResponseBody
  309. @RequiresPermissions("user")
  310. @RequestMapping(value = "webupload/delete")
  311. public AjaxJson delFile(String id) {
  312. AjaxJson j = new AjaxJson();
  313. if(FileUtils.delFile(id)){
  314. j.setSuccess(true);
  315. j.setMsg("删除文件成功");
  316. }else{
  317. j.setSuccess(false);
  318. j.setMsg("删除文件失败");
  319. }
  320. return j;
  321. }
  322. }