File Upload of Java Web Service Example

| No TrackBacks

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!");
}

}

No TrackBacks

TrackBack URL: http://server.everfine.com.tw/blog/mt-tb.cgi/251

March 2010

Sun Mon Tue Wed Thu Fri Sat
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      

Archives

Powered by Movable Type 4.33-en

About this Entry

This page contains a single entry by philipz published on May 30, 2009 11:03 AM.

Find recent content on the main index or look in the archives to find all content.