当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Programmatically update image in Applet, which was received from servlet

问题描述:

I want a desktop(mac preferable) side Java program, which should be able to receive images from a servlet and show image. Basically, I have a mobile client application, which sends an image periodically to a servlet which runs on Eclipse with Tomcat server on Mac. So, This servlet receives an updated image from the client periodically. This is done already.

Now, I want to push this image from this servlet project to a desktop Java applet or some desktop based Java application, where it will be updating with the image it receives periodically. Both the server and desktop applet can run on the same machine for testing.

Could someone please advise,

  1. How can I push the image received in the server side servlet project to the desktop java applet program?
  2. How would I program it, what is the easy way to develop desktop(mac preferable) based java applet which can receive images and show? I want to develop in java language only for viewing this image received.

网友答案:

This solution requires a slight change to your web server :

The best way forward is to create some kind of API call that your program can make that will request this image as an HTTP or HTTPS download. That way, you can save the image however you like on the server and then stream it back to the calling client.

Your desktop solution will be a standard Java SE application, NetBeans has a very neat feature that allows you to package an application as a native Mac app, which doesn't need Java installed, it has it's own JRE bundled. You can do the same in Eclipse, as it's a Java feature, I just haven't had luck getting it to work yet.

What you will then do, from your desktop, is simply have a thread that gets called, let's say, every 5 seconds to poll the API service for a new image. If there is one, it can download it using a standard HTTP call and then display the image.

There is of course a simple cheat as well - simply have your web server save the image file in a publicly readable folder, place an HTML file that displays that image there and using JavaScript or HTML Header Refresh META TAG to refresh this page once every second. That way, you do not even need an application, you can just open the page in Safari.

分享给朋友:
您可能感兴趣的文章:
随机阅读: