First Method,
endpoint code
public String echoBinaryAsString(byte[] bytes,String filename) {
String filePath = "c:/softs/img/"+filename;
try {
FileOutputStream fos = new FileOutputStream(filePath);
BufferedOutputStream outputStream = new BufferedOutputStream(fos);
outputStream.write(bytes);
outputStream.close();
System.out.println("Received file: " + filePath);
} catch (IOException ex) {
System.err.println(ex);
throw new WebServiceException(ex);
}
return new String(bytes);
}
Second Method,
Endpoint code
// Use @XmlMimeType to map to DataHandler on the client side
public void fileUpload(String fileName,
@XmlMimeType("application/octet-stream")
DataHandler data) {
String filePath = "c:/softs/img/"+fileName;
InputStream is;
try {
is = data.getInputStream();
File file =new File(filePath);// File.createTempFile(filePath, "");
OutputStream os = new FileOutputStream(file.getAbsolutePath());
System.out.println(data.getName()+","+ data.getContentType()+", "+file.getAbsolutePath());
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1)
{
//System.out.println(bytesRead);
os.write(buffer, 0, bytesRead);
}
os.flush();
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // DataHandler
}
HelloWorldImplService serviceimp = new HelloWorldImplService();
HelloWorldImpl service = serviceimp.getHelloWorldImplPort(new MTOMFeature());
String teststring = service.sayHelloWorld("test from project");
System.out.println(teststring);
String fileName = "test.jpg";
String filePath = "c:/softs/img/" + fileName;
File file = new File(filePath);
// uploads a file
/*
try {
FileInputStream fis = new FileInputStream(file);
BufferedInputStream inputStream = new BufferedInputStream(fis);
byte[] imageBytes = new byte[(int) file.length()];
inputStream.read(imageBytes);
service.echoBinaryAsString(imageBytes,"serverpic.jpg");
inputStream.close();
System.out.println("File uploaded: " + filePath);
} catch (IOException ex) {
System.err.println(ex);
ex.printStackTrace();
}
*/
DataHandler dh = new DataHandler(new FileDataSource(filePath));
for(int i = 0; i< 100; i++){
service.fileUpload("dhTest"+i+".jpg", dh);
}