스프링

다운로드

    

 @RequestMapping("/down.do")
    public void fileName(@RequestParam Integer idx, @RequestParam String fileName,
            HttpServletRequest request, HttpServletResponse response) throws Exception{
        
        UploadPath.attach_path="WEB-INF//uploads";
        String uploadDir =UploadPath.path(request);
        logger.info(" getParameter : " + fileName);
        
        //이미지 일 경우 원본이미지로 주소 변경
        String formatName=fileName.substring(fileName.lastIndexOf(".")+1);
        MediaType mType=MediaUtils.getMediaType(formatName);
        if(mType!=null){
            //원본 이미지 다운로드
            String front=fileName.substring(0, 12);
            String end=fileName.substring(14);
            fileName=front+ end;
        }
        logger.info(" 다운로드할  파일 이름" + fileName);
        logger.info(" 다운로드 할  파일 주소  + 이름 " + uploadDir+fileName.replace('/',    File.separatorChar));
        //다운로드 시작
        //스트림을 이용하여 서버의 파일을 클라이언트에게 전송
        File file =new File(uploadDir+fileName.replace('/', File.separatorChar));
        
        
        //파일 의 header정보
        response.setContentType("application/octet-strem");
        response.setContentLength((int)file.length());
        
        //파일이름은 인코딩를 해야 함(한글 처리)
        //"_원본이름.jpg 마지막 슬러시 기준으로 원본이름 추출
        String hangule =fileName.substring(fileName.lastIndexOf("_")+1);
        logger.info( " hangule :"  +hangule);;
        response.setHeader("Content-Disposition", 
                "attachment; filename=\""+new String(hangule.getBytes("UTF-8"), "ISO-8859-1") +"\"");

        //binary 전송합니다
        response.setHeader("Content-Transfer-Encoding", "binary");
        
        // 실제 첨부파일 전송
        OutputStream out =response.getOutputStream();
        FileInputStream fis =null;
        try{
            fis =new FileInputStream(file);
            
            //서버의 파일을 클라이언트로 전송
            FileCopyUtils.copy(fis, out);
    
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            if(fis!=null){
                try{
                    fis.close();
                }catch(Exception e2){
                    e2.printStackTrace();
                }
            }
        }
        out.flush(); //버퍼 비우기(다운로드 완료)
    
        try{
            logger.info("//다운로드 횟수 증가");
            //다운로드 횟수 증가
            boardService.downCont(idx);
            
        }catch(Exception e){
            e.printStackTrace();
            logger.info("다운로드 증가 오류");
        }
    }


    

 

about author

PHRASE

Level 60  머나먼나라

아버지 나를 낳으시고 어머니 나를 기르시니, 슬프다. 부모님이여, 나를 낳아 기르시느라 애쓰고 수고하셨도다. 그 은혜 갚고자 한다면 그 은혜가 넓은 하늘과 같이 끝이 없다. -시경

댓글 ( 4)

댓글 남기기

작성

스프링 목록    more