如何使用 HTTP GET 从 Google Chart API 获取图像并存储在服务器上?
谁能告诉我他们是否知道如何使用 Google Chart API 将返回的图像存储在文件系统中而不是包含在网页中?
我的情况是,我希望 Java servlet 或侦听器在 Apache Tomcat 服务器上定期运行,以向 Google Chart API 发出 HTTP GET / POST 请求,并将生成的图像存储在文件系统或应用程序数据库中。稍后图像将被放置在 HTML 页面中。
想必我正在看这样的事情:
String result = null;
URL url = new URL(urlStr);
URLConnection conn = url.openConnection ();
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuffer sb = new StringBuffer();
String line;
while ((line = rd.readLine()) != null)
{
sb.append(line);
}
rd.close();
result = sb.toString();
结果是可以写入文件或数据库的图像?但某些输出可能必须被删除。
欢迎任何建议。
摩根先生。
Can anyone tell me if they know how to use the Google Chart API to store the image returned in the file system rather than be included in a webpage?
My situation is that I'd like a Java servlet or a listener to run at regular intervals on an Apache Tomcat server to make HTTP GET / POST requests to the Google Chart API and store the resulting images in the file system or application database. Later on the images would be placed in HTML pages.
Presumably I'm looking at something like this:
String result = null;
URL url = new URL(urlStr);
URLConnection conn = url.openConnection ();
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuffer sb = new StringBuffer();
String line;
while ((line = rd.readLine()) != null)
{
sb.append(line);
}
rd.close();
result = sb.toString();
Where result is the image which can be written to file or database? But where some output may have to be stripped out.
Any advice is welcome.
Mr Morgan.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编写一个程序,打开与 Google Chart API URL 的 HTTP 连接,例如 http://chart.apis.google.com/chart?cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World
读取响应流并将其写入 FileOutputStream
如果您需要比这更具体的帮助,请撰写评论或用更具体的问题更新问题:-)
编辑:
由于它是二进制数据而不是文本数据,因此请执行以下操作:不使用 Readers - 而是直接使用 InputStream。将字节读入临时缓冲区并将它们写入 FileOutputStream。
Write a program that opens an HTTP connection to a Google Chart API URL, such as http://chart.apis.google.com/chart?cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World
Read the response stream and write it to a FileOutputStream
If you need more specific help than this, please write a comment or update the question with more specific questions :-)
EDIT:
Since it's binary data and not textual data, do not use Readers - instead work directly with the InputStream. Read bytes into a temp buffer and write them to a FileOutputStream.
直接从服务器请求图像(例如使用 cURL 或类似方法),而不是插入
。当您获取图像时,将其存储在您的服务器上并链接到该本地版本。
不过,请检查图表 API 许可条款,看看您是否可以这样做。
Request the image directly from the server (e.g. using cURL or similar), instead of inserting an
<img src...>
. When you get the image, store it on your server and link to this local version.Check the Chart API license terms though, to see if you're allowed to do this.