URLからコンテンツ取得および文字コード変換
戻る
::::::::::::::
ContentRetriever.java
::::::::::::::
import java.io.*;
import java.net.*;
/**
$Id: content-retriever.html,v 1.1 2009/06/22 16:12:07 kishi Exp kishi $
*/
public class ContentRetriever {
private String urlString;
private String outputFileName;
public ContentRetriever( String urlString, String outputFileName ) {
this.urlString = urlString;
this.outputFileName = outputFileName;
}
public void retrieve() {
try {
// URL の作成
URL url = new URL( urlString );
// コネクトして、HttpURLConnection の作成
HttpURLConnection connection = ( HttpURLConnection ) url.openConnection();
connection.setRequestMethod( "GET" );
System.out.println( "\n\n===============================================================================" );
System.out.println( "コンテントタイプは " + connection.getContentType() + " です。" );
System.out.println( "レスポンスコードは " + connection.getResponseCode() + " です。" );
System.out.println( "===============================================================================" );
// 出力ストリーム
BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream( outputFileName ) );
// 入力ストリーム
BufferedInputStream bis = new BufferedInputStream( connection.getInputStream() );
int size = bis.available();
byte[] buf = new byte[ size ];
int len = 0;
while ( ( len = bis.read( buf ) ) > 0 ) {
System.out.println( "len=" + len );
bos.write( buf, 0, len );
}
bis.close();
bos.close();
} catch ( Exception e ) {
e.printStackTrace();
}
}
static public void main( String[] args ) {
if ( args.length != 2 ) {
System.err.println( "Usage: java Test1 [URL] [outputFileName]" );
System.exit( 1 );
}
String targetUrl = args[ 0 ];
String outputFileName = args[ 1 ];
ContentRetriever retriever = new ContentRetriever( targetUrl, outputFileName );
retriever.retrieve();
}
}
::::::::::::::
GenericDecoder.java
::::::::::::::
import java.io.*;
import java.nio.*;
import java.nio.charset.*;
/**
$Id: content-retriever.html,v 1.1 2009/06/22 16:12:07 kishi Exp kishi $
*/
public class GenericDecoder {
private String filename;
private String sourceEncoding;
public GenericDecoder( String filename, String sourceEncoding ) {
this.filename = filename;
this.sourceEncoding = sourceEncoding;
}
public void decode() {
try {
//==========================================================================
// 入力ストリーム側の文字エンコーディングを事前にしておく必要がある
// なんとかならんか・・・
//==========================================================================
Charset charset = Charset.forName( sourceEncoding );
BufferedInputStream bis = new BufferedInputStream( new FileInputStream( filename ) );
int size = bis.available();
byte[] buf = new byte[ size ];
int len = 0;
while ( ( len = bis.read( buf ) ) > 0 ) {
byte[] before = new byte[ len ];
for ( int i = 0;i < len;i++ ) {
before[ i ] = buf[ i ];
}
CharBuffer result = charset.decode( ByteBuffer.wrap( before ) );
// 標準出力に出力
System.out.print( result.toString() );
}
bis.close();
} catch ( Exception e ) {
e.printStackTrace();
}
}
static public void main( String[] args ) {
if ( args.length != 2 ) {
System.out.println( "Usage: java GenericDecoder [inputFileName] [sourceEncoding]" );
System.exit( 1 );
}
GenericDecoder decoder = new GenericDecoder( args[ 0 ], args[ 1 ] );
decoder.decode();
}
}
戻る