본문 바로가기

study/java

설정된 비율로 이미지 업로드 하기 #java

  /*
     *  @author 박원쥬스
     *  @Description banner  inser update
      * @param fileName  file
     *  @return fileurl 
     */
      @RequestMapping(value="/config/bannerSQL") 
      public String bannerSQL(@RequestParam Map<String, Object> requestMap,ModelMap model
          ,InfoVo infoVo ,HttpSession session         
          , HttpServletResponse res,HttpServletRequest req) throws Exception {
      String userid = ""+session.getAttribute("userid"); 
      InetAddress ipAddress = InetAddress.getLocalHost();
        String ip = ipAddress.getHostAddress().toString();        
        UploadPath = gp.getProperty("upload.server.uploadPath"); 
      String directory = UploadPath;   
        int sizeLimit = 100*1024*1024;   
      MultipartRequest multi = new MultipartRequest(req, directory, sizeLimit, "UTF-8", new MyFileRenamePolicy() );
          
          String idx = multi.getParameter("idx"); 
          infoVo.setidx(multi.getParameter("idx"));
          infoVo.setATCH1(multi.getFilesystemName("ATCH1"));
          infoVo.setLINK1(multi.getParameter("LINK1"));
          infoVo.setPOSITION(multi.getParameter("POSITION")); 
          infoVo.setALT(multi.getParameter("ALT")); 
          infoVo.setDEL_YN(multi.getParameter("DEL_YN"));   
          infoVo.settemp1(multi.getParameter("temp1"));    
          
          int code = 0;
          if(multi.getFilesystemName("ATCH1")!=null) {
                     String fileName=multi.getFilesystemName("ATCH1");
                        BufferedImage originalImage = ImageIO.read(new File(UploadPath + fileName));  
                        double width1 = originalImage.getWidth();
                        double height1 =originalImage.getHeight();  
                        
                        String POSITION = multi.getParameter("POSITION");
                        
                        double setwidth=0;
                        double setheight=0;
                        
                        if(POSITION.equals("main") || POSITION.equals("bwaveMain")) {
                          setwidth=1280;
                          setheight=550;
                        }
if(POSITION.equals("footer")) {
 setwidth=200;
                          setheight=60;
}  

                       
log.debug("업로드 가로{}"+width1);
                    log.debug("업로드 세로{}"+height1);
                    log.debug("업로드 setwidth{}"+setwidth);
                    log.debug("업로드 setheight{}"+setheight);
                       
                        //(1200 / 1600) x 400 = 300
                        double result=0.0;
                        
                     
                         if(width1>height1) {
                         result =  (setheight * width1) /height1;
                         log.debug("width1 > height1 = result : "+result);
                         if(result < (setwidth - 30) || result > (setwidth+30)) {
                           res.setContentType("text/html; charset=UTF-8");
                          PrintWriter out = res.getWriter(); 
                          out.println("<script language='javascript'>");
                          out.println("alert('이미지가로비율이 맞지 않습니다."+result+"px사이즈  확인후 다시 업로드해 주십시요')"); 
                        
                          out.println("</script>"); 
                          out.flush();
                          code=1;
                         }
                          
                         }else {
                         result =  (setwidth * height1) /width1;
                         if(result < (setheight - 30) || result > (setheight+30)) {
                           res.setContentType("text/html; charset=UTF-8");
                          PrintWriter out = res.getWriter(); 
                          out.println("<script language='javascript'>");
                          out.println("alert('이미지세로 비율이  맞지 않습니다."+result+"px사이즈 확인후 다시 업로드해 주십시요')"); 
                           
                          out.println("</script>"); 
                          out.flush(); 
                          code=2;
                         } 
                         } 
                    }
          
          
          
          
          if ( idx.equals("") && idx.length()==0) {
           // 수정
            infoService.bannerInsert(infoVo);
          }else {
            //등록 
            infoService.bannerUpdate(infoVo);
          } 
          if(code==0) {
           log.debug("good");
          }
          
          
      return "redirect:/config/banner"; 
    }

'study > java' 카테고리의 다른 글

파일복사하기  (0) 2023.02.17
업로드 이미지 설정값의 비율과 같으면 업로드 되게  (0) 2023.02.13
Java 11 HttpClient (자바11 HttpClient) 기능 살펴 보기  (0) 2023.01.29
restAPI짭퉁  (0) 2022.12.30
JSTL  (0) 2022.12.30