• atwiki
  • まとめ @ wiki
  • データベースの内容を圧縮ダウンロードする-ZipEntry/ZipOutputStreamクラス-

まとめ @ wiki

データベースの内容を圧縮ダウンロードする-ZipEntry/ZipOutputStreamクラス-

最終更新:

shells

- view
管理者のみ編集可

データベースの内容を圧縮ダウンロードする-ZipEntry/ZipOutputStreamクラス- 【HP


目次


ソース

zip.jsp

<%@ page contentType="application/octet-stream; charset=Shift_JIS" import="java.io.*,java.sql.*,java.util.zip.*" %>
 
<%
	response.setHeader( "Content-Disposition", "attachment; filename=sample.zip" );
	Class.forName( "oracle.jdbc.driver.OracleDriver" );
	Connection db = DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:STS", "hr", "oracle" );
	db.setReadOnly( true );
	Statement objSql = db.createStatement();
	ResultSet rs = objSql.executeQuery( "select employee_id, first_name, last_name, salary, hire_date from employees" );
	ByteArrayOutputStream objBos = new ByteArrayOutputStream();
	OutputStreamWriter objOsw = new OutputStreamWriter( objBos, "Shift_JIS" );
	objOsw.write( "employee_id\tfirst_name\tlast_name\tsalary\thire_date" );
	objOsw.write( System.getProperty( "line.separator" ) );
 
	while( rs.next() ){
		objOsw.write( rs.getString( "employee_id" ) + "\t" );
		objOsw.write( rs.getString( "first_name" ) + "\t" );
		objOsw.write( rs.getString( "last_name" ) + "\t" );
		objOsw.write( rs.getString( "salary" ) + "\t" );
		objOsw.write( rs.getString( "hire_date" ) + "\t" );
		objOsw.write( System.getProperty( "line.separator" ) );
	}
	objOsw.close();
 
	ZipOutputStream objZos = new ZipOutputStream( response.getOutputStream() );
	ZipEntry objZe = new ZipEntry( "database.txt" );
	objZe.setMethod( ZipOutputStream.DEFLATED );
	objZos.putNextEntry( objZe );
	byte[] aryByt = objBos.toByteArray();
	objZos.write( aryByt, 0, aryByt.length );
	objZos.closeEntry();
	objZos.close();
	objSql.close();
	db.close();
%>

実行結果

sample.zipの保存画面となる
解凍すると中にはdatabase.txtが入っている

記事メニュー
ウィキ募集バナー