I forgot where this code appears from?
Server side:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;import javax.activation.DataHandler;
public class ImageService {
public void uploadImage(String productId, DataHandler image) {
System.out.println(image.getContentType());
try {
InputStream in = image.getInputStream();
String imageDir = "c:/tmp";
FileOutputStream out = new FileOutputStream(new File(imageDir,
productId));
try {
byte buf[] = new byte[1024];
for (;;) {
int noBytesRead = in.read(buf);
out.write(buf, 0, noBytesRead);
if (noBytesRead < buf.length) {
break;
}
}
} finally {
out.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Client side:
import java.io.ByteArrayOutputStream;
import java.io.IOException;import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;public class ImageServiceClient {
/**
* @param args
*/
public static void main(String[] args) {
ImageServiceService service = new ImageServiceService();
/*service._getServiceClient().getOptions().setProperty(
Constants.Configuration.ENABLE_MTOM, "true");*/
ImageServiceDelegate delegate = service.getImageServicePort();
DataSource source = new FileDataSource("c:/90325-232111.jpg");
DataHandler handler = new DataHandler(source);
ByteArrayOutputStream buffOS= new ByteArrayOutputStream();
try {
handler.writeTo(buffOS);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] buff = buffOS.toByteArray();
delegate.uploadImage("p01.jpg", buff);
System.out.println("Done!");
}}



