| sitelink1 | |
|---|---|
| sitelink2 | |
| sitelink3 | |
| sitelink4 | |
| sitelink5 | |
| sitelink6 | 
HTTPURLCONNECTION를 사용하여 웹 페이지 액세스하기 (첫번째)의 예제에서는 웹서버에서 파일을 다운로드하여 char형태로 데이터를 콘솔에 출력하고 있다.
만일 다운로드하는 파일이 바이너리 파일인 경우에는 byte형태가 아닌 char형태로 처리할 경우 데이터가 훼손되므로 유의하자.
다음의 예제는 다운로드한 데이터를 char형태가 아닌 byte형태로 처리하여 파일에 저장하고 있다.
import java.io.File;   
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class RequestTest {
private static URLConnection connection;
private static String saveFile = "D:Info.xls";
private static String url = "http://localhost:8080/FileDownloadTest/DownloadTest";
private static String uid = "down";
private static String pass = "test";
private static String compId = "103111";
private static void connect(String urlString) {
try {
URL url = new URL("urlString);
connection = url.openConnection();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void readContents() throws IOException {
FileOutputStream fileoutput = new FileOutputStream(new File(saveFile));
try {
InputStream input = connection.getInputStream();
byte[] buf = new byte[1024];
int length = 0;
while ((length = input.read(buf)) > 0) {
fileoutput.write(buf);
}
fileoutput.close();
input.close();
} catch (MalformedURLException mue) {
mue.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
System.out.println("Program END!!");
}
public static void main(String[] args) throws IOException {
connect(url + "?id=" + uid + "&pass=" + pass + "&compId=" + compId);
readContents();
}
}
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class RequestTest {
private static URLConnection connection;
private static String saveFile = "D:Info.xls";
private static String url = "http://localhost:8080/FileDownloadTest/DownloadTest";
private static String uid = "down";
private static String pass = "test";
private static String compId = "103111";
private static void connect(String urlString) {
try {
URL url = new URL("urlString);
connection = url.openConnection();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void readContents() throws IOException {
FileOutputStream fileoutput = new FileOutputStream(new File(saveFile));
try {
InputStream input = connection.getInputStream();
byte[] buf = new byte[1024];
int length = 0;
while ((length = input.read(buf)) > 0) {
fileoutput.write(buf);
}
fileoutput.close();
input.close();
} catch (MalformedURLException mue) {
mue.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
System.out.println("Program END!!");
}
public static void main(String[] args) throws IOException {
connect(url + "?id=" + uid + "&pass=" + pass + "&compId=" + compId);
readContents();
}
}
댓글 0
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 수 | 
|---|---|---|---|---|
| 26 | 셋째날 - 패키지, 상속, 접근제어 | 황제낙엽 | 2003.04.14 | 728 | 
| 25 | 둘째날 - 클래스, 메서드, 연산자 | 황제낙엽 | 2003.04.14 | 651 | 
| 24 | 첫째날 - 자바의 특징과 DATA TYPE | 황제낙엽 | 2003.04.14 | 667 | 
| 23 | Polymorphism과 Method Overriding에 대한 이야기 | 황제낙엽 | 2003.05.15 | 502 | 
| 22 | 자바의 특징 | 황제낙엽 | 2003.05.06 | 640 | 
| 21 | JVM (Java Virtual Machine)에 대한 몇가지 설명 | 황제낙엽 | 2003.05.06 | 453 | 
| 20 | 자바설화 | 황제낙엽 | 2003.05.03 | 607 | 
| 19 | XML기반 정보 보호 기술의 대두 | 황제낙엽 | 2003.04.22 | 512 | 
| 18 | 인터넷을 통한 자바 기술의 변화 | 황제낙엽 | 2003.04.07 | 696 | 
| 17 | JAVA관련 용어와 기술 | 황제낙엽 | 2003.04.05 | 661 | 
| 16 | UTF-8을 위한 문자열 인코딩처리 관련 | 황제낙엽 | 2006.10.06 | 1081 | 
| 15 | Code Conventions for JavaTM Programing Language   | 황제낙엽 | 2006.10.23 | 702 | 
| 14 | 자바에서 UTF-8 개발에 관한 정리 (2) | 황제낙엽 | 2006.10.06 | 798 | 
| 13 | 자바에서 UTF-8 개발에 관한 정리 (1) | 황제낙엽 | 2006.10.06 | 745 | 
| 12 | 일본어 전각 반각 변환 예제 소스 .두번째
					[1]   | 황제낙엽 | 2007.01.11 | 780 | 
| 11 | 일본어 전각 반각 변환 예제 소스 .첫번째   | 황제낙엽 | 2007.01.10 | 3685 | 
| 10 | 환경파일 로드 클래스 구성 (XML파싱) | 황제낙엽 | 2007.01.05 | 781 | 
| » | HttpURLConnection을 사용하여 웹 페이지 액세스하기 (두번째) | 황제낙엽 | 2007.01.05 | 766 | 
| 8 | 자바의 I/O 예제 [1]   | 황제낙엽 | 2006.12.28 | 547 | 
| 7 | HttpURLConnection 클래스를 이용한 Search | 황제낙엽 | 2006.12.28 | 856 | 
 
							