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
번호 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|
11 | 일본어 전각 반각 변환 예제 소스 .첫번째 | 황제낙엽 | 2007.01.10 | 3070 |
10 | 환경파일 로드 클래스 구성 (XML파싱) | 황제낙엽 | 2007.01.05 | 62 |
» | HttpURLConnection을 사용하여 웹 페이지 액세스하기 (두번째) | 황제낙엽 | 2007.01.05 | 50 |
8 | 자바의 I/O 예제 [1] | 황제낙엽 | 2006.12.28 | 50 |
7 | HttpURLConnection 클래스를 이용한 Search | 황제낙엽 | 2006.12.28 | 260 |
6 | HttpURLConnection을 사용하여 웹 페이지 액세스하기 (첫번째) | 황제낙엽 | 2006.12.28 | 57 |
5 | Servlet으로 구현한 파일 다운로드(download) 페이지 | 황제낙엽 | 2006.12.27 | 112 |
4 | JSP로 구현한 파일 다운로드(download) 페이지 | 황제낙엽 | 2006.12.27 | 77 |
3 | 파일 Download로직 구현시에 참고할만한 내용 | 황제낙엽 | 2006.12.27 | 95 |
2 | 파일을 web 이외의 경로에 올린다음 보내는 코드 예제 (JDK1.3) | 황제낙엽 | 2006.12.27 | 24 |
1 | NestedRuntimeException | 황제낙엽 | 2006.09.22 | 988 |