[ JSP ] cos.jar(오라일리)의 파일 업로드(한글 깨짐 수정)
int maxSize = 1024*1024*30; // 한 번에 업로드 한도는 30M
String root=request.getSession().getServletContext().getRealPath("/");
//웹 루트 구하기
String savePath = root+"uploadFile/";
//업로드 되는 파일의 저장 경로
String uploadFile="";
try{
MultipartRequest multi= new MultipartRequest(request,savePath,maxSize,
new DefaultFileRenamePolicy());
//오라일리의 cos.jar파일 업로드를 위한 준비
uploadFile=multi.getFilesystemName("uploadFile");
//업로드될 파일 이름
File oldFile = new File(savePath+uploadFile);
uploadFile= new String(uploadFile.getBytes("8859_1"),"EUC-KR");
//업로드 될 파일 이름 한글화(웹상에서)
//한글파일명이 깨진것을 수정한다.
File newFile = new File(savePath+uploadFile);
if(!oldFile.renameTo(newFile)){
byte[] buf = new byte[1024];
FileInputStream fin = new FileInputStream(oldFile);
FileOutputStream fout = new FileOutputStream(newFile);
int read = 0;
while((read=fin.read(buf,0,buf.length))!=-1){
fout.write(buf, 0, read);
}
fin.close();
fout.close();
oldFile.delete();
}
}catch(Exception e){e.printStackTrace();}