123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- package com.enteprise.uploadfile.controller;
- import java.io.File;
- import java.io.IOException;
- import java.util.List;
- import javax.servlet.http.HttpServletResponse;
- import com.enteprise.common.config.RuoYiConfig;
- import com.enteprise.framework.config.ServerConfig;
- import org.springframework.security.access.prepost.PreAuthorize;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.*;
- import com.enteprise.common.annotation.Log;
- import com.enteprise.common.core.controller.BaseController;
- import com.enteprise.common.core.domain.AjaxResult;
- import com.enteprise.common.enums.BusinessType;
- import com.enteprise.uploadfile.domain.Uploadfile;
- import com.enteprise.uploadfile.service.IUploadfileService;
- import com.enteprise.common.utils.poi.ExcelUtil;
- import com.enteprise.common.core.page.TableDataInfo;
- import org.springframework.web.multipart.MultipartFile;
- /**
- * uploadfileController
- *
- * @author ruoyi
- * @date 2024-09-16
- */
- @RestController
- @RequestMapping("/uploadfile/uploadfile")
- public class UploadfileController extends BaseController
- {
- @Autowired
- private IUploadfileService uploadfileService;
- @Autowired
- private ServerConfig serverConfig;
- /**
- * 查询uploadfile列表
- */
- @PreAuthorize("@ss.hasPermi('uploadfile:uploadfile:list')")
- @GetMapping("/list")
- public TableDataInfo list(Uploadfile uploadfile)
- {
- startPage();
- List<Uploadfile> list = uploadfileService.selectUploadfileList(uploadfile);
- return getDataTable(list);
- }
- /**
- * 导出uploadfile列表
- */
- @PreAuthorize("@ss.hasPermi('uploadfile:uploadfile:export')")
- @Log(title = "uploadfile", businessType = BusinessType.EXPORT)
- @PostMapping("/export")
- public void export(HttpServletResponse response, Uploadfile uploadfile)
- {
- List<Uploadfile> list = uploadfileService.selectUploadfileList(uploadfile);
- ExcelUtil<Uploadfile> util = new ExcelUtil<Uploadfile>(Uploadfile.class);
- util.exportExcel(response, list, "uploadfile数据");
- }
- /**
- * 获取uploadfile详细信息
- */
- @PreAuthorize("@ss.hasPermi('uploadfile:uploadfile:query')")
- @GetMapping(value = "/{id}")
- public AjaxResult getInfo(@PathVariable("id") Long id)
- {
- return success(uploadfileService.selectUploadfileById(id));
- }
- /**
- * 新增uploadfile
- */
- @PreAuthorize("@ss.hasPermi('uploadfile:uploadfile:add')")
- @Log(title = "uploadfile", businessType = BusinessType.INSERT)
- @PostMapping
- public AjaxResult add(@RequestBody Uploadfile uploadfile)
- {
- return toAjax(uploadfileService.insertUploadfile(uploadfile));
- }
- /**
- * 修改uploadfile
- */
- @PreAuthorize("@ss.hasPermi('uploadfile:uploadfile:edit')")
- @Log(title = "uploadfile", businessType = BusinessType.UPDATE)
- @PutMapping
- public AjaxResult edit(@RequestBody Uploadfile uploadfile)
- {
- return toAjax(uploadfileService.updateUploadfile(uploadfile));
- }
- /**
- * 删除uploadfile
- */
- @PreAuthorize("@ss.hasPermi('uploadfile:uploadfile:remove')")
- @Log(title = "uploadfile", businessType = BusinessType.DELETE)
- @DeleteMapping("/{ids}")
- public AjaxResult remove(@PathVariable Long[] ids)
- {
- return toAjax(uploadfileService.deleteUploadfileByIds(ids));
- }
- @PostMapping("/upload")
- public AjaxResult uploadFile(@RequestParam("file") MultipartFile file) {
- if (file.isEmpty()) {
- return AjaxResult.error("上传文件不能为空");
- }
- try {
- // 生成文件名(保留原始名称)
- String originalFilename = file.getOriginalFilename();
- String uploadPath = RuoYiConfig.getUploadPath();
- // 创建存储目录
- File destDir = new File(uploadPath);
- if (!destDir.exists()) {
- destDir.mkdirs();
- }
- // 保存文件
- File destFile = new File(destDir, originalFilename);
- file.transferTo(destFile);
- // 构建返回URL
- String url = serverConfig.getUrl() + "/" + originalFilename;
- return AjaxResult.success()
- .put("url", url)
- .put("fileName", uploadPath + "/" + originalFilename)
- .put("newFileName", originalFilename)
- .put("originalFilename", originalFilename);
- } catch (IOException e) {
- return AjaxResult.error("上传失败:" + e.getMessage());
- }
- }
- }
|