1.阿里云Oss上传
只需要一点就是上传到云端后,前端调用上传文件接口,返回一个资源路径。
接着在提交表单时,前端把这个路径设置为img的参数即可。
1.1上传限制 只上传图片
@Api("阿里云文件管理")
@CrossOrigin //跨域
@RestController
@RequestMapping("/eduoss/fileoss")
public class OssController {@ApiOperation(value = "文件上传")@PostMapping("upload")public Result upload( @ApiParam(name = "file",value = "文件",required = true) @RequestParam("file") MultipartFile file){// 判断文件类型是否为图片if (!file.getContentType().startsWith("image/")) {return new Result(4000, "只允许上传图片文件");}String filename = file.getOriginalFilename();String uploadUrl = OssUtils.upload(file,filename);//返回url对象return new Result(2000,"添加成功",uploadUrl);}}
2.后台返回token
当前端完成登录后,只有一个返回成功码这样容易伪造
所以在用户登录完成后返回一个token 才更安全
2.1 JWTutils
public class BgJwtUtils {private static String sign = "hrmy";//1.生成tokenpublic static String createToken(String name) throws UnsupportedEncodingException {Map<String,Object> header = new HashMap<String,Object>();header.put("alg","HS256");header.put("typ","JWT");Calendar calendar = Calendar.getInstance();//获取当前时间calendar.set(Calendar.HOUR,24);Date expire = calendar.getTime();//追加24小时候获取时间String token = JWT.create().withHeader(header).withIssuedAt(new Date()) //设置颁发时间.withExpiresAt(expire).withClaim("name",name).sign(Algorithm.HMAC256(sign));return token;}//2.校验令牌public static boolean validate(String token){try {JWT.require(Algorithm.HMAC256(sign)).build().verify(token);return true;}catch (Exception e){return false;}}//3.解析令牌public static String get(String token){String name = JWT.decode(token).getClaim("name").asString();return name;}
3.导出excel
3.1引入easyExcel依赖
<!--引入easyExcel--><dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId><version>2.2.3</version></dependency>
3.2在导出实体类中加入注解
@ApiModelProperty("创建时间")@ExcelProperty("注册时间")@JsonSerialize(using= CustomDateTimeSerializer.class)private Date createdAt;
3.3easyExcel书写
@ApiOperation("导出excel模板")@PostMapping("Userexcel")public Result uploadUserExcel(List<Users> list){String filename = "C:\\Users\\24421\\Desktop\\user.xlsx";EasyExcel.write(filename,Users.class).sheet("用户信息").doWrite(list);return new Result(2000,"写入成功");}