静态文件上传服务器代码

阿里云oss
https://help.aliyun.com/document_detail/32011.html?spm=a2c4g.84788.0.0.5cd16d31Ws1xbQ

@Value("endpoint")
    private String endpoint;
    @Value("accessKeyId")
    private String accessKeyId;
    @Value("accessKeSecret")
    private String accessKeSecret;
    @Value("bucketName")
    private String bucketName;

    public String ossUploadFile(MultipartFile multipartFile){

        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeSecret);

        try {

            String originalFilename = multipartFile.getOriginalFilename();
            String filename = UUID.randomUUID().toString();
            String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
            String newFilename = filename + "." + suffix;

            String fileUrl = getDate("yyyy/MM/dd")+newFilename;

            ossClient.putObject(bucketName,fileUrl,multipartFile.getInputStream());
            return "https://"+bucketName+"."+endpoint+"/"+fileUrl;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }finally {
            ossClient.shutdown();
        }

    }

======================================
其他自研实现
service层

	@Value("${spring.servlet.multipart.location}")
    private String uploadFolder;  //这不是tomcat服务目录,别人不认识
    @Value("${spring.servlet.multipart.max-file-size}")
    private int size;

    public Map<String, Object> uploadFile(MultipartFile multipartFile,String path) throws Exception {

        if (multipartFile == null) {
            throw new Exception("文件为空请重新上传");
        }

        long size = multipartFile.getSize();
        if (size > this.size) {
            throw new Exception("文件过大上传失败");
        }
        //指定文件格式   后期可以变更
        String originalFilename = multipartFile.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));

        //文件名
        String originalFilename = UUID.randomUUID().toString() + suffix;
        if (suffix==null||(!suffix.equals("xlsx")||!suffix.equals("xls"))) {
            throw new Exception("文件异常上传失败");
        }

        //指定文件上传目录
        String date = getDate("yyyy/MM/dd");//日期目录
        File newFile = new File(uploadFolder + path , date );

        try {
            //目录不存在
            if (!newFile.exists()) newFile.mkdirs();

            //文件
            File newFileName = new File(newFile, originalFilename);
            //文件上传到指定的目录
            multipartFile.transferTo(newFileName);

            Map<String, Object> map = new HashMap<>();
            map.put("url",path +"/" + date +"/"  + originalFilename);
            map.put("size",size);
            map.put("ext",suffix);
            map.put("filename",originalFilename);
            return map;
        } catch (IOException e) {
            return null;
        }
    }

    private static String getDate(String pattern){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        return simpleDateFormat.format(new Date());
    }

mvc 拦截器配置类

@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {
    @Value("${file.staticPatternPath}")
    private String staticPatternPath;
    @Value("${file.uploadFolder}")
    private String uploadFolder;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//        registry.addResourceHandler("访问的路径").addResourceLocations("上传资源的路径");
        registry.addResourceHandler(staticPatternPath)
                .addResourceLocations(uploadFolder);

    }
}

配置文件中

spring.servlet.multipart.enabled=true
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB
spring.servlet.multipart.file-size-threshold=20MB
spring.servlet.multipart.location=""

file.staticPatternPath="/upload/**"  #存放路径
file.uploadFolder="/www/upload/"   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值