Web Service Part:
supposed that you have a picture named Desert.jpg in package resources
@WebService(serviceName = "PictureService")
@Stateless()
public class PictureService {
@WebMethod(operationName = "getImage")
public byte[] getImage( ) throws IOException {
URL resource = this.getClass().getResource("/resources/Desert.jpg");
InputStream in = resource.openStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
for(int read; (read = in.read(buf)) != -1;) {
bos.write(buf, 0, read);
}
return bos.toByteArray()s;
}
}
Client part as Servlet:
public class test extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
PictureService_Service service = new PictureService_Service();
PictureService port=service.getPictureServicePort();
response.setContentType("image/jpeg");
OutputStream out = response.getOutputStream();
byte[] content = port.getImage();
out.write(content);
out.close();
} catch (IOException_Exception ex) {
Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}
}
No comments:
Post a Comment