StringBoot根据后缀名判断上传图片视频

文件或者视频上传如下代码

  /**
     *
     * @param request
     * @param upload_Name 文件
     * @param upload_Name_File 文件描述
     * @return
     */
    //上传文件
    @RequestMapping(value="/insertFileName.action")
    @ResponseBody
    public Response insertFileName(HttpServletRequest request ,@RequestParam("upload_Name") MultipartFile upload_Name,String upload_Name_File) throws  IOException{
        Response response = new Response ();
        if(StringUtils.isBlank ( upload_Name_File )){
            response.setError ( "文件名称不可以为空!" );
            response.setSuccess ( false );
            return  response;
        }
        if(upload_Name.getOriginalFilename ().isEmpty ()){
            response.setError ( "上传文件不可为空请重新选择!" );
            response.setSuccess ( false );
            return  response;
        }

        //获取到文件得名称然后从文件的最后的一个点(.)开始截取,截取后的内容 也就是(.mp3 .mp4  .png 等等)
        String lastIndexEnd = upload_Name.getOriginalFilename ().substring (upload_Name.getOriginalFilename ().lastIndexOf ( "." ));

        //全部转换成小写再比较
        if(lastIndexEnd != null && (".jpg".equals ( lastIndexEnd.toLowerCase () )  || lastIndexEnd.toLowerCase ().equals ( ".png" ) || ".mp4".equals ( lastIndexEnd.toLowerCase ()) )){
            //上传的路径  Config.upload_url 收我们规定存储文件得地址  如 D:\404
            String  File_url= Config.upload_url + DateUtil.getDateFormat(new Date (),"yyyyMMddHHmm")+upload_Name.getOriginalFilename ();
            //上传文件
            String Img_File = DateUtil.getDateFormat(new Date (),"yyyyMMddHHmm")+ upload_Name.getOriginalFilename ();
            response = softwareManagementDao.insertFileName(request,File_url,Img_File,upload_Name_File);//把相关的内容到到数据库
            if(response.getSuccess ()){
                upload_Name.transferTo ( new File (  File_url) );//上传方法
                System.err.println ( "上传成功!!" );
            }
        }else{
            response.setError ( "请上传jpg或png格式的图片或mp4格式的视频!! " );
            response.setSuccess ( false );
            return  response;
        }
        return  response;
    }

转载于:https://www.cnblogs.com/ysySelf/p/11088270.html

你可能感兴趣的:(StringBoot根据后缀名判断上传图片视频)