道阻且长,行则将至,走慢一点没关系,不停下就|

Ac_c0mpany丶

园龄:3年7个月粉丝:6关注:3

2022-12-05 11:58阅读: 28评论: 0推荐: 0

【项目留坑】 图片上传


# 上传文件的路径,根据情况部署,自行修改
file.upload.dir=F:/imooc-mall-images/
# 文件名称支持的后缀集合
file.suffixList=.jpg,.png,.gif,.jpeg
/**
     * 上传图片
     * @param httpServletRequest
     * @param file
     * @return
     */
    @PostMapping("/admin/upload/file")
    public R uploadImage(HttpServletRequest httpServletRequest, @RequestParam("file") MultipartFile file) {
        // 获取图片的原文件名
        String originalFilename = file.getOriginalFilename();

        // 获取图片类型后缀
        String suffixName = FileUtils.getSuffix(originalFilename);
        // 不包含规定的后缀名则直接抛出异常
        List<String> strings = Arrays.asList(suffix.split(","));
        if (!strings.contains(suffixName)) {
            throw new ImoocMallException(ImoocMallExceptionEnum.PHOTO_NAME_ILLEGAL);
        }

        // 生成图片的UUID+后缀,即新的文件名
        String newFileName = FileUtils.getFileName(originalFilename);


        // 创建文件夹
        File fileDirectory = new File(Constant.FILE_UPLOAD_DIR);
        // 创建目标图片
        File destFile = new File(Constant.FILE_UPLOAD_DIR + newFileName);


        // 判断文件夹是否存在
        if (!fileDirectory.exists()) {
            // 尝试新建文件夹【fileDirectory.mkdir()会新建一个文件夹】
            if (!fileDirectory.mkdir()) {
                // 文件夹创建失败:比如权限不够
                throw new ImoocMallException(ImoocMallExceptionEnum.MKDIR_FAILED);
            }
        }

        // 文件夹有了,现在上传图片
        try {
            // 把传入的图片写入到destFile中(把请求中的文件放入到本地目录文件夹中)
            file.transferTo(destFile);
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            // 把图片地址返回出去(此时要静态资源映射)
            return R.success(getHost(new URI(httpServletRequest.getRequestURL() + "")) + "/images/"
                    + newFileName);
        } catch (URISyntaxException e) {
            return R.error(ImoocMallExceptionEnum.PHOTO_UPLOAD_FAILED);
        }
    }

    // 获取当前ip和端口号
    private URI getHost(URI uri) {
        URI effectiveURI;
        try {
            effectiveURI = new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(),
                    null, null, null);
        } catch (URISyntaxException e) {
            effectiveURI = null;
        }
        return effectiveURI;
    }

静态资源映射:静态资源到本地目录的映射

package com.kyk.imoocmall.config;

import com.kyk.imoocmall.common.Constant;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 描述:配置地址映射
 */
@Configuration
public class ImoocMallWebMvcConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/images/**")
                .addResourceLocations("file:" + Constant.FILE_UPLOAD_DIR);
    }
}


https://blog.csdn.net/xiaochen_2715/article/details/125450681

本文作者:Ac_c0mpany丶

本文链接:https://www.cnblogs.com/keyongkang/p/16951921.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   Ac_c0mpany丶  阅读(28)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起
  1. 1 You Are My Sunshine REOL
You Are My Sunshine - REOL
00:00 / 00:00
An audio error has occurred.

作曲 : Traditional

You are my sunshine

My only sunshine.

You make me happy

When skies are gray.

You'll never know, dear,

How much I love you.

Please don't take my sunshine away

The other night, dear,

When I lay sleeping

I dreamed I held you in my arms.

When I awoke, dear,

I was mistaken

So I hung my head and cried.

You are my sunshine,

My only sunshine.

You make me happy

When skies are gray.

You'll never know, dear,

How much I love you.

Please don't take my sunshine away.

You are my sunshine,

My only sunshine

You make me happy

When skies are gray.

You'll never know, dear

How much I love you

Please don't take my sunshine away

Please don't take my sunshine away.

Please don't take my sunshine away.