Free Support Forum - aspose.com

Error opening file in multi threaded java application

Hi! I am trying to retrieve all documents from different folders through a multi-threaded Aspose word java application. But I get ‘FileCorruptedException’ in some documents. Kindly help me. Thanking you in anticipation


P.S
‘FileCorruptedException’ occurs randomly. I mean the documents keeps on changing. It doesn’t occur on a specific set of documents.

Hi

Thanks for your request. Please provide me the code that will allow me to reproduce this problem.

Also make sure that you are using separate Document object per each thread. One thread should use one Document object.

Best regards.

ProcessDocuments is my class which is implementing runnable interface.Following is the summay of my code


ProcessDocuments processDocuments = new ProcessDocuments();
ProcessDocuments.getLicense();

for(int i=0;i<THREAD_COUNT;i++)
{
processDocuments = new ProcessDocuments();
thread = new Thread(processDocuments);
thread.setName(Integer.toString(i));
thread.start();
}

My Run Method is Executing following method

private void makeTheDocumentReadOnlyAndInsertWatermark(String fileName,String sourceDirectory,String destDirectory,int fileNo,int totalFiles)
{
int currentFile = fileNo+1;
System.out.println(“File Name : “+fileName+”\nFolder : “+sourceDirectory+”\nFile : “+currentFile+”/”+totalFiles+"\n");

System.out.println(fileName + " " + Thread.currentThread().getName());
try
{
Document doc = new Document(sourceDirectory+fileName); //exception occur here
DocumentBuilder builder = new DocumentBuilder(doc);
doc.protect(ProtectionType.READ_ONLY);
insertWaterMark(doc,builder,“APPROVED”); // my method to insert watermark
doc.save(destDirectory+fileName);
doc=null;
}
catch(Exception e)
{
System.out.println(“Name = " + Thread.currentThread().getName() + " Filename” + fileName);

e.printStackTrace();
}
}




Hi,

The ctor ‘new Document(String filename)’ opens a template file read-only and normally shouldn’t cause such an exception (if you don’t modifying the template doc in some another thread). So, please check did you modifying the template doc in any thread (including open Word editor).

Also please provide me your full stack trace - com.aspose.words.FileCorruptedException in this ctor wraps a range of different exceptions from IOException to NullPointerException – but we should to know the real cause.

And at last, for workaround you can try another Document ctor: Document(InputStream stream) instead of Document(String filename). The last one uses RandomAccessFile internally – may be there are some conflicts occur in your specific environment when you are using this class in multithread.

Best regards,