maven 을 사용하는 경우 pom.xml 파일에 다음의 의존성을 추가한다.
1 2 3 4 5 6 7 8 9 10 | < dependency > < groupid >commons-io</ groupid > < artifactid >commons-io</ artifactid > < version >2.5</ version > </ dependency > < dependency > < groupid >org.apache.tika</ groupid > < artifactid >tika-core</ artifactid > < version >1.22</ version > </ dependency > |
이미지 파일은 임시파일을 생성하고 URL 연결을 통하여 읽어 드린 스트림을 저장하는 방식으로 구현하였다.
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 | /** 예제 **/ package tests; import java.io.File; import java.io.IOException; import java.net.URL; import java.util.UUID; import org.apache.commons.io.FileUtils; import org.apache.tika.Tika; public class DownloadImageFromUrlTest { public static void main(String args[]){ try { // 다운로드를 위한 이미지 URL 정보 File image = readFileFromUrl( url ); String contentType = getContentType(image); } catch (Exception e) { e.printStackTrace(); } } /** * get content type from file. * @param image * @return */ public static String getContentType(File image) { String contentType = null ; Tika tika = new Tika(); try { contentType = tika.detect(image); } catch (IOException e) { contentType = null ; } return contentType; } /** * read image file from url. * @param imageUrl * @return * @throws IOException */ public static File readFileFromUrl(URL imageUrl) throws IOException { File temp = File.createTempFile(UUID.randomUUID().toString(), ".tmp" ); temp.deleteOnExit(); FileUtils.copyURLToFile(imageUrl, temp); return temp; } } |
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 | /** 예제 **/ package tests; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import java.util.UUID; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.tika.Tika; public class DownloadImageFromUrlTest { public static void main(String args[]){ try { // 다운로드를 위한 이미지 URL 정보 File image = readFileFromUrl( url ); String contentType = getContentType(image); } catch (Exception e) { e.printStackTrace(); } } /** * get content type from file. * @param image * @return */ public static String getContentType(File image) { String contentType = null ; Tika tika = new Tika(); try { contentType = tika.detect(image); } catch (IOException e) { contentType = null ; } return contentType; } /** * read image file from url. * @param imageUrl * @return * @throws IOException */ public static File readFileFromUrl(URL url) throws Exception { InputStream inputStream = null ; try { String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36" ; URLConnection con = url.openConnection(); con.setRequestProperty( "User-Agent" , USER_AGENT); inputStream = con.getInputStream(); File temp = File.createTempFile(UUID.randomUUID().toString(), ".tmp" ); FileUtils.copyToFile( inputStream, temp ); return temp; }finally { IOUtils.closeQuietly(inputStream); } } } |
댓글 없음:
댓글 쓰기