2018.06.06 / 10:49
Java - URL °æ·ÎÀÇ ÆÄÀÏ ´Ù¿î·Îµå¹× ¼ºñ½º È°¿ë
ÀλçÀ̵åÀÚ¹Ù
Ãßõ ¼ö 203
# URL °æ·ÎÀÇ ÆÄÀÏÀ» ´Ù¿î·Îµå, ¼ºñ½º È°¿ë
> ¿ëµµ
- URL À§Ä¡ÀÇ ÆÄÀÏ(À¥ÆäÀÌÁö Æ÷ÇÔ)À» ƯÁ¤ À§Ä¡¿¡ ´Ù¿î·Îµå
(ÆÄÀÏÀ» ´Ù¿î·Îµå ÇÏ°í ¿øÇÏ´Â À§Ä¡¿¡ ¾÷·Îµå ÇÏ´Â ÀÛ¾÷ Ãà¼Ò)
> È°¿ë
- ´Ù¿î·ÎµåµÈ ÆÄÀÏÀ» Áï½Ã À¥(ºí·Î±×)À¸·Î ¼ºñ½º ÇÏ°íÀÚ ÇÒ¶§.
- DBMS ÀúÀå, À¥ ¶Ç´Â °³ÀÎ ºí·Î±×¿¡¼ ¿ÜºÎ °ø°³¹× ºñ°ø°³·Î È°¿ë
- µð·ºÅ丮¸íÀ» ÀÏÀÚº° º¯°æÇÏ¿© ÆÄÀÏ ±×·ì °ü¸®
> »ç¿ë ¶óÀ̺귯¸®
Apache Tika
FileUrlDownload.java
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 | import java.io.BufferedOutputStream; import java.io.File; 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.net.URLDecoder; import org.apache.tika.Tika; import bean.FileBean; /** * # URL»óÀÇ ÆÄÀÏÀ» ´Ù¿î·Îµå, DBMS¿¡ ÀúÀå */ public class FileUrlDownload { final static int bufferSize = 1024 ; /** * # URL °æ·ÎÀÇ ÆÄÀÏ ´Ù¿î·Îµå */ public static int fileUrlReadAndDownload(String fileUrl, String localFileName, String downloadDir) { OutputStream outStream = null ; URLConnection uCon = null ; InputStream is = null ; int byteWritten = 0 ; try { URL Url; byte [] buf; int byteRead; Url = new URL(fileUrl); outStream = new BufferedOutputStream( new FileOutputStream(downloadDir + File.separator + URLDecoder.decode( localFileName, "UTF-8" ))); uCon = Url.openConnection(); is = uCon.getInputStream(); buf = new byte [bufferSize]; 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(); } } return byteWritten; } /** * # ´Ù¿î·ÎµåµÈ ÆÄÀÏ Á¤º¸¸¦ Bean Ŭ·¡½º¿¡ ÀúÀå * - DBMS¿¡ ÀúÀå¹× Á¤º¸ È°¿ëÀ» À§ÇÔ. */ public static FileBean setUrlFileSave(String fileUrl, String downloadDir) { FileBean fileBean = new FileBean(); int fileSize = 0 ; int slashIndex = fileUrl.lastIndexOf( '/' ); int periodIndex = fileUrl.lastIndexOf( '.' ); // ÆÄÀÏ °æ·Î¿¡¼ ¸¶Áö¸· ÆÄÀϸíÀ» ÃßÃâ String fileName = fileUrl.substring(slashIndex + 1 ); String filePath = downloadDir+File.separator+fileName; if (periodIndex >= 1 && slashIndex >= 0 && slashIndex < fileUrl.length() - 1 ) { fileSize = fileUrlReadAndDownload(fileUrl, fileName, downloadDir); } fileBean.setFileName(fileName); fileBean.setFilePath(filePath); fileBean.setFileSize(fileSize); String mimeType = null ; Tika tika = new Tika(); // ÆÄÀÏÀÇ Mime-Type ÃßÃâ try { mimeType = tika.detect( new File(filePath)); } catch (IOException e) { e.printStackTrace(); } fileBean.setFileType(mimeType); // ÆÄÀÏ Mime-Type return fileBean; } /** * # main */ public static void main(String[] args) { // ÆÄÀÏ URL String fileUrl = "" ; // ´Ù¿î·Îµå µð·ºÅ丮 String downDir = "data" ; /* * ÆÄÀÏ ´Ù¿î·Îµå¹× ÆÄÀÏ Á¤º¸¸¦ ÀúÀåÇÏ¿© ¼ºñ½º È°¿ë */ setUrlFileSave(fileUrl, downDir); } } |
FileBean.java
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 | /** * # ÆÄÀÏ Á¤º¸¸¦ ´ãÀ» Bean Ŭ·¡½º */ public class FileBean { private int fileSeq; // ÆÄÀÏ ¹øÈ£ private String fileName; // ÆÄÀÏ ¸í private String fileMemo; // ÆÄÀÏ Á¤º¸ private String fileType; // ÆÄÀÏ Å¸ÀÔ private int fileSize; // ÆÄÀÏ »çÀÌÁî private String filePath; // ¾÷·Îµå µÈ ÆÄÀÏ °æ·Î private String filePathOrg; // ½ÇÁ¦ ÆÄÀÏ °æ·Î : URL Á¤º¸ private int isOpen; // °ø°³¿©ºÎ private String createDate; // »ý¼ºÀÏÀÚ private String createId; // »ý¼ºÀÚ ¾ÆÀ̵ð public String toString(){ StringBuffer sb = new StringBuffer(); sb.append( "### FileBean.toString() ###" ); sb.append( " fileSeq :" + this .fileSeq); sb.append( " fileName :" + this .fileName); sb.append( " fileMemo :" + this .fileMemo); sb.append( " fileType :" + this .fileType); sb.append( " fileSize :" + this .fileSize); sb.append( " filePath :" + this .filePath); sb.append( " filePathOrg :" + this .filePathOrg); sb.append( " isOpen :" + this .isOpen); sb.append( " createDate :" + this .createDate); sb.append( " createId :" + this .createId); return sb.toString(); } public String getFilePathOrg() { return filePathOrg; } public void setFilePathOrg(String filePathOrg) { this .filePathOrg = filePathOrg; } public String getCreateId() { return createId; } public void setCreateId(String createId) { this .createId = createId; } public int getFileSeq() { return fileSeq; } public void setFileSeq( int fileSeq) { this .fileSeq = fileSeq; } public String getFileName() { return fileName; } public void setFileName(String fileName) { this .fileName = fileName; } public String getFileMemo() { return fileMemo; } public void setFileMemo(String fileMemo) { this .fileMemo = fileMemo; } public String getFileType() { return fileType; } public void setFileType(String fileType) { this .fileType = fileType; } public int getFileSize() { return fileSize; } public void setFileSize( int fileSize) { this .fileSize = fileSize; } public String getFilePath() { return filePath; } public void setFilePath(String filePath) { this .filePath = filePath; } public int getIsOpen() { return isOpen; } public void setIsOpen( int isOpen) { this .isOpen = isOpen; } public String getCreateDate() { return createDate; } public void setCreateDate(String createDate) { this .createDate = createDate; } }
|