FileController.java 11 KB

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