FrameWork/Spring

[Spring, JAVA] 파일 복사(FileChannel 이용)

밍글링글링 2017. 9. 21.
728x90

방식은 이러합니다.
SQL 쿼리문에서 파일정보를 가져옵니다.
saveNm과 fileNm 매칭하여 리스트들을 불러와 FileChannel객체를 이용하여 파일을 카피합니다.
이 방법이 local파일 카피속도는 제일 빠르다.(제 생각..)
filecopy 메소드에서 경로를 잘지정하고 DB에서 saveNm과 fileNm만 잘 가지고 오면된다.

Controller.java

@RequestMapping(value = "/fileDownloadY.do", method = RequestMethod.GET)
    public void filedownloadY(HttpServletRequest request, HttpServletResponse response
                                                                , @RequestParam(value="approval", required=false) String approval
                                                                , @RequestParam(value="receiptClf", required=false) String receiptClf
                                                                , @RequestParam(value="deleteYn",  required=false) String deleteYn
                                                                ) throws Exception {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("approval", approval);
            map.put("receiptClf", receiptClf);
            List<Map<String,Object>> fileList = communityService.selectFileListY(map);
            for(int i=0; i<fileList.size(); i++){
                String paramFileName = "";
                try{
                    paramFileName = StringUtil.isNullToString(fileList.get(i).get("saveNm"));
                    String oriFileName = StringUtil.isNullToString(fileList.get(i).get("fileNm"));
                    FileDownloadUtil fileDownloadUtil = new FileDownloadUtil();
                    fileDownloadUtil.downloadY(request, response, paramFileName, oriFileName, "");
                } catch(Exception e) {
                }
            }
    }
 

 

FileDownloadUtil.java

public void downloadY(HttpServletRequest request, HttpServletResponse response, String paramFileName,
            String oriFileName, String deleteYn) throws Exception {
        byte[] buffer = new byte[BUFF_SIZE];

        ServletOutputStream out_stream = null;
        BufferedInputStream in_stream = null;
        String filename = new String(paramFileName.getBytes(), CHARSET);

        if ((filename != null && filename.indexOf("..") > -1) || (filename != null && filename.indexOf("..") > -1)) {
            throw new RuntimeException("Not Authorized!!!");
        }

        File file = new File(UPLOAD_PATH_PRE + filename);

        if (file.length() > 0) {
            fileCopy(filename, filename);
        } else {
            System.out.println("unknown!!!!!!!!!!!!!!!!!!!!!");
        }
    }

public void fileCopy(String inFileName, String outFileName) {
        try {
            FileInputStream inputStream = new FileInputStream(UPLOAD_PATH_PRE + inFileName);
            FileOutputStream outputStream = new FileOutputStream("D:\\download\\일반\\" + outFileName);

            FileChannel fcin = inputStream.getChannel();
            FileChannel fcout = outputStream.getChannel();

            long size = fcin.size();
            fcin.transferTo(0, size, fcout);

            fcout.close();
            fcin.close();

            outputStream.close();
            inputStream.close();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
 

 

728x90

댓글