linux上传logo头像图片,回显到页面。springboot

该博客介绍了一个在Linux系统中使用SpringBoot进行图片上传的方法,包括判断系统类型、设置目标路径、检查文件类型和大小,以及将图片保存到指定路径并更新数据库记录。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//本地windows系统,项目要部署在linux系统。

public Map<String, Object> uploadFile(HttpServletRequest request, MultipartFile uploadFile, Long id,String targetPath) {
        Map<String, Object> resultMap = new HashMap<>();
        String path = null;
        String os = System.getProperties().getProperty("os.name");

//判断系统是windows,还是linux系统
        if (!os.toLowerCase().startsWith("win")) {
            // linux
            path = targetPath;//-----/home/admin/upload
        } else {
            path = request.getSession().getServletContext().getRealPath("upload");
        }
        log.info("========path=========" + path);
        String originalFilename = uploadFile.getOriginalFilename();
        String fileTyle = originalFilename.substring(originalFilename.lastIndexOf(".") + 1, originalFilename.length());
        String newFileName = getFileNameStr() + "." + fileTyle;
        long size = uploadFile.getSize();
        String[] types = OrganizationConstant.IMG_TYPES;
        if (!Arrays.asList(types).contains(fileTyle.toUpperCase())) {
            throw new BusinessValidationException(
                    "请选择" + String.format("%s,%s,%s", OrganizationConstant.IMG_TYPES) + "图片类型");
        }
        if (size > OrganizationConstant.MAXSIZE) {
            throw new BusinessValidationException("图片大于" + OrganizationConstant.MAXSIZE / 1024 + "KB,请重新上传");
        }
        originalFilename = newFileName;
        File targetFile = new File(path, originalFilename);
        log.info("路径:" + path + originalFilename);
        File pathExist = new File(path);
        if (!pathExist.exists()) {
            pathExist.mkdirs();
        }
        try {
            uploadFile.transferTo(targetFile);
            SystemPlat system = new SystemPlat();
            system.setId(id);
            system.setLogoUrl(File.separator + originalFilename);
            systemMapper.updateSystem(system);
            resultMap.put("id", id);
            resultMap.put("success", true);
        } catch (Exception e) {
            throw new ServiceValidationException("上传图片出错!", e);
        }
        return resultMap;
    }
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值