📄 storage.java
字号:
package org.ehotsoft.yekki.sql;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.SQLException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.ehotsoft.yekki.upload.Upload;
import org.ehotsoft.yekki.upload.File;
import org.ehotsoft.yekki.upload.UploadException;
import org.ehotsoft.yekki.sql.ConnectionManager;
import oracle.jdbc.OracleResultSet;
import oracle.sql.BLOB;
public class Storage {
//刚插入的数据如何得到其ID号?
public static int upload( Upload upload, int moduleId )
throws UploadException {
SQLFacade facade = new SQLFacade();
Connection cnn = null;
Statement stmt = null;
ResultSet rst = null;
OracleResultSet oracleRst = null;
File file = upload.getFiles().getFile( 0 );
int id = -1;
try {
cnn = facade.getConnection();
cnn.setAutoCommit( false );
stmt = cnn.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE );
StringBuffer sql = new StringBuffer();
sql.append( "select seq_storage.nextval from dual" );
rst = facade.executeQuery( sql.toString() );
rst.next();
id = rst.getInt( 1 );
sql = new StringBuffer();
sql.append( "insert into tbl_storage_file( id, file_type, file_name, file_ext, file_size, module_id, content ) values(" )
.append( id )
.append( ",'" )
.append( file.getContentType() )
.append( "','" )
.append( file.getFileName() )
.append( "','" )
.append( file.getFileExt() )
.append( "'," )
.append( file.getSize() )
.append( "," )
.append( moduleId )
.append( ", EMPTY_BLOB() )" );
stmt.executeUpdate( sql.toString() );
sql = new StringBuffer( "select content from tbl_storage_file where id=" ).append( id ).append( " for update" );
oracleRst = ( OracleResultSet )stmt.executeQuery( sql.toString() );
oracleRst.next();
BLOB blob = ( BLOB )oracleRst.getBlob( 1 );
OutputStream out = blob.getBinaryOutputStream();
out.write( file.getBinaryData(), file.getStartData(), file.getSize() );
out.flush();
out.close();
cnn.commit();
cnn.setAutoCommit( true );
}
catch ( Exception e ) {
try {
cnn.rollback();
}
catch ( SQLException se ) {
se.printStackTrace();
}
e.printStackTrace();
throw new UploadException( e.getMessage() );
}
finally {
try {
cnn.setAutoCommit( true );
}
catch ( SQLException e ) {
e.printStackTrace();
}
}
return id;
}
public static int dropById( int id )
throws SQLException {
String sql = "delete from tbl_storage_file where id=" + id;
SQLFacade facade = new SQLFacade();
int retCount = facade.executeUpdate( sql );
facade.close();
return retCount;
}
public static void dump( HttpServletResponse response, int id )
throws SQLException, IOException {
SQLFacade facade = new SQLFacade();
String sql = "select * from tbl_storage_file where id=" + id;
ResultSet rst = facade.executeQuery( sql );
if ( rst.next() ) {
String fileType = rst.getString( "file_type" );
if ( fileType.equals( "document" ) ) {
fileType = " text/html";
}
response.setContentType( fileType + "; charset=GB2312" );
if ( !( fileType.equals( " text/html" ) || fileType.equals( " image/gif" ) || fileType.equals( " image/pjpeg" ) || fileType.equals( " text/plain" ) ) ) {
response.setHeader( "Content-disposition", "attachment; filename=" + rst.getString( "file_name" ) );
}
ServletOutputStream out = response.getOutputStream();
int len = 0;
InputStream in = rst.getBinaryStream( "content" );
byte[] bytes = new byte[ 4096 * 10 ];
while ( ( len = in.read( bytes ) ) != -1 ) {
out.write( bytes, 0, len );
out.flush();
}
in.close();
out.close();
}
facade.close();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -