xiaoyong931011
2022-06-06 8436adbda66025f53ff901a75af41b4528a005b2
src/main/java/com/xcong/farmer/cms/modules/system/Controller/AdminCommonController.java
@@ -2,31 +2,32 @@
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSONObject;
import com.xcong.farmer.cms.common.contants.AppContants;
import com.xcong.farmer.cms.common.response.Result;
import com.xcong.farmer.cms.common.system.bean.LoginUserBean;
import com.xcong.farmer.cms.common.system.dto.LoginDto;
import com.xcong.farmer.cms.configurations.properties.ApplicationProperties;
import com.xcong.farmer.cms.modules.system.dto.AdminBase64UploadDto;
import com.xcong.farmer.cms.modules.system.dto.AdminLoginDto;
import com.xcong.farmer.cms.modules.system.service.ICommonService;
import com.xcong.farmer.cms.modules.system.service.IUserService;
import com.xcong.farmer.cms.utils.RedisUtils;
import com.xcong.farmer.cms.utils.SpringContextHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.FileCopyUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import cn.hutool.core.util.ObjectUtil;
@RestController
@RequestMapping(value = "/api/common")
@@ -52,4 +53,103 @@
        return iCommonService.login(adminLoginDto);
    }
    /**
     * 用户退出登录
     * @return
     */
    @ApiOperation(value="用户退出登录", notes="用户退出登录")
    @GetMapping(value = "/Logout")
    public Result  memberLogout() {
        return iCommonService.memberLogout();
    }
    private Long maxSize = 1024*1024*100L;
//    /**
//     * 文件上传方法
//     */
//    @ApiOperation(value = "文件上传", notes = "文件上传")
//    @PostMapping(value = "/doUpload")
//    public Result doFileUpload(@RequestBody @Validated AdminBase64UploadDto uploadDto){
//        // 文件保存目录路径
//        String savePath = AppContants.PICTURE_PATH;
//        // 文件保存目录URL
//        String saveUrl = resourceUrl;
//        // 保存和访问路径检查
//        if (StrUtil.isEmpty(saveUrl) || StrUtil.isEmpty(savePath)) {
//            return Result.fail("文件上传失败");
//        }
//        // 检查目录
//        File uploadDir = new File(savePath);
//        if (!uploadDir.isDirectory()) {
//            uploadDir.mkdir();
//        }
//        log.info("uploadDto:" + uploadDto.getBase64Str());
//        BASE64Decoder decoder = new BASE64Decoder();
//        byte[] bytes = new byte[0];
//        try {
//            bytes = decoder.decodeBuffer(uploadDto.getBase64Str());
//        } catch (IOException e) {
//            return Result.fail("上传文件失败");
//        }
//
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
//        String ymd = sdf.format(new Date());
//        savePath += ymd + "/";
//        saveUrl += ymd + "/";
//        File dirFile = new File(savePath);
//        if (!dirFile.exists()) {
//            dirFile.mkdirs();
//        }
//        if (bytes.length > maxSize) {
//            return Result.fail("上传文件大小超过限制");
//        }
//        String newFileName = IdUtil.simpleUUID() + IdUtil.simpleUUID() + ".png";
//        File uploadedFile = new File(savePath, newFileName);
//        try {
//            FileCopyUtils.copy(bytes, uploadedFile);
//        } catch (Exception e) {
//            return Result.fail("上传文件失败");
//        }
//        log.info("saveUrl:" + saveUrl);
//        String visitPath = saveUrl + newFileName;
//        log.info("上传一个文件:" + newFileName);
//        log.info("访问路径:" + visitPath);
//
//        return Result.ok("上传成功",visitPath);
//    }
    @Value("${static.resource.url}")
    private String resourceUrl;
    @ApiOperation(value = "文件上传", notes = "文件上传")
    @PostMapping("/uploadFile")
    public Result uploadFile(@RequestParam("file") MultipartFile file) throws Exception {
        // 文件保存目录路径
        String savePath = AppContants.PICTURE_PATH;
        // 文件保存目录URL
        String saveUrl = resourceUrl;
        // 检查目录
        File uploadDir = new File(savePath);
        if (!uploadDir.isDirectory()) {
            uploadDir.mkdir();
        }
        //获得文件的后缀
        String filename = IdUtil.simpleUUID() + file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        File filepath = new File(savePath + filename);
        try {
            //存文件
            file.transferTo(filepath);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String visitPath = (saveUrl + filename);
        return Result.ok("上传成功",visitPath);
    }
}