gpt4 book ai didi

java - 使用 Java Servlet 合并 Oracle 中的多个 BLOB 以下载到 .docx 文件中

转载 作者:太空宇宙 更新时间:2023-11-04 12:28:20 25 4
gpt4 key购买 nike

我正在使用 Apache POI XWPF 读取和写入以 BLOB 数据类型存储在 Oracle 数据库中的 .docx 文件。

Apache POI 的大部分文档是:

1.- 对于 Excel 文件。2.- 对于本地文件。

这是我的试用版本,它工作得很好,我需要它,但是你有什么想法读取查询的多个 BLOB 并将其捕获到他自己的变量中吗?我正在考虑使用 Array 或 ArrayList,但我不知道到底如何做到这一点。

我采纳了这篇文章的想法of how merge Documents using Apache POI

这是我的代码:

import geq.gdig.dao.ConexionBD;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.naming.Context;
import javax.naming.InitialContext;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.xmlbeans.XmlOptions;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBody;

/**
*
* @author euriber
*/
public class PruebaBLOB extends HttpServlet {

/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
//PrintWriter out = response.getWriter();
System.out.println("Hola");
String lNombreArchivo = null;
String query1 = "select d.id_documento,(select bl.content from documentos_blob bl where bl.id_documento=69)content69," +
"(select bl.content from documentos_blob bl where bl.id_documento=63)content63," +
"(select bl.content from documentos_blob bl where bl.id_documento=53)content53," +
"(select bl.content from documentos_blob bl where bl.id_documento=68)content68," +
"(select bl.content from documentos_blob bl where bl.id_documento=54)content54," +
"(select bl.content from documentos_blob bl where bl.id_documento=55)content55," +
"(select bl.content from documentos_blob bl where bl.id_documento=74)content74," +
"(select bl.content from documentos_blob bl where bl.id_documento=51)content51," +
"(select bl.content from documentos_blob bl where bl.id_documento=75)content75," +
"(select bl.content from documentos_blob bl where bl.id_documento=60)content60," +
"(select bl.content from documentos_blob bl where bl.id_documento=56)content56," +
"(select bl.content from documentos_blob bl where bl.id_documento=61)content61," +
"(select bl.content from documentos_blob bl where bl.id_documento=59)content59," +
"(select bl.content from documentos_blob bl where bl.id_documento=57)content57," +
"(select bl.content from documentos_blob bl where bl.id_documento=62)content62," +
"(select bl.content from documentos_blob bl where bl.id_documento=52)content52," +
"(select bl.content from documentos_blob bl where bl.id_documento=73)content73," +
"(select bl.content from documentos_blob bl where bl.id_documento=58)content58," +
"(select bl.content from documentos_blob bl where bl.id_documento=65)content65," +
"(select bl.content from documentos_blob bl where bl.id_documento=64)content64," +
"(select bl.content from documentos_blob bl where bl.id_documento=66)content66," +
"(select bl.content from documentos_blob bl where bl.id_documento=67)content67," +
"(select bl.content from documentos_blob bl where bl.id_documento=70)content70," +
"(select bl.content from documentos_blob bl where bl.id_documento=71)content71," +
"(select bl.content from documentos_blob bl where bl.id_documento=72)content72" +
" from documentos d where d.id_documento = 1";

String query2 = "select d.* from documentos d where d.id_documento= 2";
Context ic = null;
DataSource dataSource = null;
Connection conn = null;
Statement st = null;
Statement st2 = null;
ResultSet rs = null;
ResultSet rs2 = null;
try {
System.out.print("Dentro del try");
ic = new InitialContext();
dataSource = (DataSource) ic.lookup(ConexionBD.DATA_SOURCE_GEQGDIG);
conn = dataSource.getConnection();
st = conn.createStatement();
st2 = conn.createStatement();
System.out.print("Antes de execute...");
rs = st.executeQuery(query1);
System.out.print(query1);
System.out.print("Después de execute...");

while (rs.next()) {
//System.out.print("Dentro del while...");
lNombreArchivo = "PED_SPF_1ER_INFORME.docx";
System.out.println("lNombreArchivo-->" + lNombreArchivo);
Blob blob69 = rs.getBlob("CONTENT69");
Blob blob63 = rs.getBlob("CONTENT63");
Blob blob53 = rs.getBlob("CONTENT53");
Blob blob68 = rs.getBlob("CONTENT68");
Blob blob54 = rs.getBlob("CONTENT54");
Blob blob55 = rs.getBlob("CONTENT55");
Blob blob74 = rs.getBlob("CONTENT74");
Blob blob51 = rs.getBlob("CONTENT51");
Blob blob75 = rs.getBlob("CONTENT75");
Blob blob60 = rs.getBlob("CONTENT60");
Blob blob56 = rs.getBlob("CONTENT56");
Blob blob61 = rs.getBlob("CONTENT61");
Blob blob59 = rs.getBlob("CONTENT59");
Blob blob57 = rs.getBlob("CONTENT57");
Blob blob62 = rs.getBlob("CONTENT62");
Blob blob52 = rs.getBlob("CONTENT52");
Blob blob73 = rs.getBlob("CONTENT73");
Blob blob58 = rs.getBlob("CONTENT58");
Blob blob65 = rs.getBlob("CONTENT65");
Blob blob64 = rs.getBlob("CONTENT64");
Blob blob66 = rs.getBlob("CONTENT66");
Blob blob67 = rs.getBlob("CONTENT67");
Blob blob70 = rs.getBlob("CONTENT70");
Blob blob71 = rs.getBlob("CONTENT71");
Blob blob72 = rs.getBlob("CONTENT72");


InputStream src69 = blob69.getBinaryStream();
InputStream src63 = blob63.getBinaryStream();
InputStream src53 = blob53.getBinaryStream();
InputStream src68 = blob68.getBinaryStream();
InputStream src54 = blob54.getBinaryStream();
InputStream src55 = blob55.getBinaryStream();
InputStream src74 = blob74.getBinaryStream();
InputStream src51 = blob51.getBinaryStream();
InputStream src75 = blob75.getBinaryStream();
InputStream src60 = blob60.getBinaryStream();
InputStream src56 = blob56.getBinaryStream();
InputStream src61 = blob61.getBinaryStream();
InputStream src59 = blob59.getBinaryStream();
InputStream src57 = blob57.getBinaryStream();
InputStream src62 = blob62.getBinaryStream();
InputStream src52 = blob52.getBinaryStream();
InputStream src73 = blob73.getBinaryStream();
InputStream src58 = blob58.getBinaryStream();
InputStream src65 = blob65.getBinaryStream();
InputStream src64 = blob64.getBinaryStream();
InputStream src66 = blob66.getBinaryStream();
InputStream src67 = blob67.getBinaryStream();
InputStream src70 = blob70.getBinaryStream();
InputStream src71 = blob71.getBinaryStream();
InputStream src72 = blob72.getBinaryStream();
response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document" + "; name=\"" + lNombreArchivo + "\"");
response.setHeader("Content-disposition",
"attachment; filename=\"" + lNombreArchivo + "\"");
OutputStream os = response.getOutputStream();
//byte[] bytes = new byte[1024];
//int n = 64;

merge(src69, src63, src53, src68, src54, src55, src74, src51, src75, src60, src56, src61, src59,
src57, src62, src52, src73, src58, src65, src64, src66, src67, src70, src71, src72, os);
os.close();

}
} catch (Exception e) {
System.out.println("error-->" + e.getMessage());
} finally {
try {
if (conn != null) {
conn.close();
}
if (rs != null) {
rs.close();
}
if (st != null) {
st.close();
}
if (rs2 != null) {
rs2.close();
}
if (st2 != null) {
st2.close();
}
} catch (Exception e) {
}
}
}

public static void merge(InputStream src1, InputStream src2, InputStream src3,
InputStream src4, InputStream src5, InputStream src6, InputStream src7, InputStream src8, InputStream src9, InputStream src10, InputStream src11, InputStream src12, InputStream src13, InputStream src14, InputStream src15, InputStream src16, InputStream src17, InputStream src18, InputStream src19, InputStream src20, InputStream src21, InputStream src22, InputStream src23, InputStream src24, InputStream src25, OutputStream dest) {
try {
OPCPackage src1Package = OPCPackage.open(src1);
OPCPackage src2Package = OPCPackage.open(src2);
OPCPackage src3Package = OPCPackage.open(src3);
OPCPackage src4Package = OPCPackage.open(src4);
OPCPackage src5Package = OPCPackage.open(src5);
OPCPackage src6Package = OPCPackage.open(src6);
OPCPackage src7Package = OPCPackage.open(src7);
OPCPackage src8Package = OPCPackage.open(src8);
OPCPackage src9Package = OPCPackage.open(src9);
OPCPackage src10Package = OPCPackage.open(src10);
OPCPackage src11Package = OPCPackage.open(src11);
OPCPackage src12Package = OPCPackage.open(src12);
OPCPackage src13Package = OPCPackage.open(src13);
OPCPackage src14Package = OPCPackage.open(src14);
OPCPackage src15Package = OPCPackage.open(src15);
OPCPackage src16Package = OPCPackage.open(src16);
OPCPackage src17Package = OPCPackage.open(src17);
OPCPackage src18Package = OPCPackage.open(src18);
OPCPackage src19Package = OPCPackage.open(src19);
OPCPackage src20Package = OPCPackage.open(src20);
OPCPackage src21Package = OPCPackage.open(src21);
OPCPackage src22Package = OPCPackage.open(src22);
OPCPackage src23Package = OPCPackage.open(src23);
OPCPackage src24Package = OPCPackage.open(src24);
OPCPackage src25Package = OPCPackage.open(src25);


XWPFDocument src1Document = new XWPFDocument(src1Package);
CTBody src1Body = src1Document.getDocument().getBody();
XWPFDocument src2Document = new XWPFDocument(src2Package);
CTBody src2Body = src2Document.getDocument().getBody();
XWPFDocument src3Document = new XWPFDocument(src3Package);
CTBody src3Body = src3Document.getDocument().getBody();
XWPFDocument src4Document = new XWPFDocument(src4Package);
CTBody src4Body = src4Document.getDocument().getBody();
XWPFDocument src5Document = new XWPFDocument(src5Package);
CTBody src5Body = src5Document.getDocument().getBody();
XWPFDocument src6Document = new XWPFDocument(src6Package);
CTBody src6Body = src6Document.getDocument().getBody();
XWPFDocument src7Document = new XWPFDocument(src7Package);
CTBody src7Body = src7Document.getDocument().getBody();
XWPFDocument src8Document = new XWPFDocument(src8Package);
CTBody src8Body = src8Document.getDocument().getBody();
XWPFDocument src9Document = new XWPFDocument(src9Package);
CTBody src9Body = src9Document.getDocument().getBody();
XWPFDocument src10Document = new XWPFDocument(src10Package);
CTBody src10Body = src10Document.getDocument().getBody();
XWPFDocument src11Document = new XWPFDocument(src11Package);
CTBody src11Body = src11Document.getDocument().getBody();
XWPFDocument src12Document = new XWPFDocument(src12Package);
CTBody src12Body = src12Document.getDocument().getBody();
XWPFDocument src13Document = new XWPFDocument(src13Package);
CTBody src13Body = src13Document.getDocument().getBody();
XWPFDocument src14Document = new XWPFDocument(src14Package);
CTBody src14Body = src14Document.getDocument().getBody();
XWPFDocument src15Document = new XWPFDocument(src15Package);
CTBody src15Body = src15Document.getDocument().getBody();
XWPFDocument src16Document = new XWPFDocument(src16Package);
CTBody src16Body = src16Document.getDocument().getBody();
XWPFDocument src17Document = new XWPFDocument(src17Package);
CTBody src17Body = src17Document.getDocument().getBody();
XWPFDocument src18Document = new XWPFDocument(src18Package);
CTBody src18Body = src18Document.getDocument().getBody();
XWPFDocument src19Document = new XWPFDocument(src19Package);
CTBody src19Body = src19Document.getDocument().getBody();
XWPFDocument src20Document = new XWPFDocument(src20Package);
CTBody src20Body = src20Document.getDocument().getBody();
XWPFDocument src21Document = new XWPFDocument(src21Package);
CTBody src21Body = src21Document.getDocument().getBody();
XWPFDocument src22Document = new XWPFDocument(src22Package);
CTBody src22Body = src22Document.getDocument().getBody();
XWPFDocument src23Document = new XWPFDocument(src23Package);
CTBody src23Body = src23Document.getDocument().getBody();
XWPFDocument src24Document = new XWPFDocument(src24Package);
CTBody src24Body = src24Document.getDocument().getBody();
XWPFDocument src25Document = new XWPFDocument(src25Package);
CTBody src25Body = src25Document.getDocument().getBody();

appendBody(src1Body, src2Body, src3Body, src4Body, src5Body, src6Body, src7Body, src8Body, src9Body, src10Body, src11Body, src12Body, src13Body, src14Body, src15Body, src16Body, src17Body, src18Body, src19Body, src20Body, src21Body, src22Body, src23Body, src24Body, src25Body);
src1Document.write(dest);
} catch (Exception e) {
System.out.println("error-->" + e.getMessage());
}
}

private static void appendBody(CTBody src, CTBody append, CTBody append2, CTBody append3, CTBody append4, CTBody append5, CTBody append6, CTBody append7, CTBody append8, CTBody append9, CTBody append10, CTBody append11, CTBody append12, CTBody append13, CTBody append14, CTBody append15, CTBody append16, CTBody append17, CTBody append18, CTBody append19, CTBody append20, CTBody append21, CTBody append22, CTBody append23, CTBody append24) throws Exception {
XmlOptions optionsOuter = new XmlOptions();
optionsOuter.setSaveOuter();
String appendString = append.xmlText(optionsOuter);
String appendString2 = append2.xmlText(optionsOuter);
String appendString3 = append3.xmlText(optionsOuter);
String appendString4 = append4.xmlText(optionsOuter);
String appendString5 = append5.xmlText(optionsOuter);
String appendString6 = append6.xmlText(optionsOuter);
String appendString7 = append7.xmlText(optionsOuter);
String appendString8 = append8.xmlText(optionsOuter);
String appendString9 = append9.xmlText(optionsOuter);
String appendString10 = append10.xmlText(optionsOuter);
String appendString11 = append11.xmlText(optionsOuter);
String appendString12 = append12.xmlText(optionsOuter);
String appendString13 = append13.xmlText(optionsOuter);
String appendString14 = append14.xmlText(optionsOuter);
String appendString15 = append15.xmlText(optionsOuter);
String appendString16 = append16.xmlText(optionsOuter);
String appendString17 = append17.xmlText(optionsOuter);
String appendString18 = append18.xmlText(optionsOuter);
String appendString19 = append19.xmlText(optionsOuter);
String appendString20 = append20.xmlText(optionsOuter);
String appendString21 = append21.xmlText(optionsOuter);
String appendString22 = append22.xmlText(optionsOuter);
String appendString23 = append23.xmlText(optionsOuter);
String appendString24 = append24.xmlText(optionsOuter);


String srcString = src.xmlText();
String prefix = srcString.substring(0, srcString.indexOf(">") + 1);
String mainPart = srcString.substring(srcString.indexOf(">") + 1, srcString.lastIndexOf("<"));
String sufix = srcString.substring(srcString.lastIndexOf("<"));
String addPart = appendString.substring(appendString.indexOf(">") + 1, appendString.lastIndexOf("<"));
String addPart2 = appendString2.substring(appendString2.indexOf(">") + 1, appendString2.lastIndexOf("<"));
String addPart3 = appendString3.substring(appendString3.indexOf(">") + 1, appendString3.lastIndexOf("<"));
String addPart4 = appendString4.substring(appendString4.indexOf(">") + 1, appendString4.lastIndexOf("<"));
String addPart5 = appendString5.substring(appendString5.indexOf(">") + 1, appendString5.lastIndexOf("<"));
String addPart6 = appendString6.substring(appendString6.indexOf(">") + 1, appendString6.lastIndexOf("<"));
String addPart7 = appendString7.substring(appendString7.indexOf(">") + 1, appendString7.lastIndexOf("<"));
String addPart8 = appendString8.substring(appendString8.indexOf(">") + 1, appendString8.lastIndexOf("<"));
String addPart9 = appendString9.substring(appendString9.indexOf(">") + 1, appendString9.lastIndexOf("<"));
String addPart10 = appendString10.substring(appendString10.indexOf(">") + 1, appendString10.lastIndexOf("<"));
String addPart11 = appendString11.substring(appendString11.indexOf(">") + 1, appendString11.lastIndexOf("<"));
String addPart12 = appendString12.substring(appendString12.indexOf(">") + 1, appendString12.lastIndexOf("<"));
String addPart13 = appendString13.substring(appendString13.indexOf(">") + 1, appendString13.lastIndexOf("<"));
String addPart14 = appendString14.substring(appendString14.indexOf(">") + 1, appendString14.lastIndexOf("<"));
String addPart15 = appendString15.substring(appendString15.indexOf(">") + 1, appendString15.lastIndexOf("<"));
String addPart16 = appendString16.substring(appendString16.indexOf(">") + 1, appendString16.lastIndexOf("<"));
String addPart17 = appendString17.substring(appendString17.indexOf(">") + 1, appendString17.lastIndexOf("<"));
String addPart18 = appendString18.substring(appendString18.indexOf(">") + 1, appendString18.lastIndexOf("<"));
String addPart19 = appendString19.substring(appendString19.indexOf(">") + 1, appendString19.lastIndexOf("<"));
String addPart20 = appendString20.substring(appendString20.indexOf(">") + 1, appendString20.lastIndexOf("<"));
String addPart21 = appendString21.substring(appendString21.indexOf(">") + 1, appendString21.lastIndexOf("<"));
String addPart22 = appendString22.substring(appendString22.indexOf(">") + 1, appendString22.lastIndexOf("<"));
String addPart23 = appendString23.substring(appendString23.indexOf(">") + 1, appendString23.lastIndexOf("<"));
String addPart24 = appendString24.substring(appendString24.indexOf(">") + 1, appendString24.lastIndexOf("<"));
/*System.out.println("prefix-->"+prefix);
System.out.println("mainPart-->"+mainPart);
System.out.println("addPart-->"+addPart);
System.out.println("sufix-->"+sufix);*/
//String addPart2 = appendString2.substring(appendString2.indexOf(">") + 1, appendString2.lastIndexOf("<"));
CTBody makeBody = CTBody.Factory.parse(prefix + mainPart + addPart + addPart2 + addPart3 + addPart4 + addPart5 + addPart6 + addPart7 + addPart8 + addPart9 + addPart10 + addPart11 + addPart12 + addPart13 + addPart14 + addPart15 + addPart16 + addPart17 + addPart18 + addPart19 + addPart20 + addPart21 + addPart22 + addPart23 + addPart24 + sufix);
src.set(makeBody);
}

最佳答案

我认为这应该非常接近您想要做的事情。但请注意,我还没有编译它或尝试运行它。我要做的唯一的其他简化可能是进行数据库查询以将 blob 放入循环中,以进一步简化。祝你好运。

import geq.gdig.dao.ConexionBD;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.xmlbeans.XmlOptions;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBody;

/**
*
* @author euriber
*/
public class PruebaBLOB extends HttpServlet {

/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request
* servlet request
* @param response
* servlet response
* @throws ServletException
* if a servlet-specific error occurs
* @throws IOException
* if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
// PrintWriter out = response.getWriter();
System.out.println("Hola");
String lNombreArchivo = null;
// An ordered list
List<Blob> blobContent = new LinkedList<Blob>();
// query content from documentos_blob for multiple document ids and
// return all content in a single result set
String query1 = "select d.id_documento,(select bl.content from documentos_blob bl where bl.id_documento=69)content69,"
+ "(select bl.content from documentos_blob bl where bl.id_documento=63)content63,"
+ "(select bl.content from documentos_blob bl where bl.id_documento=53)content53,"
+ "(select bl.content from documentos_blob bl where bl.id_documento=68)content68,"
+ "(select bl.content from documentos_blob bl where bl.id_documento=54)content54,"
+ "(select bl.content from documentos_blob bl where bl.id_documento=55)content55,"
+ "(select bl.content from documentos_blob bl where bl.id_documento=74)content74,"
+ "(select bl.content from documentos_blob bl where bl.id_documento=51)content51,"
+ "(select bl.content from documentos_blob bl where bl.id_documento=75)content75,"
+ "(select bl.content from documentos_blob bl where bl.id_documento=60)content60,"
+ "(select bl.content from documentos_blob bl where bl.id_documento=56)content56,"
+ "(select bl.content from documentos_blob bl where bl.id_documento=61)content61,"
+ "(select bl.content from documentos_blob bl where bl.id_documento=59)content59,"
+ "(select bl.content from documentos_blob bl where bl.id_documento=57)content57,"
+ "(select bl.content from documentos_blob bl where bl.id_documento=62)content62,"
+ "(select bl.content from documentos_blob bl where bl.id_documento=52)content52,"
+ "(select bl.content from documentos_blob bl where bl.id_documento=73)content73,"
+ "(select bl.content from documentos_blob bl where bl.id_documento=58)content58,"
+ "(select bl.content from documentos_blob bl where bl.id_documento=65)content65,"
+ "(select bl.content from documentos_blob bl where bl.id_documento=64)content64,"
+ "(select bl.content from documentos_blob bl where bl.id_documento=66)content66,"
+ "(select bl.content from documentos_blob bl where bl.id_documento=67)content67,"
+ "(select bl.content from documentos_blob bl where bl.id_documento=70)content70,"
+ "(select bl.content from documentos_blob bl where bl.id_documento=71)content71,"
+ "(select bl.content from documentos_blob bl where bl.id_documento=72)content72"
+ " from documentos d where d.id_documento = 1";

String query2 = "select d.* from documentos d where d.id_documento= 2";
Context ic = null;
DataSource dataSource = null;
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
System.out.print("Dentro del try");
ic = new InitialContext();
dataSource = (DataSource) ic.lookup(ConexionBD.DATA_SOURCE_GEQGDIG);
conn = dataSource.getConnection();
st = conn.createStatement();
System.out.print("Antes de execute...");
rs = st.executeQuery(query1);
System.out.print(query1);
System.out.print("Después de execute...");

// Iterate over the result set (one iteration only??)
while (rs.next()) {
// System.out.print("Dentro del while...");
lNombreArchivo = "PED_SPF_1ER_INFORME.docx";
System.out.println("lNombreArchivo-->" + lNombreArchivo);
// Get blob data from each column in the result set

blobContent.add(rs.getBlob("CONTENT69"));
blobContent.add(rs.getBlob("CONTENT63"));
blobContent.add(rs.getBlob("CONTENT53"));
blobContent.add(rs.getBlob("CONTENT68"));
blobContent.add(rs.getBlob("CONTENT54"));
blobContent.add(rs.getBlob("CONTENT55"));
blobContent.add(rs.getBlob("CONTENT74"));
blobContent.add(rs.getBlob("CONTENT51"));
blobContent.add(rs.getBlob("CONTENT75"));
blobContent.add(rs.getBlob("CONTENT60"));
blobContent.add(rs.getBlob("CONTENT56"));
blobContent.add(rs.getBlob("CONTENT61"));
blobContent.add(rs.getBlob("CONTENT59"));
blobContent.add(rs.getBlob("CONTENT57"));
blobContent.add(rs.getBlob("CONTENT62"));
blobContent.add(rs.getBlob("CONTENT52"));
blobContent.add(rs.getBlob("CONTENT73"));
blobContent.add(rs.getBlob("CONTENT58"));
blobContent.add(rs.getBlob("CONTENT65"));
blobContent.add(rs.getBlob("CONTENT64"));
blobContent.add(rs.getBlob("CONTENT66"));
blobContent.add(rs.getBlob("CONTENT67"));
blobContent.add(rs.getBlob("CONTENT70"));
blobContent.add(rs.getBlob("CONTENT71"));
blobContent.add(rs.getBlob("CONTENT72"));

// prepare response headers
response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document"
+ "; name=\"" + lNombreArchivo + "\"");
response.setHeader("Content-disposition", "attachment; filename=\"" + lNombreArchivo + "\"");
// prepare output stream
OutputStream os = response.getOutputStream();
// Bind OPCPackages to each inputstream
CTBody src = null;
XWPFDocument outDoc = null;
StringBuilder sb = new StringBuilder();
Iterator<Blob> li = blobContent.listIterator();
String suffix;
XmlOptions optionsOuter = new XmlOptions();
optionsOuter.setSaveOuter();
while (li.hasNext()) {
// for each blob
Blob b = li.next();
// Do the repeated stuff
InputStream is = b.getBinaryStream();
OPCPackage opc = OPCPackage.open(is);
XWPFDocument doc = new XWPFDocument(opc);
CTBody body = doc.getDocument().getBody();
if (sb.length() == 0) {
// First blob in the list we only enter here
outDoc = doc;
// Get src XML text string
String bodyXml = body.xmlText();
// Get prefix xml tag
sb.append(bodyXml.substring(0, bodyXml.indexOf(">") + 1));
// Get main XML body
sb.append(bodyXml.substring(bodyXml.indexOf(">") + 1, bodyXml.lastIndexOf("<")));
// Get final XML tag
suffix = bodyXml.substring(bodyXml.lastIndexOf("<"));
} else {
// all other blobs in the list, get the body XML between
// first and last XML tags
String bodyXml = body.xmlText(optionsOuter);
sb.append(bodyXml.substring(bodyXml.indexOf(">") + 1, bodyXml.lastIndexOf("<")));
}
}
// Finally append the suffix
sb.append(suffix);
// Rewrite the document body with the entire contents of the
// string builder.
outDoc.getDocument().getBody().set(CTBody.Factory.parse(sb.toString()));
// Write it out to the stream
outDoc.write(os);
// OutputStream os
os.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (conn != null) {
conn.close();
}
if (rs != null) {
rs.close();
}
if (st != null) {
st.close();
}
} catch (Exception e) {
}
}
}

}

关于java - 使用 Java Servlet 合并 Oracle 中的多个 BLOB 以下载到 .docx 文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38150664/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com