2017. 9. 27. 11:13, DEV/java
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | package common; import java.io.BufferedOutputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.URL; import java.net.URLConnection; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; public class FileLocalDownload { /** * 버퍼 사이즈 */ final static int size = 1024; public static void fileUrlReadAndDownload(String fileAddress, String localFileName, String downloadDir) { OutputStream outStream = null; URLConnection uCon = null; InputStream is = null; try { System.out.println("-------Download Start------"); URL Url; byte[] buf; int byteRead; int byteWritten = 0; Url = new URL(fileAddress); outStream = new BufferedOutputStream(new FileOutputStream(downloadDir + "\\" + localFileName)); uCon = Url.openConnection(); is = uCon.getInputStream(); buf = new byte[size]; while ((byteRead = is.read(buf)) != -1) { outStream.write(buf, 0, byteRead); byteWritten += byteRead; } System.out.println("Download Successfully."); System.out.println("File name : " + localFileName); System.out.println("of bytes : " + byteWritten); System.out.println("-------Download End--------"); } catch (Exception e) { e.printStackTrace(); } finally { try { is.close(); outStream.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * * @param fileAddress * @param downloadDir */ public static void fileUrlDownload(String fileAddress, String downloadDir) { int slashIndex = fileAddress.lastIndexOf("filename="); int periodIndex = fileAddress.lastIndexOf("."); // 파일 어드레스에서 마지막에 있는 파일이름을 취득 String fileName = fileAddress.substring(slashIndex + 9); if (periodIndex >= 1 && slashIndex >= 0 && slashIndex < fileAddress.length() - 1) { fileUrlReadAndDownload(fileAddress, fileName, downloadDir); } else { System.err.println("path or file name NG."); } } public static List getResult(ResultSet rs) throws Exception { ResultSetMetaData rsmd = rs.getMetaData(); //resultSet의 컬럼정보를 가져온다. int columnCount = rsmd.getColumnCount(); //컬럼 갯수 ArrayList hashMapList = new ArrayList(); HashMap columnMap= new HashMap(); //DB의 Field의 정보 모음 //column 정보 담기 for(int i = 1; i <= columnCount; i++) { String sTypeStr ="string"; columnMap.put(rsmd.getColumnName(i).toLowerCase(), sTypeStr); } //row 담기 while(rs.next()) { HashMap hashMap= new HashMap(); Iterator iter = columnMap.keySet().iterator(); while(iter.hasNext()) { String key = (String)iter.next(); hashMap.put(key.toLowerCase(), rs.getString((key))); } hashMapList.add(hashMap); } return hashMapList; } public static void main(String[] args) throws Exception { Connection cn = null; String s_id = "id"; /* 수정 : DB정보수정 */ String s_pwd = "pwd"; /* 수정 : DB정보수정 */ String jdbc_driver = "oracle.jdbc.driver.OracleDriver"; String jdbc_url = "jdbc:oracle:thin:@ip:port:sid"; /* 수정 : DB정보수정 */ try{ Class.forName(jdbc_driver); cn = DriverManager.getConnection(jdbc_url, s_id, s_pwd); } catch(Exception e) { System.out.println(e); } ResultSet rs = null; String sql; PreparedStatement pstmt = null; Statement st = null; List dataList = null; List<HashMap> resultList = new ArrayList(); sql = " select filename, realfilename from file_info_table "; /* 수정 : SQL 작성하기 */ pstmt = cn.prepareStatement(sql); rs = pstmt.executeQuery(); if(rs != null ){ dataList = getResult(rs); } String downDir = "C:/download"; /* 수정 : 다운로드받을 경로 작성 */ for(int i=0; i < dataList.size(); i++){ HashMap temp = (HashMap)dataList.get(i); String url = "http://filedownloadsite.com/filedown.do?filename="+temp.get("realfilename"); /* 수정 : 다운로드받을 url넣기 */ fileUrlReadAndDownload(url, String.valueOf(temp.get("filename")), downDir); } } } | cs |
'DEV > java' 카테고리의 다른 글
System property file encoding (0) | 2017.10.19 |
---|---|
gmail smtp mail발송 (0) | 2017.08.22 |
특수문자 변환 (0) | 2017.07.13 |
Comments, Trackbacks