UploadfileController.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package com.enteprise.uploadfile.controller;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.util.List;
  5. import javax.servlet.http.HttpServletResponse;
  6. import com.enteprise.common.config.RuoYiConfig;
  7. import com.enteprise.framework.config.ServerConfig;
  8. import org.springframework.security.access.prepost.PreAuthorize;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.web.bind.annotation.*;
  11. import com.enteprise.common.annotation.Log;
  12. import com.enteprise.common.core.controller.BaseController;
  13. import com.enteprise.common.core.domain.AjaxResult;
  14. import com.enteprise.common.enums.BusinessType;
  15. import com.enteprise.uploadfile.domain.Uploadfile;
  16. import com.enteprise.uploadfile.service.IUploadfileService;
  17. import com.enteprise.common.utils.poi.ExcelUtil;
  18. import com.enteprise.common.core.page.TableDataInfo;
  19. import org.springframework.web.multipart.MultipartFile;
  20. /**
  21. * uploadfileController
  22. *
  23. * @author ruoyi
  24. * @date 2024-09-16
  25. */
  26. @RestController
  27. @RequestMapping("/uploadfile/uploadfile")
  28. public class UploadfileController extends BaseController
  29. {
  30. @Autowired
  31. private IUploadfileService uploadfileService;
  32. @Autowired
  33. private ServerConfig serverConfig;
  34. /**
  35. * 查询uploadfile列表
  36. */
  37. @PreAuthorize("@ss.hasPermi('uploadfile:uploadfile:list')")
  38. @GetMapping("/list")
  39. public TableDataInfo list(Uploadfile uploadfile)
  40. {
  41. startPage();
  42. List<Uploadfile> list = uploadfileService.selectUploadfileList(uploadfile);
  43. return getDataTable(list);
  44. }
  45. /**
  46. * 导出uploadfile列表
  47. */
  48. @PreAuthorize("@ss.hasPermi('uploadfile:uploadfile:export')")
  49. @Log(title = "uploadfile", businessType = BusinessType.EXPORT)
  50. @PostMapping("/export")
  51. public void export(HttpServletResponse response, Uploadfile uploadfile)
  52. {
  53. List<Uploadfile> list = uploadfileService.selectUploadfileList(uploadfile);
  54. ExcelUtil<Uploadfile> util = new ExcelUtil<Uploadfile>(Uploadfile.class);
  55. util.exportExcel(response, list, "uploadfile数据");
  56. }
  57. /**
  58. * 获取uploadfile详细信息
  59. */
  60. @PreAuthorize("@ss.hasPermi('uploadfile:uploadfile:query')")
  61. @GetMapping(value = "/{id}")
  62. public AjaxResult getInfo(@PathVariable("id") Long id)
  63. {
  64. return success(uploadfileService.selectUploadfileById(id));
  65. }
  66. /**
  67. * 新增uploadfile
  68. */
  69. @PreAuthorize("@ss.hasPermi('uploadfile:uploadfile:add')")
  70. @Log(title = "uploadfile", businessType = BusinessType.INSERT)
  71. @PostMapping
  72. public AjaxResult add(@RequestBody Uploadfile uploadfile)
  73. {
  74. return toAjax(uploadfileService.insertUploadfile(uploadfile));
  75. }
  76. /**
  77. * 修改uploadfile
  78. */
  79. @PreAuthorize("@ss.hasPermi('uploadfile:uploadfile:edit')")
  80. @Log(title = "uploadfile", businessType = BusinessType.UPDATE)
  81. @PutMapping
  82. public AjaxResult edit(@RequestBody Uploadfile uploadfile)
  83. {
  84. return toAjax(uploadfileService.updateUploadfile(uploadfile));
  85. }
  86. /**
  87. * 删除uploadfile
  88. */
  89. @PreAuthorize("@ss.hasPermi('uploadfile:uploadfile:remove')")
  90. @Log(title = "uploadfile", businessType = BusinessType.DELETE)
  91. @DeleteMapping("/{ids}")
  92. public AjaxResult remove(@PathVariable Long[] ids)
  93. {
  94. return toAjax(uploadfileService.deleteUploadfileByIds(ids));
  95. }
  96. @PostMapping("/upload")
  97. public AjaxResult uploadFile(@RequestParam("file") MultipartFile file) {
  98. if (file.isEmpty()) {
  99. return AjaxResult.error("上传文件不能为空");
  100. }
  101. try {
  102. // 生成文件名(保留原始名称)
  103. String originalFilename = file.getOriginalFilename();
  104. String uploadPath = RuoYiConfig.getUploadPath();
  105. // 创建存储目录
  106. File destDir = new File(uploadPath);
  107. if (!destDir.exists()) {
  108. destDir.mkdirs();
  109. }
  110. // 保存文件
  111. File destFile = new File(destDir, originalFilename);
  112. file.transferTo(destFile);
  113. // 构建返回URL
  114. String url = serverConfig.getUrl() + "/" + originalFilename;
  115. return AjaxResult.success()
  116. .put("url", url)
  117. .put("fileName", uploadPath + "/" + originalFilename)
  118. .put("newFileName", originalFilename)
  119. .put("originalFilename", originalFilename);
  120. } catch (IOException e) {
  121. return AjaxResult.error("上传失败:" + e.getMessage());
  122. }
  123. }
  124. }