Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to pass a Word Document into a byte array?

807580Apr 20 2010 — edited Apr 20 2010
Hi All
I have an application wherin i select multiple files and send them over to a webservice method.The files selected are zipped internally and sent across as a single entity.
I could pass the files and retrieve them using my test tool to check thir successful send and retrieval as long as the files were in "text" format.
But now i need to figure a way out to pass word docs or any other content types.I tried passing word docs in the mechanism that works fine for "text" files.But when i retrieve the files,and try to open the word documents the MS Word states that "the file is corrupt and cannot be opened". The following is the code.
public byte[] getContent(IDfSessionManager sessionManager,String repositoryName, String objectIdString) {

   ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
   StringBuffer sb = new StringBuffer("");
    try {
          System.out.println("Repository Name " + repositoryName);
           mySession = sessionManager.getSession(repositoryName);
           // Get the object ID based on the object ID string.
            DfId dfId = new DfId(objectIdString);
             if (dfId.isObjectId()) 
              {
	System.out.println("Successfully fetched id " + dfId);
	}

          IDfSysObject sysObj = (IDfSysObject) mySession.getObject(dfId);
          System.out.println("Content Type ++++++ " + sysObj.getContentType());
          ByteArrayInputStream buf = sysObj.getContent();
          int i = 0;
         InputStreamReader readInput = new InputStreamReader(buf, "UTF8");
         BufferedReader br = new BufferedReader(readInput);
         while (br.ready()) {
        sb.append(br.readLine());
         sb.append("\n");
                          }
          String captureText = sb.toString();
	try {
	is = new ByteArrayInputStream(captureText.getBytes("UTF-8"));
                	} catch (UnsupportedEncodingException e) {
                                e.printStackTrace();
			}

	final int BUF_SIZE = 1 << 8; // 1KiB buffer
	byte[] buffer = new byte[BUF_SIZE];
                int bytesRead = -1;
                 while ((bytesRead = is.read(buffer)) > -1) {
	outputStream.write(buffer, 0, bytesRead);
	}
	is.close();
	System.out.println("This is byte acquisition ###### >>>>>>>>>>>>>>>>>>>");
	binaryData = outputStream.toByteArray();
			
	File temp1 = File.createTempFile("Test",".docx");
	FileOutputStream fos = new FileOutputStream(temp1);
	fos.write(binaryData);
	fos.close();
	}
		// Handle any exceptions.
		catch (Exception ex) {
		ex.printStackTrace();

		}
		finally {
			sessionManager.release(mySession);
		}
		return binaryData;
	}
I assume that UTF8 must be applicatbe only to deal with .txt files.
I would really appreciate your guidance to obtain a way to acquire data from word documents so that i could open and read them without errors.

Thank You
Regards
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 18 2010
Added on Apr 20 2010
10 comments
1,115 views