2012-10-26

Java] file channel을 이용한 파일 복사, 파일 다운로드




// file channel 을 이용한 파일 복사

public void fileCopy(String src, String target) throws IOException {

FileInputStream inputStream = new FileInputStream(src);        

FileOutputStream outputStream = new FileOutputStream(target);



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();

}

// request, response 를 이용한 파일 다운로드 기능 구현...

// 출처 : http://hatssarjy.tistory.com/entry/Jsp-FileDownload

public void downloadExec(HttpServletRequest req,HttpServletResponse resp,

String fn) {

resp.setContentType("application/octet-stream");

String filename = fn;

String filename1;

try {

filename1 = new String(fn.getBytes("ISO-8859-1"),"euc-kr");

String filename2 = java.net.URLEncoder.encode(filename1, "UTF8");

LConfiguration conf = LConfiguration.getInstance();

String absPath = conf.getDevonHome().replaceAll("devonhome", "")+"/web/upload"; //  <=== context의 경로

resp.setHeader("Content-Disposition","attachment; filename=\""+filename2+"\";");

//File file = new File ("upload/"+filename);

File file = new File (absPath+"/"+filename);

byte[] bytestream = new byte[(int)file.length()];

FileInputStream filestream = new FileInputStream(file);

int i = 0, j = 0;

while((i = filestream.read()) != -1) {

bytestream[j] = (byte)i;

j++;

}

OutputStream outStream = resp.getOutputStream();

outStream.write(bytestream);                                        

outStream.close();

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (DevonException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}



      // 위의 메소드를 개선한 메소드.

public void downloadExec2(HttpServletRequest req,HttpServletResponse resp,

String fn) {

LConfiguration conf;

try {

conf = LConfiguration.getInstance();

String absPath = conf.getDevonHome().replaceAll("devonhome", "")+"/web/upload"; //로컬경로

String change_name = StringUtils.defaultString(fn);

String real_name = StringUtils.defaultString(fn);

//실제 저장소의 파일명

String fileName = "/upload"+"/"+change_name;

// File file = new File(fileName);

File file = new File (absPath+"/"+real_name);

int fileSize = (int)file.length();

resp.setContentType("application/octet-stream");

resp.setHeader("Content-Disposition", "attachment;filename="+real_name+";");

resp.setContentLength(fileSize);

resp.setHeader("Content-Transfer-Encoding", "binary;");

resp.setHeader("Pragma", "no-cache;");

resp.setHeader("Expires", "-1;");

resp.setHeader("Cache-Control", "cache, must-revalidate");

if(fileSize >0 && file.isFile()){

BufferedInputStream fin;

//fin = new BufferedInputStream(new FileInputStream(fileName));

fin = new BufferedInputStream(new FileInputStream(file));

BufferedOutputStream outs = new BufferedOutputStream(resp.getOutputStream());

byte b[] = new byte[fileSize];

int read = 0;

try{

while((read=fin.read(b)) != -1){

outs.write(b,0,read);

}

}finally{

outs.close();

fin.close();

}

}

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (DevonException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}
;

댓글 없음:

댓글 쓰기