2015년 12월 3일 목요일

파일업로드 및 파일명 변경



  1. import java.io.BufferedReader;  
  2. import java.io.File;  
  3. import java.io.FileInputStream;  
  4. import java.io.FileNotFoundException;  
  5. import java.io.IOException;  
  6. import java.io.InputStreamReader;  
  7. import java.util.ArrayList;  
  8. import java.util.HashMap;  
  9. import java.util.StringTokenizer;  
  10. import java.util.UUID;  
  11.   
  12. public class RandomName {  
  13.     public static void main(String agrs[]) {  
  14.         ArrayList<HashMap<String, Object>> files = new ArrayList<HashMap<String, Object>>();  
  15.         HashMap<String, Object> map = new HashMap<String, Object>();  
  16.           
  17.         String extName = "";  
  18.         String fileName = "";  
  19.         String seq= "";  
  20.   
  21.         // 버퍼 생성  
  22.         BufferedReader br = null;  
  23.   
  24.         // Input 스트림 생성  
  25.         InputStreamReader isr = null;  
  26.   
  27.         // File Input 스트림 생성  
  28.         FileInputStream fis = null;  
  29.   
  30.         // File 경로  
  31.         File file = new File("C:\\files.txt");  
  32.   
  33.         // 버퍼로 읽어들일 임시 변수  
  34.         String temp = "";  
  35.   
  36.         try {  
  37.   
  38.             // 파일을 읽어들여 File Input 스트림 객체 생성  
  39.             fis = new FileInputStream(file);  
  40.   
  41.             // File Input 스트림 객체를 이용해 Input 스트림 객체를 생서하는데 인코딩을 UTF-8로 지정  
  42.             isr = new InputStreamReader(fis, "EUC-KR");  
  43.   
  44.             // Input 스트림 객체를 이용하여 버퍼를 생성  
  45.             br = new BufferedReader(isr);  
  46.   
  47.             // 버퍼를 한줄한줄 읽어들여 내용 추출  
  48.             int cnt = 0;  
  49.             while ((temp = br.readLine()) != null) {  
  50.                 StringTokenizer st = new StringTokenizer(temp, "&&");  
  51.                 while(st.hasMoreTokens()) {  
  52.                     if(cnt == 0) {  
  53.                         map = new HashMap<String, Object>();  
  54.                         map.put("seq", st.nextToken());  
  55.                         cnt++;  
  56.                     } else {  
  57.                         map.put("filename", st.nextToken());  
  58.                         files.add(map);  
  59.                         cnt = 0;  
  60.                     }  
  61.                 }  
  62.             }  
  63.               
  64.             for(int i=0; i<files.size(); i++) {  
  65.                 HashMap<String, Object> tempMap = new HashMap<String, Object>();  
  66.                 tempMap = files.get(i);  
  67.                 seq= tempMap.get("seq").toString();  
  68.                 extName = getFileExt(tempMap.get("filename").toString());  
  69.                 fileName = Long.toString(System.currentTimeMillis()) + "_" + getRandomStr(8)+"." +extName; //실제 저장되는 파일 이름.  
  70.                 File oriFile = new File("C:/Users/user/Desktop/upload/" + tempMap.get("filename").toString());  
  71.                 File convFile = new File("C:/Users/user/Desktop/upload/" + fileName);  
  72.                   
  73.                 oriFile.renameTo(convFile);  
  74.             }  
  75.   
  76.         } catch (FileNotFoundException e) {  
  77.             e.printStackTrace();  
  78.   
  79.         } catch (Exception e) {  
  80.             e.printStackTrace();  
  81.   
  82.         } finally {  
  83.             try {  
  84.                 fis.close();  
  85.             } catch (IOException e) {  
  86.                 e.printStackTrace();  
  87.             }  
  88.   
  89.             try {  
  90.                 isr.close();  
  91.             } catch (IOException e) {  
  92.                 e.printStackTrace();  
  93.             }  
  94.   
  95.             try {  
  96.                 br.close();  
  97.             } catch (IOException e) {  
  98.                 e.printStackTrace();  
  99.             }  
  100.   
  101.         }  
  102.     }  
  103.       
  104.     // 랜덤 문자열 반환  
  105.     public static String getRandomStr(int length) {  
  106.         String rtnVal = "";  
  107.         String str = UUID.randomUUID().toString();  
  108.           
  109.         if(str.length() > length)  
  110.             rtnVal = str.substring(0, length);  
  111.         return rtnVal;  
  112.     }  
  113.       
  114.     public static String getFileExt(String filename) {  
  115.         String ext = "";  
  116.         if(filename==null || "".equals(filename)) {  
  117.             ext = "";  
  118.         } else {  
  119.             if(filename.indexOf(".")>-1) {  
  120.                 ext = filename.substring(filename.lastIndexOf(".")+1, filename.length());  
  121.             }  
  122.         }  
  123.         return ext;  
  124.     }  
  125. }  

댓글 없음:

댓글 쓰기