Server Side code :
Below file start the Grizzly server and register the uploadService class as jax-ws web service
package com.sap;
import com.sun.grizzly.http.embed.GrizzlyWebServer;
import java.io.IOException;
import javax.xml.ws.Endpoint;
import javax.xml.ws.spi.http.HttpContext;
import org.jvnet.jax_ws_commons.transport.grizzly_httpspi.GrizzlyHttpContextFactory;
public class JaxwsMain {
/**
* @param args
*/
public static void main(String[] args) {
String contextPath = "/ws";
String path = "/test";
int port = 8081;
String address = "http://localhost:"+port+contextPath+path;
GrizzlyWebServer server = new GrizzlyWebServer(port);
HttpContext context = GrizzlyHttpContextFactory.createHttpContext(server, contextPath, path);
Endpoint endpoint = Endpoint.create(new UploadService());
//endpoint.create(new UploadService());
endpoint.publish(context);
try {
server.start();
System.out.println(12121);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
below is uploadService class that basically upload the file
package com.sap;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.xml.ws.WebServiceException;
@WebService
public class UploadService {
/* @WebMethod
public int up(@WebParam(name="value1") int value1, @WebParam(name="value2") int value2) {
return value1 + value2;
}
*/
@WebMethod
public void upload(String fileName, byte[] imageBytes) {
String filePath = "D:/desktops/12march2014/uploads/" + fileName;
try {
FileOutputStream fos = new FileOutputStream(filePath);
BufferedOutputStream outputStream = new BufferedOutputStream(fos);
outputStream.write(imageBytes);
outputStream.close();
System.out.println("Received file: " + filePath);
} catch (IOException ex) {
System.err.println(ex);
throw new WebServiceException(ex);
}
}
}
run JaxwsMain Java class as java application . That will register the uploadService on Grizzly server\
use wsimport jax-ws utility from command line to generate the Client artifacts in Client project
This will create below files
- ObjectFactory.java
- package-info.java
- Upload.java
- UploadResponse.java
- UploadService.java
- UploadServiceService.java
below the Client code file that will invoke the upload service
Execute this File and pass the file to be uploaded
package com.sa;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
import java.util.logging.FileHandler;
import javax.xml.ws.WebServiceRef;
import javax.xml.ws.soap.MTOMFeature;
public class JaxwsClient {
/**
* @param args
*/
public static void main(String[] args) {
JaxwsClient client =new JaxwsClient();
client.doTest(args);
}
public void doTest(String[] args) {
UploadServiceService service = new UploadServiceService();
UploadService port = service.getPort(UploadService.class, new MTOMFeature(10240));
String fileName = "tpd-alert-1.2.0.zip";
String filePath = "D:/desktops/12march2014/" + fileName;
File file = new File(filePath);
if (args.length >0 && null != args[0]) {
file = new File(args[0]);
} else {
System.out.println("Enter full path of file..");
String path;
Scanner scanIn = new Scanner(System.in);
path = scanIn.nextLine();
file = new File(path);
}
try {
filePath=file.getPath();
FileInputStream fis = new FileInputStream(file);
BufferedInputStream inputStream = new BufferedInputStream(fis);
byte[] imageBytes = new byte[(int) file.length()];
inputStream.read(imageBytes);
port.upload(file.getName(), imageBytes);
inputStream.close();
System.out.println("File uploaded: " + filePath);
} catch (IOException ex) {
System.err.println(ex);
}}
}
Done!!!
No comments:
Post a Comment