级别: 中级 Nicholas Chase (ibmquestions@nicholaschase.com), 自由撰稿人, Backstop Media
2007 年 7 月 05 日 您可能已经知道 Atom 联合格式是提供博客文章信息的一种方式。但是,您知不知道使用 Atom 联合格式结合 Atom 发布协议( Atom Publishing Protocol )可以管理媒体文件呢?本文就将介绍如何使用 Atom 创建基于 Web 的媒体储存库。
准备工作
阅读本文至少需要对 Atom 联合格式和一些 HTML 概念(比如说表单)有一定了解,文章中的例子使用的是 Apache Abdera 的 Java 版本,因此如果您想要使用特定的例子,那么可以借助 Java 的帮助,但是例子中的概念可以应用在任何编程语言的实现中,甚至可以应用于手写的非 API 辅助的应用程序中。
首先我们需要配置系统,使它能够处理 Atom。要完成这一步,我们将需要一个支持 Atom 发布协议( Atom Publishing Protocol )的应用服务器。Roller 很棒,并且甚至可以在此处的 developerWorks 网站上运行博客,不过其安装手册有 27 页那么长。幸运的是,Dave Johnson 已经把所有必备工具集成在了博客应用服务器中,可从以下链接获得:https://blogapps.dev.java.net/servlets/ProjectDocumentList?folderID=4598&expandFolder=4598&folderID=0. 要安装该服务器,只需将其解压至一个目录名中没有空格的目录内。然后,设置 CATALINA_HOME 环境变量并启动服务器(如图 1 所示)。
清单 1. 启动服务器
set CATALINA_HOME=c:\sw\blogapps-server-1.0.4
cd %CATALINA_HOME%\bin
startup
|
代码中的路径当然是要使用您自己的!
要运行示例应用程序,将需要一个支持 servlet 的服务器。博客应用程序中有 Tomcat,因此可以使用该服务器。如果要使用其他实例,则需要更改 server.xml 文件以防止端口冲突。
最后一步,安装 Apache Abdera 程序包(参见 参考资料 获得下载链接),这个程序可以使 APP 易于使用。解压该程序包,把 main 和 lib 目录下的所有 jar 文件添加至 CLASSPATH。
一切工作已准备就绪。
目标
这篇文章将向大家介绍如何创建一个图 1 所示的媒体集应用程序。
图 1. 最终产品
要实现这个目标,我们将创建一个 servlet,这个 servlet 使用 Atom 发布协议把媒体资源添加到一个 Atom 采集程序中,而 Atom 采集程序是某个工作空间的一部分。要添加各个媒体资源,我们都将使用一个 POST 请求把该文件发送到采集的目标 URL。
在执行这一操作时,服务器便会创建一个相应媒体链接条目 来引用它。然后就可以在 Atom feed 中解压所有与这些项有关的信息,再使用这个 Atom feed 在 Web 页面上把信息显示出来。
Atom 协议还需要使用到一些其他的 HTTP 方法。我们将使用 GET 查询信息,当然,使用 DELETE 删除信息和使用 PUT 编辑已存在的信息(如果可能的话)。
我们从服务文档开始。
服务文档
服务文档能显示出可用的工作空间和集。比方说,如果从如下 URL 中检索 Blogapp 服务:http://localhost:8080/roller/app,那么可使用如下内容(如清单 2 所示)。
清单 2. 服务文档
<?xml version="1.0" encoding="UTF-8"?>
<app:service xmlns:app="http://purl.org/atom/app#">
<app:workspace>
<atom:title xmlns:atom="http://www.w3.org/2005/atom"
>AdminBlog</atom:title>
<app:collection href=
"http://localhost:8080/roller/app/adminblog/entries">
<atom:title xmlns:atom="http://www.w3.org/2005/atom"
>Weblog Entries</atom:title>
<app:categories app:fixed="yes" app:scheme=
"http://localhost:8080/roller/adminblog/">
<atom:category xmlns:atom="http://www.w3.org/2005/atom"
atom:term="/General" atom:label="General" />
...
</app:categories>
<app:accept>entry</app:accept>
</app:collection>
<app:collection href=
"http://localhost:8080/roller/app/adminblog/resources">
<atom:title xmlns:atom="http://www.w3.org/2005/atom"
>Media Files</atom:title>
<app:accept>image/*</app:accept>
</app:collection>
</app:workspace>
<app:workspace>
<atom:title xmlns:atom=
"http://www.w3.org/2005/atom">main</atom:title>
<app:collection href=
"http://localhost:8080/roller/app/main/entries">
<atom:title xmlns:atom="http://www.w3.org/2005/atom"
>Weblog Entries</atom:title>
<app:categories app:fixed="yes" app:scheme=
"http://localhost:8080/roller/main/">
<atom:category xmlns:atom="http://www.w3.org/2005/atom"
atom:term="/General" atom:label="General" />
...
</app:categories>
<app:accept>entry</app:accept>
</app:collection>
<app:collection href=
"http://localhost:8080/roller/app/main/resources">
<atom:title xmlns:atom="http://www.w3.org/2005/atom"
>Media Files</atom:title>
<app:accept>image/*</app:accept>
</app:collection>
</app:workspace>
</app:service>
|
请注意,文档中显示了两个工作空间,分别是 Adminblog 和 main。此处我们只要考虑后者。它有两个集。第一个是 Weblog Entries,它设计为 Atom 条目并由 accept 元素指定。第二个是一个媒体集,用于接收图象。后面,我们将向这个集进行提交。
创建条目
首先,创建一个 Java 类,使用这个 Java 类创建一个简单的条目并提交至服务器。创建条目就像操作任意其他对象(如清单 3 所示)。
清单 3. 创建条目
import java.util.Date;
import org.apache.abdera.Abdera;
import org.apache.abdera.factory.Factory;
import org.apache.abdera.model.Document;
import org.apache.abdera.model.Entry;
import org.apache.abdera.protocol.client.Client;
import org.apache.abdera.protocol.client.CommonsClient;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
public class CreateEntry {
public static void main(String[] args) throws Exception {
Abdera abdera = new Abdera();
Factory factory = abdera.getFactory();
Entry entry = factory.newEntry();
entry.setId("urn:sampleEntryIdValue");
entry.setTitle("Pizza, pizza everywhere...");
entry.setUpdated(new Date());
entry.addAuthor("Nick Chase");
entry.setContent("... and not a thing to drink. What kind "+
"of place is this, anyway?");
Document<Entry> doc = entry.getDocument();
doc.writeTo(System.out);
System.out.println();
}
}
|
在 清单 3 中,我们创建了 Abdera 对象,Abdera 对象在其核心中包含了大多数函数。还提供了一个 Factory 对象,我们可以使用该对象创建新 Entry 对象。Entry 对象代表的是 entry 元素,就像我们在 Atom feed 中看到的一样。最后,我们可以设置元素的各种属性,比如说标题、更新日期、作者等等。
创建了 Entry 对象之后,便可以把它转换成 Document 元素,Document 元素可以方便地把 Entry 对象序列化为命令行。
其输出内容如清单 4 所示。
清单 4. 输出
<?xml version='1.0' encoding='UTF-8'?>
<entry xmlns="http://www.w3.org/2005/Atom">
<id>urn:sampleEntryIdValue</id>
<title type="text">Pizza, pizza everywhere...</title>
<updated>2007-03-19T03:59:49.750Z</updated>
<author>
<name>Nick Chase</name>
</author>
<content type="text">... and not a thing to drink. What kind of
place is this, anyway?</content>
</entry>
|
清注意,考虑到代码的可读性,此处添加了一些空格。
需要注意 content 元素。在一个条目中,它包含文本或 HTML 之类的内容,和一个文本的类型属性值。这是我们最终将要存储媒体文件的位置,不过首先还是看看如何在系统中添加条目。
要完成这一步,我们需要使用到一个 POST 请求,使用条目作为请求的内容(如清单 5 所示)。
清单 5. 发送条目
...
System.out.println();
Client client = new CommonsClient(abdera);
UsernamePasswordCredentials credentials =
new UsernamePasswordCredentials("admin", "admin");
client.addCredentials(
"http://localhost:8080/roller/app/main/entries",
null, null, credentials);
Document<Entry> docOut = client.post(
"http://localhost:8080/roller/app/main/entries",
entry).getDocument();
docOut.writeTo(System.out);
}
}
|
首先,创建一个新的 Client 对象。这个对象基本上是一个 HTTP 客户机。博客应用服务器适用用户名和密码来保护所有的 URL,因此要使服务器能正常工作,需要为客户机添加身份凭证。
POST 请求自身包含请求所指向的 URL和条目,条目构成请求的主要内容。
这个操作发回一个响应,我们可以使用这个响应检查响应代码,查看请求是否成功(没有显示)。
在执行 POST 请求时,服务器发回有关已创建条目的信息,如清单 6 所示。
清单 6. 创建的条目
<?xml version='1.0' encoding='utf-8'?>
<entry xmlns="http://www.w3.org/2005/Atom">
<title type="text">Pizza, pizza everywhere...</title>
<link rel="alternate"
href="http://localhost:8080/roller/main/entry/pizza_pizza_everywhere" />
<link rel="edit"
href="http://localhost:8080/roller/app/main/entry/46921b221167d69001116859d7ea0097" />
<category term="/General" />
<author>
<name>admin</name>
<email>admin@example.com</email>
</author>
<id>http://localhost:8080/roller/main/entry/pizza_pizza_everywhere
</id>
<updated>2007-03-19T03:59:50Z</updated>
<published>2007-03-19T03:59:50Z</published>
<content type="html">... and not a thing to drink.
What kind of place is this, anyway?</content>
<app:control xmlns:app="http://purl.org/atom/app#">
<app:draft>no</app:draft>
</app:control>
<atom-draft xmlns="http://rollerweblogger.org/namespaces/app">9</atom-draft>
</entry>
|
这个文档包含两段。第一段为 alternate 链接,通过它提供的 URL 可以在浏览器中浏览条目内容。第二段为 edit 链接,它提供的 URI 在内部表示数据。后面,如果试图编辑这个条目,那么就需要使用这个 URI。
添加了条目之后,要想在集中查看它,请在浏览器的地址栏中输入 http://localhost:8080/roller/app/main/entries。应该可以看到新的条目内容,如清单 7 所示。
清单 7. 集 feed 中的新条目
...
<content type="html"><div xmlns="http://www.w3.org/1999/xhtml">
<p><i>The Blogapps server supports the upcoming Atom spec,
so if you install it... </i></p>
</div></content>
<app:control>
<app:draft>no</app:draft>
</app:control>
</entry>
<entry>
<title>Pizza, pizza everywhere...</title>
<link rel="alternate"
href="http://localhost:8080/roller/main/entry/pizza_pizza_everywhere" />
<link rel="edit"
href="http://localhost:8080/roller/app/main/entry/46921b221167d69001116859d7ea0097" />
<category term="/General" />
<author>
<name>admin</name>
<email>admin@example.com</email>
</author>
<id>http://localhost:8080/roller/main/entry/pizza_pizza_everywhere
</id>
<updated>2007-03-19T03:59:50Z</updated>
<published>2007-03-19T03:59:50Z</published>
<content type="html">... and not a thing to drink. What kind of
place is this, anyway?</content>
<app:control>
<app:draft>no</app:draft>
</app:control>
</entry>
<entry>
<title>This is an entry</title>
<link rel="alternate" href=
"http://localhost:8080/roller/main/entry/this_is_an_entry2" />
<link rel="edit" href=
"http://localhost:8080/roller/app/main/entry/402881821161c1fb011162b0ea
3400fd" />
<category term="/General" />
<author>
...
|
接下来看看如何对媒体资源进行操作。
创建媒体资源
创建媒体资源与创建条目的过程基本相同,不同之处就是文件本身成为了 POST 请求的内容(如清单 8 所示)。
清单 8. 创建媒体资源
import java.io.FileInputStream;
import java.util.Date;
import org.apache.abdera.Abdera;
import org.apache.abdera.factory.Factory;
import org.apache.abdera.model.Document;
import org.apache.abdera.model.Entry;
import org.apache.abdera.protocol.client.Client;
import org.apache.abdera.protocol.client.CommonsClient;
import org.apache.abdera.protocol.client.RequestOptions;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
public class CreateResource {
public static void main(String[] args) throws Exception {
Abdera abdera = new Abdera();
Factory factory = abdera.getFactory();
Client client = new CommonsClient(abdera);
FileInputStream inputStream = new FileInputStream(
"c:\\ebay\\robin\\robin4.jpg");
InputStreamRequestEntity file =
new InputStreamRequestEntity(inputStream,
"image/jpg");
RequestOptions options = client.getDefaultRequestOptions();
options.setHeader("Title", "Robin4");
UsernamePasswordCredentials credentials =
new UsernamePasswordCredentials("admin", "admin");
client.addCredentials(
"http://localhost:8080/roller/app/main/resources",
null, null, credentials);
Document<Entry> docOut = client.post(
"http://localhost:8080/roller/app/main/resources",
file).getDocument();
docOut.writeTo(System.out);
}
}
|
在本例中,我们使用计算机上的一个物理文件作为参数创建了一个 FileInputStream 对象,而不是创建一个 Entry 对象。为它指定了一个 mime 类型之后,就可以开始添加内容了。不过首先,我们可以为它指定一个标题,它可以通过在请求的选项中发送头部来实现。(请注意,对标题的重视程度是完全由服务器决定的。比方说,博客应用程序会把所以的标题都转换成文件名。)
终于,可以开始向系统中添加文件了。清注意,只需要替换请求中的条目,添加文件作为 POST 请求的主体即可。
返回的文档看上去可能有点眼熟(如清单 9 所示)。
清单 9. 媒体描述条目
<?xml version='1.0' encoding='utf-8'?>
<entry xmlns="http://www.w3.org/2005/Atom">
<title type="text">main-2007031900765.jpg</title>
<link rel="edit" href=
"http://localhost:8080/roller/app/main/resource/main-
2007031900765.jpg.media-link" />
<link rel="edit-media" href=
"http://localhost:8080/roller/app/main/resource/main-2007031900765.jpg"
/>
<id>http://localhost:8080/roller/app/main/resource/main-
2007031900765.jpg</id>
<updated>1970-01-01T00:00:00Z</updated>
<content type="image/jpeg" src=
"http://localhost:8080/roller/resources/main/main-2007031900765.jpg" />
<atom-draft xmlns="http://rollerweblogger.org/namespaces/app">9</atom-draft>
</entry>
|
在本例中,条目显示了如何对图像进行访问(http://localhost:8080/roller/resources/main/main-2007031900765.jpg)。如何在浏览器地址栏中输入这个 URL,那么应该可以看到图像,如图 2 所示。清注意,代码中还包含有两个编辑链接,而不是一个。编辑链接允许我们编辑 XML 条目,而 edit-media 链接允许我们编辑真正的媒体文件。
图 2. 上传的图像
我们还可以在 http://localhost:8080/roller/app/main/resources 链接中看到资源集的图像条目(如清单 10 所示)。
清单 10. 新 feed
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" ...>
<title>main</title>
<link rel="alternate" type="text/html"
href="http://localhost:8080/roller/main" />
<id>http://localhost:8080/roller/app/main/entries/0</id>
<updated>2007-03-19T04:46:41Z</updated>
<entry>
<title>main-2007031900765.jpg</title>
<link rel="edit" href=
"http://localhost:8080/roller/app/main/resource/main-
2007031900765.jpg.media-link" />
<link rel="edit-media" href=
"http://localhost:8080/roller/app/main/resource/main-2007031900765.jpg"
/>
<id>http://localhost:8080/roller/app/main/resource/main-
2007031900765.jpg</id>
<updated>2007-03-19T04:31:36Z</updated>
<content type="image/jpeg" src=
"http://localhost:8080/roller/resources/main/main-2007031900765.jpg" />
</entry>
<entry>
<title>robin3.jpg</title>
<link rel="edit" href=
"http://localhost:8080/roller/app/main/resource/robin3.jpg.media-link"
/>
<link rel="edit-media" href=
"http://localhost:8080/roller/app/main/resource/robin3.jpg" />
<id>http://localhost:8080/roller/app/main/resource/robin3.jpg</id>
<updated>2007-03-19T02:20:30Z</updated>
<content type="image/jpeg" src=
"http://localhost:8080/roller/resources/main/robin3.jpg" />
</entry>
<entry>
<title>robin2.jpg</title>
...
</entry>
</feed>
|
这个 feed 是 servlet 的基础。
显示 feed 中的项
第一步,创建一个新的 servlet。要显示集中的项目列表,就要解析表示该集的 feed(如清单 11 所示)。
清单 11. 解析 feed
import java.io.FileInputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.util.List;
import org.apache.abdera.*;
import org.apache.abdera.model.*;
import org.apache.abdera.protocol.client.*;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
public class FeedList extends javax.servlet.http.HttpServlet
implements javax.servlet.Servlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
java.io.PrintWriter out = response.getWriter();
out.println(
"<html><head><title>Media List</title></head><body>");
try{
Abdera abdera = new Abdera();
Client client = new CommonsClient(abdera);
String uri =
"http://localhost:8080/roller/app/main/resources";
UsernamePasswordCredentials credentials =
new UsernamePasswordCredentials("admin", "admin");
client.addCredentials(uri, null, null, credentials);
Document<Feed> doc = client.execute("get", uri, null,
null).getDocument();
Feed feed = doc.getRoot();
List<Entry> entries = feed.getEntries();
int numEntries = entries.size();
for (int i = 0; i < numEntries; i++){
Entry thisEntry = entries.get(i);
out.println(thisEntry.getTitle());
out.println("<br />");
out.print("<img height='120' src='");
out.println(
thisEntry.getContentElement().getSrc().toString());
out.println("' /><br />");
out.println("<a href='FeedList?action=delete&uri="+
thisEntry.getEditLinkResolvedHref()+"'>Delete</a>");
out.println("<hr />");
}
out.println("<form action='FeedList' method='post'>");
out.println(
"URL for media: <input type='text' name='newMedia' />");
out.println(
"Title for media: <input type='text' name='title' />");
out.println(
"<br /><input type='submit' value='Add New Asset' />");
out.println("</form>");
} catch (Exception e){
e.printStackTrace(response.getWriter());
}
out.println("</body></html>");
}
}
|
在 servlet 的 doGet() 方法(该方法用于触发在浏览器向 servlet 提交的一个 GET 请求)中,像前面一样创建 Abdera 和客户机对象,添加凭证等等。而在本例中,我们将向服务器发送一个 GET 请求,用于检索对应于该集的 Feed 文档。
获取这个文档之后,便可以通过使用
getRoot() 方法获得真正的 Feed 对象。要从 feed 中检索真正的条目,可以使用 getEntries() 方法检索各个 List<Entry>。完成这步之后,便可以逐个遍历它的 Entry 对象,并为每个 Entry 对象显示相应的 HTML。这个 HTML 包含一个 img 标志,这个 img 标记以最小高度 120 象素显示真正的图像和一个删除该资源的链接。(后者使用 getEditLinkResolvedHref() 方法检索在内部与条目对应的实际的 URL。)
需要注意页面底部的表单。那个表单可以让用户输入要添加的内容。因为该表单指定了 POST 方法,所以我们将把那个函数添加到 doPost() 方法中(如清单 12 所示)。
清单 12. doPost() 方法
...
out.println("</body></html>");
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
java.io.PrintWriter out = response.getWriter();
out.println(
"<html><head><title>Media List</title></head><body>");
try {
Abdera abdera = new Abdera();
Client client = new CommonsClient(abdera);
UsernamePasswordCredentials credentials =
new UsernamePasswordCredentials("admin", "admin");
client.addCredentials(
"http://localhost:8080/roller/app/main/resources",
null, null, credentials);
String uri =
"http://localhost:8080/roller/app/main/resources";
Document<Entry> docOut = null;
FileInputStream inputStream = new FileInputStream(
request.getParameter("newMedia").toString());
InputStreamRequestEntity file =
new InputStreamRequestEntity(inputStream,
"image/jpg");
RequestOptions options = client.getDefaultRequestOptions();
options.setHeader("Title", request.getParameter("title"));
docOut = client.post(uri, file, options).getDocument();
doGet(request, response);
} catch (Exception e){
e.printStackTrace();
}
out.println("</body></html>");
}
}
|
此处我们添加了图像,其操作和在 standalone 中添加图像一样。不同之处就是需要从 servlet 请求中检索参数,以决定待添加文件的位置和标题。
清注意,我们还可加以设置,使用户能够上传文件到服务器中,不过因为这一操作涉及到提取上载到 Java servlet 中的文件的复杂操作,所以我把这个操作留给读者做为练习来完成。
应该可以在 feed 中看到新的条目,如清单 13. 所示。
清单 13. 新 feed
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" ... >
<title>main</title>
<link rel="alternate" type="text/html"
href="http://localhost:8080/roller/main" />
<id>http://localhost:8080/roller/app/main/entries/0</id>
<updated>2007-03-19T04:46:41Z</updated>
<entry>
<title>main-2007031900125.jpg</title>
<link rel="edit" href=
"http://localhost:8080/roller/app/main/resource/main-
2007031900125.jpg.media-link" />
<link rel="edit-media" href=
"http://localhost:8080/roller/app/main/resource/main-2007031900125.jpg"
/>
<id>http://localhost:8080/roller/app/main/resource/main-
2007031900125.jpg</id>
<updated>2007-03-19T04:46:41Z</updated>
<content type="image/jpeg" src=
"http://localhost:8080/roller/resources/main/main-2007031900125.jpg" />
</entry>
<entry>
<title>main-200703190078.jpg</title>
<link rel="edit" href=
"http://localhost:8080/roller/app/main/resource/main-
200703190078.jpg.media-link" />
...
|
我们还可以在当前的 servlet 页面中看到新的图像。
删除资源
删除某个的条目非常简单。记住,我们为每个资源都创建了一个链接。这个链接使浏览器中的请求有两个参数:action 参数和资源的 edit-URI。可以使用这个信息来删除资源(如清单 14 所示)。
清单 14. 删除资源
...
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
java.io.PrintWriter out = response.getWriter();
out.println(
"<html><head><title>Media List</title></head><body>");
String action = null;
if (request.getParameter("action") == null){
action = "list";
} else {
action = request.getParameter("action").toString();
}
try{
Abdera abdera = new Abdera();
Client client = new CommonsClient(abdera);
String uri =
"http://localhost:8080/roller/app/main/resources";
UsernamePasswordCredentials credentials =
new UsernamePasswordCredentials("admin", "admin");
client.addCredentials(uri, null, null, credentials);
if (action.equals("list")){
...
} else if (action.equals("delete")){
client.delete(request.getParameter("uri").toString());
out.println("The item has been deleted. "+
"<a href='FeedList'>Back to the list</a>");
}
} catch (Exception e){
...
|
这就全部完成了!该项已经不在集中了,因此也不在 feed 中了。
着眼于未来
您可以想知道,完成这些后该如何编辑已存在的内容项。当前版本的博客应用程序并不支持编辑媒体资源(通常的配置也是如此),不过其过程类似于清单 15,其中显示了如何编辑条目。
清单 15. 编辑条目
...
String editUri = "http://localhost:8080/roller/app/main/entry/40288182115c1af501115c2e50
a80033";
Document<Entry> editDoc = client.get(editUri).getDocument();
Entry thisEntry = editDoc.getRoot();
thisEntry.setTitle("This is a new Title, one that's been edited");
client.put(editUri, thisEntry);
...
|
要编辑某个条目,首先必须使用 edit URI(edit URI 在内部表示数据)从数据库中检索这个条目。要实现这个目标,可以使用客户机(当然是经过正确配置的)执行向那个 URL 发出的 get() 请求。这个方法会返回一个 Response 对象,我们可以从中检索 Document。有了 Document 之后,便可以使用 getRoot() 方法检索 Entry 对象了。
获得 Entry 对象之后,就可以和前面一样对它进行处理了。然而,要替代某个已存在的条目,必须使用 PUT 方法而不是使用 POST。使用 PUT 方法,可以指定 URL 和数据,而且服务器能够替代已存在的信息而不是创建一个新的条目。
记住,每个媒体资源都有一个 media-link 条目,编辑媒体信息需要使用到 media-link 条目。要替换当前的资源,只需使用文件替换编辑过的条目,就和刚开始添加资源的步骤一样(如清单 16 所示)。
清单 16. 替换资源
...
String editUri = "http://localhost:8080/roller/app/main/entry/40288182115c1af501115c2e50
a80033";
FileInputStream inputStream = new FileInputStream(
"c:\\ebay\\redbowl\\redbowl1.jpg");
InputStreamRequestEntity thisFile =
new InputStreamRequestEntity(inputStream,
"image/jpg");
client.put(editUri, thisFile);
...
|
在本例中,并不需要检索 Entry,因为我们要替换的只是资源。
结束语
Atom 联合格式(Atom Syndication Format)非常适于在显示集中的信息细节。使用它与功能强大的Atom 发布协议( Atom Publishing Protocol )相结合,我们可以以多种方式管理资源、图像、音频或者甚至文本和 xhtml 文件。这篇文章向大家介绍了如何使用 Apache Abdera,轻松利用这些功能来创建一个基于 Web 的媒体资源管理程序。
下载 | 描述 | 名字 | 大小 | 下载方法 |
|---|
| 示例代码 | x-atommedia.source.zip | 3KB | HTTP |
|---|
参考资料 学习
获得产品和技术
讨论
关于作者  | |  | Nicholas Chase 曾经参与多家公司的网站开发,包括 Lucent Technologies、Sun Microsystems、Oracle 和 Tampa Bay Buccaneers。Nick 曾经做过高中物理教师、低放射性废弃设备管理员、在线科幻杂志的编辑、多媒体工程师、Oracle 教员以及一家交互通信公司的首席技术官。他出版了多部著作,包括 XML Primer Plus(Sam's)。 |
对本文的评价
|