title: Downloading large files via web service in Java
body: <p>I have a web application sitting out there that is a repository for files. This web application provides web services that allow clients to search the repository and download any of the attachments via SOAP.</p>
  
  <p>Currently I have tried to use Spring-WS 1.5.8 with MTOM to send the attachment to the client, but I keep getting out of memory errors. I don't believe these errors are related to my Tomcat 6 instance because my server has 8GB of memory and I configured Tomcat to use 4GB of that. I am getting these errors on files as small as 200MB.</p>
  
  <p>I need to use SOAP, even though it's probably not the best approach at all. I would prefer a solution in Spring, but if that's not possible then I am open to other ideas. I read that one can use the AxiomSoapMessageFactory to stream files to the server for uploading purposes, but not the other way around. Is this true? I am using Java 6.</p>
  
  <p>Here is the error that I keep getting within the Spring WS Framework:</p>
  
  <pre><code>java.lang.OutOfMemoryError: Java heap space
      com.sun.xml.internal.messaging.saaj.util.ByteOutputStream.ensureCapacity(Unknown Source)
      com.sun.xml.internal.messaging.saaj.util.ByteOutputStream.write(Unknown Source)
      com.sun.xml.internal.messaging.saaj.packaging.mime.internet.BMMimeMultipart.find(Unknown Source)
      com.sun.xml.internal.messaging.saaj.packaging.mime.internet.BMMimeMultipart.readBody(Unknown Source)
      com.sun.xml.internal.messaging.saaj.packaging.mime.internet.BMMimeMultipart.getNextPart(Unknown Source)
      com.sun.xml.internal.messaging.saaj.packaging.mime.internet.BMMimeMultipart.parse(Unknown Source)
      com.sun.xml.internal.messaging.saaj.packaging.mime.internet.BMMimeMultipart.parse(Unknown Source)
      com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeMultipart.getCount(Unknown Source)
      com.sun.xml.internal.messaging.saaj.soap.MessageImpl.initializeAllAttachments(Unknown Source)
      com.sun.xml.internal.messaging.saaj.soap.MessageImpl.getAttachments(Unknown Source)
      org.springframework.ws.soap.saaj.Saaj13Implementation.getAttachment(Saaj13Implementation.java:305)
      org.springframework.ws.soap.saaj.SaajSoapMessage.getAttachment(SaajSoapMessage.java:226)
      org.springframework.ws.support.MarshallingUtils$MimeMessageContainer.getAttachment(MarshallingUtils.java:109)
      org.springframework.oxm.jaxb.Jaxb2Marshaller$Jaxb2AttachmentUnmarshaller.getAttachmentAsDataHandler(Jaxb2Marshaller.java:532)
      com.sun.xml.internal.bind.v2.runtime.unmarshaller.MTOMDecorator.startElement(Unknown Source)
      com.sun.xml.internal.bind.v2.runtime.unmarshaller.InterningXmlVisitor.startElement(Unknown Source)
      com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(Unknown Source)
      com.sun.xml.internal.bind.unmarshaller.DOMScanner.visit(Unknown Source)
      com.sun.xml.internal.bind.unmarshaller.DOMScanner.visit(Unknown Source)
      com.sun.xml.internal.bind.unmarshaller.DOMScanner.visit(Unknown Source)
      com.sun.xml.internal.bind.unmarshaller.DOMScanner.visit(Unknown Source)
      com.sun.xml.internal.bind.unmarshaller.DOMScanner.visit(Unknown Source)
      com.sun.xml.internal.bind.unmarshaller.DOMScanner.visit(Unknown Source)
      com.sun.xml.internal.bind.unmarshaller.DOMScanner.visit(Unknown Source)
      com.sun.xml.internal.bind.unmarshaller.DOMScanner.scan(Unknown Source)
      com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(Unknown Source)
      com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(Unknown Source)
      javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
      org.springframework.oxm.jaxb.Jaxb2Marshaller.unmarshal(Jaxb2Marshaller.java:421)
      org.springframework.ws.support.MarshallingUtils.unmarshal(MarshallingUtils.java:62)
      org.springframework.ws.client.core.WebServiceTemplate$3.extractData(WebServiceTemplate.java:374)
      org.springframework.ws.client.core.WebServiceTemplate.doSendAndReceive(WebServiceTemplate.java:560)
  </code></pre>