项目用户模块用到的技术

Thumbnailator是一个优秀的图片处理的开源Java类库

<dependency> <groupId>net.coobird</groupId> <artifactId>thumbnailator</artifactId> <version>0.4.8</version> </dependency>

Thumbnailator的github地址

Thumbnialator的api文档

size(宽度, 高度),比如    

 若图片横比200小,高比300小,不变   

 若图片横比200小,高比300大,高缩小到300,图片比例不变   

 若图片横比200大,高比300小,横缩小到200,图片比例不变   

 若图片横比200大,高比300大,图片按比例缩小,横为200或高为300   

//对图片进行缩放
Thumbnails.of(savepath+originName).size(120,120).toFile(savepath+savename);       //按比例缩放,测试图片为533*300,得到图片为120*68
//下面两句功能相同,都是把文件写进目录中
file.transferTo(new File(savepath + originName));
FileUtils.writeByteArrayToFile(new File(savepath + originName),file.getBytes());

头像文件上传


     //单个文件上传,方式一,利用MultipartHttpServletRequest来解析request中的文件
     //用 transferTo方法来保存图片,保存到本地磁盘。
    @JsonView(Avatar.AvatarView.class)
    @RequestMapping(value = "/settings/avatar",method = RequestMethod.POST)
    public Result uploadAvatar(HttpServletRequest request){
        //实例化一个文件解析器
        CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
        //判断request请求中是否有文件上传
        if(commonsMultipartResolver.isMultipart(request)){
            //转化request,普通request无法解析请求中的文件
            MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
            //获得文件
            MultipartFile file = multiRequest.getFile("file");
            if(file != null && !file.isEmpty()) {
                //获取图片的原始名称
                String originName = file.getOriginalFilename();
                //获取图片的后缀名
                String suffix = originName.substring(originName.lastIndexOf(".") + 1, originName.length());
                //重命名图片
                String savename = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + +new Random().nextInt(1000) + "." + suffix;
                //创建保存的目录,在服务器的根目录下
                String savepath = request.getSession().getServletContext().getRealPath("/upload/");
//            System.out.println(savepath);
                //如果保存路径不存在就创建一个
                File dir = new File(savepath);
                if (!dir.exists()) dir.mkdirs();
                User user = userService.findUser(userId);
                //如果存在头像就将其删除
                if (user.getAvatar() != null) {
                    System.out.println("有头像啦");
                    avatarService.deleteAvatar(user.getId());
                }
                try {
                    //将接受的文件保存到指定的文件中
                    file.transferTo(new File(savepath + savename));
                    //裁剪后的图片名
                    String newname = savename.substring(0,savename.lastIndexOf(".")) + "_120x120" + "." + suffix;
                    //对图片进行裁剪
                    Thumbnails.of(savepath + savename).sourceRegion(Positions.CENTER, 120, 120).size(120, 120).toFile(savepath + newname);
                    Avatar avatar = new Avatar(savename, newname, savepath);
                    avatarService.saveAvatar(userId, avatar);
                    return new Result(avatar);
                } catch (IOException e) {
                    File errorFile = new File(savepath+savename);
                    if(errorFile.exists())
                        errorFile.delete();
                    throw new FileUploadFailedException();
                }
            }
        }
        return new Result(Code.FILE_NOTFOUND);
    }

    //好处是简单,坏处是请求参数中的file不能为空,否则无法请求
    //单个文件上传,方式二,使用MultipartFile作为方法参数接收传入的文件
    //@RequestParam("file")中的file对应input标签中的name属性值 
    @RequestMapping(value = "/settings/avatar",method = RequestMethod.POST)
    public Result uploadAvatar(@RequestParam("file") MultipartFile file,HttpServletRequest request){
            if(!file.isEmpty()) {
            。。。。。。。。。   
        }
    }

参考这里

javaMail邮件开发,参考这里

将前端传来的字符串转换为枚举类型,参考这里

spring 管理事务,开启CGLIB动态代理,service层没有接口

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值