Return-Path: Delivered-To: apmail-ws-axis-user-archive@www.apache.org Received: (qmail 61722 invoked from network); 29 May 2004 19:27:22 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur-2.apache.org with SMTP; 29 May 2004 19:27:22 -0000 Received: (qmail 39326 invoked by uid 500); 29 May 2004 19:27:10 -0000 Delivered-To: apmail-ws-axis-user-archive@ws.apache.org Received: (qmail 39238 invoked by uid 500); 29 May 2004 19:27:09 -0000 Mailing-List: contact axis-user-help@ws.apache.org; run by ezmlm Precedence: bulk Reply-To: axis-user@ws.apache.org list-help: list-unsubscribe: list-post: Delivered-To: mailing list axis-user@ws.apache.org Received: (qmail 39228 invoked by uid 99); 29 May 2004 19:27:09 -0000 Received: from [210.18.48.2] (HELO shardagate.mahindrabt.com) (210.18.48.2) by apache.org (qpsmtpd/0.27.1) with ESMTP; Sat, 29 May 2004 12:27:09 -0700 Received: from Interscan_sh ([10.5.2.145]) by shardagate.mahindrabt.com (8.12.11/8.12.10) with ESMTP id i4TJQYZj006259 for ; Sun, 30 May 2004 00:56:34 +0530 Received: from intranet.sharda.mahindrabt.com ([10.5.0.15]) by 10.5.2.145 wi th trend_isnt_name_B; Sun, 30 May 2004 01:01:21 +0530 Received: from MBT ([172.24.86.113])by intranet.sharda.mahindrabt.com (8.12. 10/8.12.10) with SMTP id i4TJignW028111for ; Sun, 30 May 2004 01:14:43 +0530 X-MSReally-From: rommels@mahindrabt.com Message-ID: <001101c445b3$1bfacba0$715618ac@MBT> From: "Rommel Sharma" To: References: <5FA8811AC134664B9941F72F926EB3CA01F45E26@ex-2.internal.salesfor ce.com> <19e0530f040529103911efd601@mail.gmail.com> Subject: Java Code To Send An Attachment Date: Sun, 30 May 2004 00:58:22 +0530 Organization: Mahindra British Telecom MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_NextPart_000_000E_01C445E1.34D8D440" X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2800.1106 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106 X-imss-version: 2.0 X-imss-result: Passed X-imss-scores: Clean:19.9880 C:20 M:1 S:5 R:5 X-imss-settings: Baseline:1 C:1 M:1 S:1 R:1 (0.0000 0.0000) X-Virus-Checked: Checked X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N This is a multi-part message in MIME format. ------=_NextPart_000_000E_01C445E1.34D8D440 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Hi, I have attached Java code for sending attachments using Axis for those who are yet to empower their webservice with attachments and want to see the complete client side and server side code. I have tried to keep the code very simple and clear and added documentation too. Hope the newbies will benefit from it. Thanks, Rommel Sharma. ********************************************************* Disclaimer: This message (including any attachments) contains confidential information intended for a specific individual and purpose, and is protected by law. If you are not the intended recipient, you should delete this message and are hereby notified that any disclosure, copying, or distribution of this message, or the taking of any action based on it, is strictly prohibited. ********************************************************* Visit us at http://www.mahindrabt.com ------=_NextPart_000_000E_01C445E1.34D8D440 Content-Type: application/octet-stream; name="AttachmentSoapBindingImpl.java" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="AttachmentSoapBindingImpl.java" /** * AttachmentSoapBindingImpl.java * This file was auto-generated from WSDL * by the Apache Axis WSDL2Java emitter. */ package ws; import java.io.*; import org.apache.axis.AxisFault; //For Iterator that reads MimeHeaders import java.util.*; //For attachments import org.apache.axis.Message; import org.apache.axis.MessageContext; import javax.activation.DataSource; import javax.activation.DataHandler; import org.apache.axis.attachments.Attachments; import org.apache.axis.attachments.AttachmentPart; /** *This program displays how to receive the files sent by a client as an = attachment. *@author: Rommel Sharma. * In this program, the getMessageAttachments() implementation as = provided * by Steve Loughran in his mail to axis-user group * http://www.mail-archive.com/axis-user@xml.apache.org/msg08732.html */ public class AttachmentSoapBindingImpl implements = ws.FileUploadServerInterface { //The status message for attachment download that we will send back to = the client. String status =3D "The atachment has been received and saved."; /** * This method receives the MIME encoded attachment from the client and = saves it on the server. * @return status A string informing the client if the attachment was = received correctly. * @param obj An object to receive the Vector containing the file names = of the attachments. */ public java.lang.String saveFile(java.lang.Object obj) throws = java.rmi.RemoteException { InputStream is =3Dnull; FileOutputStream os =3D null; try { //File names vector Vector fileInfo =3D (Vector)obj; //Get all the attachments AttachmentPart[] attachments =3D getMessageAttachments(); /* * getMessageAttachments() as provided by Steve Loughran in his mail = to axis-user group * = http://www.mail-archive.com/axis-user@xml.apache.org/msg08732.html */ //Put the logic in a loop for totalAttachments for multiple = attachments. int totalAttachments =3D attachments.length; System.out.println("Total Attachments Received Are: = "+totalAttachments); //Extract the first attachment. (Since in this case we have only one = attachment sent) DataHandler dh =3D attachments[0].getDataHandler(); //Extract the file name of the first attachment. String name =3D (String)fileInfo.get(0); System.out.println("File received on server is: "+name); is =3Ddh.getInputStream(); File file =3D new File(name); os =3D new FileOutputStream(file); int i=3D0; while(i!=3D-1) { i=3Dis.read(); os.write(i); } is.close(); os.close(); } catch(Exception e) { status=3D"File Could Not Be Saved: "+e.getMessage(); System.out.println("In Impl: "+e); } return status; } //saveFile /* * Reusing the method implementation for AttachmentPart[] = getMessageAttachments() * as provided by Steve Loughran in his mail to axis-user group * http://www.mail-archive.com/axis-user@xml.apache.org/msg08732.html */ /** * extract attachments from the current request * @return a list of attachmentparts or * an empty array for no attachments support in this axis * buid/runtime */ private AttachmentPart[] getMessageAttachments() throws AxisFault { MessageContext msgContext =3D MessageContext.getCurrentContext(); Message reqMsg =3D msgContext.getRequestMessage(); Attachments messageAttachments =3D reqMsg.getAttachmentsImpl(); if (null =3D=3D messageAttachments) { System.out.println("no attachment support"); return new AttachmentPart[0]; } int attachmentCount=3D messageAttachments.getAttachmentCount(); AttachmentPart attachments[] =3D new AttachmentPart[attachmentCount]; Iterator it =3D messageAttachments.getAttachments().iterator(); int count =3D 0; while (it.hasNext()) { AttachmentPart part =3D (AttachmentPart) it.next(); attachments[count++] =3D part; } return attachments; } } ------=_NextPart_000_000E_01C445E1.34D8D440 Content-Type: application/octet-stream; name="FileUploadClient.java" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="FileUploadClient.java" package wsclient; import java.io.File; import java.net.URL; import java.util.Vector; //For attachments import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import org.apache.axis.attachments.AttachmentPart; /* *Call: This class can be used to invoke the Web Service. *It can be prefilled by a WSDL document (on the constructor to the = Service object) * or you can fill in the data yourself. */ import org.apache.axis.client.Call; /** *This program displays how to send files as an attachment. *The code is part of a webservice deployed on Axis 1.1 and Tomcat 1.4.29 *@author Rommel Sharma */ class FileUploadClient { public static void main(String args[]) throws Exception { //Specify the URL to your webservice that is needed by the = Stub String endpoint =3D = "http://serverIP:serverPort/axis/services/Attachment"; URL targetURL =3D new URL(endpoint); //Use a vector in case more than one attachment is being = added to store the file names. Vector fileNames =3D new Vector(); //In our example, we add just one attachment. File file =3D new = File("C:"+File.separator+"code"+File.separator+"wsclient"+File.separator+= "attach.pdf"); String name =3D file.getName(); /* Add the filename to the Vector, so that when the webservice on the = server * receives the attachments, it can resolve the filenames (name and = extension) * for the attachments received. */ fileNames.add(name); //--- Add some comments to see what we've got --- System.out.println(name); System.out.println("File exists?: "+file.exists()); //--- Above: some comments to see what we've got --- /* *Create a Service object using the Service and = ServiceLoactor classes generated using *the wsdl2java tool. We also use the stub, generated from = the same tool. *{ * FYR: I used the following options: * java org.apache.axis.wsdl.WSDL2Java -d Session = --server-side --skeletonDeploy true MyWSDL.wsdl *} */ ws.FileUploadServerInterfaceService service =3D new = ws.FileUploadServerInterfaceServiceLocator(); //Create a stub, for the webservice at the URL specified. ws.AttachmentSoapBindingStub stub =3D new = ws.AttachmentSoapBindingStub(targetURL, service); // Use classes from the Java Activation Framework (import = activation.jar) to wrap the attachment. DataHandler attachmentFile =3D new DataHandler(new = FileDataSource(file)); //Tell the stub that the message being formed also contains an = attachment, and it is of type MIME encoding. stub._setProperty(Call.ATTACHMENT_ENCAPSULATION_FORMAT, = Call.ATTACHMENT_ENCAPSULATION_FORMAT_MIME); //Add the attachment to the message stub.addAttachment(attachmentFile); System.out.println("Calling WS Method next..."); //Invoke the remote method through the stub, passing in the = filenames. System.out.println("I got: "+stub.saveFile(fileNames)); } } ------=_NextPart_000_000E_01C445E1.34D8D440--