Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

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!

Axis - Send Attachment from Client to Server

843833May 4 2007 — edited Mar 19 2008
Hi Everybody!

I have the following problem.

I have to develop a Web Service, using Axis, and a client that is able to download data and upload data.

Last week I implemented a mechanism to download data from the webservice to the client. This works perfect, even for file above 1.5Gb.

No I used the same methods for the upload mechanism and I have the problem that the client throws an OutOfMemory-Exception as soon as the server executes "request.getAttachments". This exception is thrown somewhere deep inside the AxisClient.invoke()-method.

But it works for small attachments with sizes like 5Mb...

Here is my code:

Client:
	private void addFileToSOAPMessage(File file, Call call, String contentId) {
	
		logger.debug("entering addFileToSOAPMessage");
		
		//Create the data for the attached file.
        DataHandler dhSource = new DataHandler(new FileDataSource(file));
		
        call.addAttachmentPart(dhSource); //Add the file.
        
        call.setProperty(Call.ATTACHMENT_ENCAPSULATION_FORMAT, Call.ATTACHMENT_ENCAPSULATION_FORMAT_DIME);
		
		logger.debug("exiting addFileToSOAPMessage");
		
	}
Server:
private void readFilesFromSOAPMessage() {
		
		logger.debug("entering readFilesFromSOAPMessage");
		
		Message request = AxisEngine.getCurrentMessageContext().getCurrentMessage();
		
		Iterator it = request.getAttachments();
		
		System.out.println("FOUND: " +request.countAttachments() + " ATTACHMENTS");
		
		//Save the files to the temporary-directory
		while (it.hasNext()) {
			try {
			  org.apache.axis.attachments.AttachmentPart attachment = (org.apache.axis.attachments.AttachmentPart)it.next();
	          javax.activation.DataHandler dh = attachment.getDataHandler();
	          org.apache.axis.attachments.ManagedMemoryDataSource mmds = (org.apache.axis.attachments.ManagedMemoryDataSource)dh.getDataSource();
	          if (mmds!=null){
	        	  	        	  
	              java.io.InputStream is = mmds.getInputStream();	
	              
	              //Get File-Type (XML Description or Uploaded File)
	              java.io.File attachedFile = null;
	              
	              if (KeyValueMap.CL_UPLD_FILE.equalsIgnoreCase(attachment.getContentId())) {
	            	  this.setFile(new File(CUtlInit.getTempDir() + File.separator + this.getFilePath().substring(this.getFilePath().lastIndexOf(File.separator))));
					  attachedFile = this.getFile();
	              }
	              else if (KeyValueMap.CL_UPLD_XML_FILE.equalsIgnoreCase(attachment.getContentId())) {
	            	  this.setXmlFile(new File(CUtlInit.getTempDir() + File.separator + this.getFilePathXml().substring(this.getFilePathXml().lastIndexOf(File.separator))));
					  attachedFile = this.getXmlFile();
	              }
	              else {
	            	  logger.error("Wrong Attachment Content Id");
	            	  logger.debug("exiting readFilesFromSOAPMessage");
	            	  return;
	              }
	              
	              java.io.BufferedOutputStream bos = new java.io.BufferedOutputStream(new java.io.FileOutputStream(attachedFile));
	              int size = 0;
	              byte[] buf = new byte[1024*8];
	              while ((size = is.read(buf)) > -1) {
	                  bos.write(buf, 0, size);
	              }
	              bos.flush();
	              bos.close();
	              //Delete the temporary file
	              mmds.delete();
	          }
			} catch (SOAPException sex) {
				logger.error(sex.getMessage());
			} catch (IOException ioex) {
				logger.error(ioex.getMessage());
			}				

		}	
		
		logger.debug("exiting readFilesFromSOAPMessage");
		
	}
The only difference between sending a file from the server to the client and sending a file from the client to the server is that once i use "response.addAttachmentPart()" and once I use call.addAttachmentPart()"...

I am glad for any help, because I really got stuck here.
I do not understand why the mechanism works when transmitting data from the server to the client but not in the opposite direction, from the client to the server.

Maybe there is anything I have to configure in the wsdd-file?

Please do not tell me to read the Axis, Echo-Attachment Sample. I already did this but the problem is that i have a defined interface to the webservice like "String handleCommand(String)". I can not alter this to support an additional parameter like a DataHandler. I have to put the attachments directly into the reponse or the request.

Thanks in Advance!
Stefan
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 16 2008
Added on May 4 2007
2 comments
469 views