JAVA/JSE
[nio] Channels 클래스
tornado
2005. 1. 17. 11:20
간단한 회사 웹 메일 만들던 중 메일 발송 부분에서 자료 첨부를 Struts 의 FormBean 을 이용해서
파일을 보내는데 nio 를 이용해 보려고 했다.
폼 파일에서는 FormFile.getInputStream() 이라는 것만 제공해 주는데..
무식하게 스리 getInputStream() 으로 반납되는 InputStream 을 FileInputStream 으로 바꾸려
뻘짓거리를 ㅡㅡ;;
그래서 API 를 뒤져보니.. java.nio.channels 클래스를 보니 newChannel() 메소드 발견~
아래와 같이 처리 함.
File file = new File("경로","파일명");
InputStream is = formFile.getInputStream();
ReadableByteChannel byteChannel = Channels.newChannel( is )
FileChannel outputChannel = new FileOutputStream(file).getChannel();
outputChannel.transferFrom(byteChannel, 0, is.available());
일단 되는것 확인...
Channels 클래스에 InputStream, OutputStream, Reader, Writer 로 변환해 주는게 다 모여있넹.
대충 알았으니 이제 놀자 ㅋㅋ