java
InputStream을 파일로 저장
기계새
2013. 11. 1. 10:30
간단한 소스이나 메모.
finally에서 try catch를 한번 더 해줘야 하나.. 소스도 지저분해지고
개발서버의 경우 risk가 크지 않으므로 생략.
(실서버 또는 민감한 사항을 다루는곳에서는 추가.)
private void writeInputStreamToFile(String fileName, InputStream in) throws IOException {
OutputStream out = null;
try {
out = new FileOutputStream(fileName);
int read = 0;
byte[] bytes = new byte[1024];
while ((read = in.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
}
}