java 上传图片或文件到服务器 微信小程序上传图片。获取图片宽高比

话不多说,直接上代码,

这是封装好的静态方法直接使用就行。

/**
     * 将文件流保存在磁盘上,为防止文件重名,文件名前面增加了时间戳,最终路径以返回的文件路径为准。
     *
     * @param dir   上传地址
     * @param fileName   文件名称
     * @param is    输入流
     * @return
     * @throws ServiceException
     */
    public static String saveFile(String dir, String fileName, InputStream is) throws ServiceException
    {

        // 目录是否存在
        File fileDir = new File(dir);
        if (!fileDir.exists())
        {
            fileDir.mkdirs();
        }

        // 文件名增加时间戳
        String saveFileName = System.currentTimeMillis() + fileName;

        String destFilePath = dir + saveFileName;

        try
        {

            FileOutputStream out = new FileOutputStream(new File(destFilePath));

            int length = 0;
            byte[] buf = new byte[1024];

            while ((length = is.read(buf)) != -1)
            {
                out.write(buf, 0, length);

            }

            is.close();
            out.close();
        }
        catch (FileNotFoundException e)
        {
            throw new ServiceException("文件读写异常:" + e.getMessage(), e.getCause());
        }
        catch (IOException e)
        {

        }

        return saveFileName;
    }

使用方式:

@ResponseBody
    @RequestMapping(value = "/uploadImage", method = RequestMethod.POST,produces = "application/json")
    public Map uploadImage(HttpServletRequest request) throws IOException {
        // 接受文件流

        MultipartHttpServletRequest req =(MultipartHttpServletRequest)request;
        MultipartFile file =  req.getFile("file");

        // 微信小程序验证图片  开始   (如果不需要直接删除)
        String accessToken = BaseFun.getToken(tokenMapper,appId,secret);
        Boolean res = SafeUtil.imgSecCheck(file,accessToken); // 具体验证方法看下一篇文章
        if (!res) {
            return rtnParam(1001,"图片未通过验证,请更换图片");
        }

        // 微信小程序验证图片  结束   (如果不需要直接删除)

        if(!file.isEmpty()){
            // 检查上传文件的类型
            if (!FileUtil.checkSpecialFileType(file.getContentType())) {
                try {
                    throw new ServiceException(
                            "上传的图片类型错误,只能上传JPG,PNG,GIF,JPEG类型!", new Throwable());
                } catch (ServiceException e) {
                    e.printStackTrace();
                }
            }
            // 获取图片宽高比  开始
            BufferedImage image = ImageIO.read(file.getInputStream());
            int width = image.getWidth();
            int height = image.getHeight();
            System.out.println(image);
            System.out.println(width);
            System.out.println(height);
            DecimalFormat df = new DecimalFormat("#.00");
            Double widthBiHeight = Double.parseDouble(df.format(width/height));
            System.out.println(widthBiHeight);


            // 获取图片宽高比  结束

            String fileType = FileUtil.getMyFileType(file.getContentType());
            //  给需要上传的文件命名(我这是图片) 
            String fileName;
            if (!"".equals(fileType)){
                fileName = getRandom(1000000000,1).toString()+""+fileType;
            }else{
                fileName = getRandom(1000000000,1).toString()+".png";
            }
            InputStream in = file.getInputStream();
//
            String filePath = null;
//            // 上传文件文件
            Map map = new HashMap();
            map.put("widthBiHeight",widthBiHeight);


            try {

                //  调用上传方法
                String saveFileName = FileUtil.saveFile(baseDir,fileName, in);
                map.put("fileName",saveFileName);
                return map;
            } catch (ServiceException e) {
                e.printStackTrace();
            }
        }

        return rtnParam(1001,"图片");
    }

 

你可能感兴趣的:(微信小程序,java,java,小程序)