当前位置: 首页 > 后端技术 > Java

原始jdbc连接数据读取字符串生成zip文件,读入字节数组

时间:2023-04-01 13:20:45 Java

代码如下:本次数据库为sqlservetry{Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");连接connection=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DataBaseName=cs","sa","Zmy123456.");Stringsql="selectATFile,ATFileNamefromAT_AttachmentFilewhereATGuid='A1:05c5bf44-2323-48ec-8f23-0f6623ee7fcd'";PreparedStatementps=connection.prepareStatement(sql);结果集rs=ps.executeQuery();结果集元数据rsmd=rs.getMetaData();MapmapMetaData=newHashMap<>();压缩文件zipFile=null;流signStream=null;流zipStream=null;while(rs.next()){字符串columnLabel=rsmd.getColumnLabel(1);BlobcolumnValue=rs.getBlob(columnLabel);输入Streamins=columnValue.getBinaryStream();文件file=newFile("D:\\","cs.zip");org.apache.commons.io.FileUtils.copyInputStreamToFile(ins,文件);zipFile=newZipFile(file,Charset.defaultCharset());signStream=zipFile.stream();zipStream=zipFile.stream();//断言PredicatesignTxt=ze->ze.getName().contains("sign,txt");PredicatezipTxt=ze->ze.getName().endsWith(".zip");//,过滤OptionalsignInfo=(Optional)signStream.filter(signTxt).findFirst();可选zipInfo=(可选)zipStream.filter(zipTxt).findFirst();ins.close();zipFile.close();//获取文件名StringfileName=rsmd.getColumnLabel(2);对象文件名=rs.getObject(文件名);StringzipDir="D:\\";//解压zip文件Stringname=ZipUtils.unzip(file.getPath(),zipDir);文件.删除();//读取Returnbyte[]字节到字节数组bytes=FileUtil.readFile2Bytes(newFile(zipDir+File.separator+name),true);System.out.println(bytes);}//将字符串写入zip如下//Filefile=newFile("D:\\","cs.zip");//java.util.zip.ZipOutputStreamzipOutputStream=newZipOutputStream(newFileOutputStream(file)));//mapMetaData.forEach((k,v)->{//try{//zipOutputStream.putNextEntry(newZipEntry("15.pdf"));//IoUtil.write(zipOutputStream,false,(byte[])v);//zipOutputStream.flush();//zipOutputStream.closeEntry();//}catch(IOExceptione){//e.printStackTrace();//}//});}catch(Exceptione){e.printStackTrace();}解压文件方法unzip//解压zip文件publicstaticStringunzip(StringfilePath,StringzipDir){Stringname=null;BufferedOutputStreamdes=null;BufferedInputStream为=null;压缩文件zipFile=null;尝试{ZipEntry条目;zipFile=newZipFile(文件路径);枚举dir=zipFile.entries();while(dir.hasMoreElements()){entry=(ZipEntry)dir.nextElement();if(entry!=null){name=entry.getName();}}枚举e=zipFile.entries();while(e.hasMoreElements()){entry=(ZipEntry)e.nextElement();如果(entry.isDirectory()){继续;}else{是=新的BufferedInputStream(zipFile.getInputStream(条目));整数计数;byte[]bytes=newbyte[2048];FileOutputStreamfos=newFileOutputStream(zipDir+entry.getName());des=newBufferedOutputStream(fos,2048);while((count=is.read(bytes,0,2048))!=-1){des.write(bytes,0,count);}}}返回名称;}catch(IOExceptione){e.printStackTrace();}最后{尝试{zipFile.close();des.flush();des.close();is.close();}catch(IOExceptione){e.printStackTrace();}}返回名称;};读成字节数组publicstaticbyte[]readFile2Bytes(Filefile,booleanisDelete)throwsIOException{InputStreaminputStream=newFileInputStream(file);ByteArrayOutputStreamoutputStream=newByteArrayOutputStream();尝试{intlen=0;字节[]b=新字节[10240];while((len=inputStream.read(b))!=-1){outputStream.write(b,0,len);}byte[]byteArray=outputStream.toByteArray();返回字节数组;}最后{IOUtils.close(inputStream);IOUtils.close(outputStream);如果(isDelete){file.delete();}}}