java功能需要

1.上传图片功能需要

pom文件阿里云oss依赖

<!--        阿里云oss文件存储依赖-->
<dependency>
    <groupId>com.aliyun.oss</groupId>
    <artifactId>aliyun-sdk-oss</artifactId>
    <version>3.15.1</version>
</dependency>

application配置文件

#OSS????
oss.endpoint=oss-cn-hangzhou.aliyuncs.com
oss.accessKeyId=LTAI5t6ijtMwjLXkaEPeDJPw
oss.accessKeySecret=x5oKEyjxl7GW2Va1knuOJW5e43cBqq
oss.bucketName=updata-test
oss.url=https://updata-test.oss-cn-hangzhou.aliyuncs.com

配置文件

<!--文件上传解析器-->
<bean id="multipartResolver"
      class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <!-- 设定文件上传的最大值为5MB,5*1024*1024 -->
    <property name="maxUploadSize" value="5242880"></property>
    <!-- 设定文件上传时写入内存的最大值,如果小于这个参数不会生成临时文件,默认为10240 -->
    <property name="maxInMemorySize" value="40960"></property>
</bean>

OssUtil工具类

public class OssUtil {
    //实例化Map的子类 Properties
    private static Properties properties =new Properties();

    static {
        //任意一个类的Class对象中都存在一个方法getResourceAsStream   可以把properties读成输入字节流
        InputStream inputStream = OssUtil.class.getResourceAsStream("/oss.properties");
        try {
            properties.load(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static  String uploadFile(String filePath, MultipartFile multipartFile){
        InputStream inputStream = null;
        OSS ossClient = null;
        try {
            //   springmvc中提供文件处理对象   multipartFile获取到上传文件的原始名称 filePath=a/b/c/   aaa.png
            String originalFilename = multipartFile.getOriginalFilename();
            //获取原文件名称的后缀   aaa.png     suffix=.png
            String suffix = originalFilename.substring(originalFilename.
                    lastIndexOf("."));
            //为了防止后上传的请求与先上传请求的文件名称一致,内容不同  ,被覆盖,所以一定要让保存到服务器上的文件名称不同
            String newFileName = UUID.randomUUID()+suffix;//uuid + 后缀
            //springmvc中提供文件处理对象   multipartFile 直接获取上传文件的输入流
            inputStream = multipartFile.getInputStream();
            // 创建OSSClient实例。
            ossClient = new OSSClientBuilder().build(properties.getProperty("oss.endpoint")
                    , properties.getProperty("oss.accessKeyId"),
                    properties.getProperty("oss.accessKeySecret"));
            // InputStream inputStream = new FileInputStream(filePath);
            // 创建PutObject请求。  //filePath+"/"+newFileName = a/b/c/8e3f7b40-41bc-4125-bbcf-97b38dad4a2d.docx
            ossClient.putObject( properties.getProperty("oss.bucketName"), filePath+"/"+newFileName, inputStream);
            //https://qy1681.oss-cn-hangzhou.aliyuncs.com/pzydir/pzy.txt
            return properties.getProperty("oss.url")+"/"+filePath+"/"+newFileName;
        } catch (Exception oe) {
            oe.printStackTrace();
        }  finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
            try {
                if(inputStream!=null){
                    inputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

}

Controller层功能调用

//    使用阿里云控制上传
@RequestMapping("/fileUpload")
public String fileUpload(MultipartFile file) throws IOException {
    String orname = file.getOriginalFilename();
    System.out.println(orname);
    System.out.println("上传文件成功");
    //上传到阿里云
    String url = OssUtil.uploadFile("ossTest", file);
    System.out.println(url);
    if(url !=null){
        return url;
    }
    return "上传失败";
}

 

 2. JWT令牌生成(Token)

 pom依赖需要

<!--        JWT令牌依赖-->
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
            <version>0.9.1</version>
        </dependency>

JWTUtil工具类

public class JwtUtils {
private static String signKey = "ixwixw";
private static long expire = 43200000L;
    /**
     * 生成jwt令牌
     */
    public static String generateJwt(Map<String, Object> claims){

      String jwt = Jwts.builder()
                .addClaims(claims)
              .signWith(SignatureAlgorithm.HS256,signKey)
              .setExpiration(new Date(System.currentTimeMillis() + expire))
              .compact();
      return jwt;
    }

 /**
     * 解析jwt令牌
     */
    public static Claims parseJWT(String jwt){
       Claims claims = Jwts.parser()
               .setSigningKey(signKey)
               .parseClaimsJws(jwt)
               .getBody();
       return claims;
    }
}

 

Controller层调用

HashMap<String, Object> jwt = new HashMap<>();
jwt.put("user",use);        //将对象user放入到JWT令牌中
String token = JwtUtils.generateJwt(jwt);
return ResultUtils.success(token);

 

3. 后端跨域需要

打开idea创建springboot项目中,创建config配置层,创建GlobalCorsConfig配置文件

@Configuration
public class GlobalCorsConfig implements WebMvcConfigurer{
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowCredentials(true)
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .maxAge(3600);
    }


}

 

 4.Redis配置使用需要

 https://www.cnblogs.com/9--1/p/17668139.html

【注:这篇文章会带领配置使用Redis】

 

 


 

posted @ 2023-09-14 19:36  九极致之术  阅读(12)  评论(0编辑  收藏  举报