Service:
--------
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.asman;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;
import javax.activation.DataHandler;
import javax.jws.WebMethod;
import javax.jws.WebService;
/**
*
* @author laxman
*/
@WebService()
public class AttachmentService_Impl {
public DataHandler dataHandler=null;
@WebMethod
public String attachAnyFile(DataHandler dataHandler, String file)
{
String filename="";
try {
//DataHandler dataHandler=new DataHandler(fileDataSource);
File f=new File(file);
filename=f.getName();
System.out.println("\n\t Writing to File /" + filename);
BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(filename));
BufferedInputStream bin = new BufferedInputStream(dataHandler.getInputStream());
byte[] buffer = new byte[256];
while (true)
{
int bytesRead = bin.read(buffer);
if (bytesRead == -1)
break;
bout.write(buffer, 0, bytesRead);
}
bin.close();
bout.close();
}
catch (Exception e)
{
System.out.println("\n\t [AttachmentService_Impl] Exception Occured While Writing in File : "+e);
return e.toString();
}
return ("\n\t File Attachment Completed Succesfully " + filename + "\t at " + new Date());
}
}
Client:
-------
package com.asman; |
02 | import javax.activation.DataHandler; |
03 | import javax.activation.*; |
04 | import java.io.*; |
05 | public class Client |
06 | { |
07 | public static void main(String[] ar) throws Exception |
08 | { |
09 | System.out.println( "\n\tstart client" ); |
10 | AttachmentServiceImplService service= new AttachmentServiceImplService(); |
11 | AttachmentServiceImpl port=service.getAttachmentServiceImplPort(); |
12 |
13 | String fileName_WithPath="d:/myfile.txt" ; |
14 | long length= new java.io.File fileName_WithPath.length(); |
17 | System.out.println( "\n\tCalling service to send a file from a customer..." ); |
18 |
19 | BufferedInputStream bin = new BufferedInputStream(dataHandler.getInputStream()); |
20 | byte b[]= new byte [( int )length]; |
21 | bin.read(b); |
22 | String result = port.attachAnyFile(b,fileName_WithPath); |
23 | System.out.println( "\n\t Result From Service: " +result); |
24 | System.out.println( "\n\tend client" ); |
25 | } |
26 | } |
No comments:
Post a Comment