服务器、本地文件上传预览

文件上传到服务器本地,然后预览图片

接口

    @Value("${file.winFilePath}")
    private  String winFilePath;
    @Value("${file.linuxFilePath}")
    private  String linuxFilePath;
    @Value("${file.fileIp}")
    private  String fileIp;
    @Value("${file.filePort}")
    private  String filePort;
    
public String localUpload(MultipartFile file) {
        if (file == null) {
            throw new RuntimeException("文件不能为空");
        }
        //后缀
        String extension = "." + FilenameUtils.getExtension(file.getOriginalFilename());
        String cruDate = DateUtil.format(new Date(), "yyyyMMdd");
        String uuid = UUID.randomUUID().toString();
        String newFileNamePrefix = cruDate + uuid;
        //新文件名
        String newFileName = newFileNamePrefix + extension;
        //判断jar包所在的服务器
        String oss = System.getProperty("os.name").toLowerCase();
        String uploadPath = "";
        if (oss.startsWith("win")) {
            uploadPath= winFilePath;
        } else {
            uploadPath=linuxFilePath;
        }
        String dateDirPath = uploadPath + "/" + cruDate;
        System.out.println(dateDirPath);
        File dir = new File(dateDirPath);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        try {
            file.transferTo(new File(dir, newFileName));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  fileIp+":"+filePort + "/upload/"+cruDate+"/"+newFileName;
    }

WebConfiguration拦截

package com.chanlian.canteen.admin.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.File;

@Configuration
@Slf4j
public class WebConfiguration implements WebMvcConfigurer {

    @Value("${file.winFilePath}")
    private String winFilePath;

    @Value("${file.linuxFilePath}")
    private String linuxFilePath;
    @Value("${file.mappingUrl}")
    private String mappingUrl;

    /**
     * 允许跨域
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
                .allowCredentials(true)
                .maxAge(3600)
                .allowedHeaders("*");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");

        String uploadUrl=null;
        String oss = System.getProperty("os.name").toLowerCase();
        if (oss.startsWith("win")) {
            uploadUrl= winFilePath;
        } else {
            uploadUrl=linuxFilePath;
        }
        registry.addResourceHandler(mappingUrl+"/**").addResourceLocations("file:"+ uploadUrl + File.separator);
        System.out.println("file:"+ uploadUrl + File.separator);
    }
}

接口放行

**放行/upload/接口

{
        // 注册 Sa-Token 拦截器,定义详细认证规则
        registry.addInterceptor(new SaInterceptor(handler -> {
            // 指定一条 match 规则
            SaRouter
                    .match("/**")    // 拦截的 path 列表,可以写多个 */
//                    .notMatch("/**")    // 拦截的 path 列表,可以写多个 */
                    .notMatch("/login/password")        // 排除掉的 path 列表,可以写多个
                    .notMatch("/isLogin")        // 排除掉的 path 列表,可以写多个
                    .notMatch("/upload/**")         // 排除掉的 path 列表,可以写多个
                    .notMatch("**/swagger-ui.html",
                            "/swagger-resources/**",
                            "/webjars/**",
                            "/v2/**",
                            "/swagger-ui.html/**",
                            "/doc.html/**",
                            "/error",
                            "/favicon.ico",
                            "sso/auth",
                            "/csrf")        // 排除掉的 path 列表,可以写多个
                    .check(r -> StpUtil.checkLogin());        // 要执行的校验动作,可以写完整的 lambda 表达式

         
        })).addPathPatterns("/**");
    }

上传文件接口返回,直接请求返回地址即可访问(本机测试所以返回127.0.0.1):
在这里插入图片描述
实际上传地址:
D:\file\20231204

  • 10
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值