现在做的一个处理需要实现以下功能 查找功能
按下查找按钮,会把查找的结果生成一个csv文件,然后把这个文件压缩,存到oracle的blob的字段中。 csv-->zip--->bolb ,这个过程不能在本地生成文件,直接用把csv文件压缩zip文件,然后更新blob字段。 这个已经ok.下边的功能还不行,望大虾们指点一下。
从blob字段中取出上一步存入的zip文件,解压,生成csv文件,然后返回OutputStream形式。这个过程不允许在本地或服务器上生成文件。
难死小弟了,望大哥们指教!!
按下查找按钮,会把查找的结果生成一个csv文件,然后把这个文件压缩,存到oracle的blob的字段中。 csv-->zip--->bolb ,这个过程不能在本地生成文件,直接用把csv文件压缩zip文件,然后更新blob字段。 这个已经ok.下边的功能还不行,望大虾们指点一下。
从blob字段中取出上一步存入的zip文件,解压,生成csv文件,然后返回OutputStream形式。这个过程不允许在本地或服务器上生成文件。
难死小弟了,望大哥们指教!!
final int BUFFER = 2048;
final String strSplitFlg = "\\"; boolean isResultOk = true;
if (!OUUtil.hasData(strFileName) || (!OUUtil.hasData(odUpdateKey.getCompanyId())) ||
(!OUUtil.hasData(odUpdateKey.getOperator())) ||
((odUpdateKey.getOperationDate()== null) ||
(!OUUtil.hasData(odUpdateKey.getOperationFunction())))) {
logger.error("parameter is null.");
return false;
}
String strZipFileName = strFileName.substring(strFileName.lastIndexOf(strSplitFlg)+1);
byte[] outZipByte = null;
try {
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ZipOutputStream outZip = new ZipOutputStream(byteOut);
byte data[] = new byte[BUFFER];
BufferedInputStream origin = new BufferedInputStream(csvInput, BUFFER);
ZipEntry entry = new ZipEntry(strZipFileName);
outZip.putNextEntry(entry);
int count;
while ((count = origin.read(data, 0, BUFFER)) != -1) {
outZip.write(data, 0, count);
}
origin.close();
outZip.close();
outZipByte = byteOut.toByteArray();
}catch (IOException e) {
logger.error("IOException :"+ e.getMessage());
return false;
}
try {
String strSerchSql = "SELECT FILE_CONTENT FROM OPERATION_RECORD " +
"WHERE COMPANY_ID = ? " +
"AND OPERATOR = ? " +
"AND OPERATION_DATE = ? " +
"AND OPERATION_FUNCTION = ? " +
"FOR UPDATE NOWAIT";
PreparedStatement searchPstmt = con.prepareStatement(strSerchSql);
int i = 1;
searchPstmt.setString(i++, odUpdateKey.getCompanyId());
searchPstmt.setString(i++, odUpdateKey.getOperator());
searchPstmt.setString(i++, "2008/09/01");
searchPstmt.setString(i++, odUpdateKey.getOperationFunction());
ResultSet rs =searchPstmt.executeQuery();
if (rs.next()) {
oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob("File_Content");
OutputStream outStream = blob.getBinaryOutputStream();
outStream.write(outZipByte, 0, outZipByte.length);
outStream.flush();
outStream.close();
} else {
logger.error("NO Data.");
isResultOk = false;
}
con.commit();
rs.close();
} catch (SQLException e) {
logger.error("SQLException :"+ e.getMessage());
return false; } catch (IOException ioe) {
logger.error("IOException :"+ ioe.getMessage());
return false;
}
return isResultOk;
}
OutputStream output = null;
InputStream is = null;
//从数据库结果集中获取blob字段内容
is = queryRs.getBlob("file_blob").getBinaryStream();byte[] b = new byte[1024];
int i = 0;
while ((i = is.read(b)) > 0) {
output.write(b, 0, i);
}return output;//返回输出流即可
下面是我做的,虽然能实现,但是需要在本地生成临时文件。我想不要生成临时文件,直接把解压后的文件的输出流返回!
public OutputStream unZipAndDownload(ODOperationRecord odSerchKey, Connection con) throws Exception{
//zip
String strPath = "c:\\out\\"+OUUtil.getTime()+".zip"; String StrOutPutFilePath = "c:\\test\\";
final int BUFFER = 2048;
OutputStream reulstStream = null; if ((!OUUtil.hasData(odSerchKey.getCompanyId())) ||
(!OUUtil.hasData(odSerchKey.getOperator())) ||
((odSerchKey.getOperationDate() == null) ||
(!OUUtil.hasData(odSerchKey.getOperationFunction())))) {
return reulstStream;
} try {
con.setAutoCommit(false);
Statement st = con.createStatement();
String serchSql = "SELECT FILE_CONTENT FROM OPERATION_RECORD WHERE ";
serchSql = serchSql + "COMPANY_ID = '" +odSerchKey.getCompanyId()+"' AND " ;
serchSql = serchSql + "OPERATOR = '" +odSerchKey.getOperator() +"' AND ";
serchSql = serchSql + "OPERATION_DATE = to_date('"+ odSerchKey.getOperationDate()+"', 'yyyy-mm-dd hh24:mi:ss') AND ";
serchSql = serchSql + "OPERATION_FUNCTION = '" +odSerchKey.getOperationFunction()+"'" ;
if( logger.isDebugEnabled() ) {
logger.debug("SQL=" + serchSql);
}
ResultSet rs = st.executeQuery(serchSql);
if (rs.next()) {
java.sql.Blob blob = rs.getBlob(1);
InputStream ins = blob.getBinaryStream();
OutputStream fout = new FileOutputStream(strPath);
byte[] b = new byte[1024];
int len = 0;
while ( (len = ins.read(b)) != -1) {
fout.write(b, 0, len);
}
fout.close();
ins.close();
} else {
con.commit();
rs.close();
st.close();
con.close();
return null;
}
rs.close();
st.close();
} catch (SQLException e) {
return null; } catch (IOException ioe) {
return null;
}
try {
ZipFile zipFile = new ZipFile(strPath);
Enumeration emu = zipFile.entries();
while(emu.hasMoreElements()) { ZipEntry entry = (ZipEntry)emu.nextElement();
BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));
StrOutPutFilePath = StrOutPutFilePath+ entry.getName();
File file = new File(StrOutPutFilePath);
File parent = file.getParentFile();
if(parent != null && (!parent.exists())) {
parent.mkdirs();
}
OutputStream fos = new FileOutputStream(file);
OutputStream bos = new BufferedOutputStream(fos,BUFFER);
int count;
byte data[] = new byte[BUFFER];
while ((count = bis.read(data, 0, BUFFER)) != -1) {
bos.write(data, 0, count);
}
reulstStream = fos;
bos.flush();
bos.close();
bis.close();
}
zipFile.close();
removeFile(StrOutPutFilePath);
removeFile(strPath);
} catch (IOException e) {
e.printStackTrace();
return null;
} return reulstStream;
}
OutputStream output = null;
InputStream is = null;
//从数据库结果集中获取blob字段内容
is = queryRs.getBlob("file_blob").getBinaryStream();
//创建zip输入流
ZipInputStream zis=new ZipInputStream(is);
//获取zip输入流中第一个文件流
zis.getNextEntry();byte[] b = new byte[1024];
int i = 0;
while ((i = zis.read(b)) > 0) {
output.write(b, 0, i);
}return output;//返回输出流即可
这些都可以获得OutputStream的,用这些OutputStream替换你临时文件就可以了
ok!ok!ok!ok!ok!大哥,留个联系方式,兄弟有空请你吃饭!!!
http://bbs.warlord.cn/images/smilies/yc01.gif真的假的?偶最喜欢吃了!娃哈哈!
QQ:19666125