Home > Back-end >  Java download the word from the server to the web client, change the word, will modify the word docu
Java download the word from the server to the web client, change the word, will modify the word docu

Time:11-03

Java download the word from the server to the web client, change the word, will modify the word documents uploaded to the server again, replace files, before pursuing big brand how to write!!!!!!
I've got to save is to modify the code to the local, don't want to save to a local!
It is according to the word document to bookmark location insertion picture


//eapFileUrl download files from the server url, path, gongyi of query to file id
URL the URL=new URL (download file server URL + "download/? ID="+ document ID);
HttpURLConnection conn=(HttpURLConnection) url. OpenConnection ();
//set super time for 3 seconds
Conn. SetConnectTimeout (3 * 1000);
//to prevent blocking fetching and returns a 403 error
Conn. SetRequestProperty (" the user-agent ", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT. DigExt) ");

//get the input stream
InputStream InputStream=conn. GetInputStream ();
//get the data file
WordprocessingMLPackage wPackage=WordprocessingMLPackage. Load (inputStream);
//to extract text
MainDocumentPart MainDocumentPart=wPackage. GetMainDocumentPart ();
The Document wmlDoc=(Document) mainDocumentPart. GetJaxbElement ();
Body Body=wmlDoc. GetBody ();
//all paragraphs in extraction of text
List Paragraphs=body. GetContent ();
//extract and create bookmarks cursor
RangeFinder rt=new RangeFinder (" CTBookmark ", "CTMarkupRange");
New TraversalUtil (paragraphs, rt);
For (CTBookmark bm: rt getStarts ()) {
//here can be a single bookmarks, you can also use a map to deal with all the bookmarks
For (String sq: bookmart) {
If (bm. GetName () equals (sq)) {
//read the picture and translated into a byte array, because docx4j pictures can only be inserted into a byte array
InputStream is=new FileInputStream (" D: \ \ 33 PNG ");
Byte [] bytes=IOUtils. ToByteArray (is);
//create an inline images
BinaryPartAbstractImage imagePart=BinaryPartAbstractImage. CreateImagePart (wPackage, bytes);
//createImageInline function in the first four parameters I didn't find the specific what meaning
//the most one is to limit the width of the images, scaling on the basis of
The Inline Inline=imagePart. CreateImageInline (null, null, 0, 1, false, 1000);
//get the sign of the parent paragraph
P=P (P) (bm) getParent ());
The ObjectFactory factory=new ObjectFactory ();
//R object is anonymous complex type, but I don't know what the exact meaning, estimate the won't know until you have a good look at the ooxml
R run=factory. CreateR ();
//drawing as the canvas
Drawing Drawing=factory. CreateDrawing ();
Drawing. GetAnchorOrInline (). The add (inline);
The run. GetContent (). The add (drawing);
P.g etContent (). The add (run);
}
}
}
WPackage. Save (new FileOutputStream (" E: \ \ e.d ocx "));

  • Related