Google Docs API“setMd5Checksum”不工作
最近,我在 Java 使用 Google 文档API v3.0。新条目的创建方式如下:
DocumentListEntry newEntry = new DocumentListEntry();
newEntry.setFile(file, Common.resolveMimeType(file)); //Common is a custom class
newEntry.setFilename(entryTitle.getPlainText()); //entryTitle is a TextConstruct
newEntry.setTitle(entryTitle);
newEntry.setDraft(false);
newEntry.setHidden(file.isHidden());
newEntry.setMd5Checksum(Common.getMD5HexDigest(file));
请相信我,Common.getMD5HexDigest(file)
返回有效且唯一的 MD5 十六进制哈希值。
现在,文件上传正常,但在通过 entry.getMd5Checksum()
方法检索文件并检查 MD5 校验和时,它始终返回 null
。
我已经尝试了一切,甚至设置了 ETag
、ResourceID
和 VersionID
,但它们都被默认值覆盖(null
code> 或服务器生成的字符串)。
Recently I've implemented an application in Java that uses the Google Docs API v3.0. New entries are created like this:
DocumentListEntry newEntry = new DocumentListEntry();
newEntry.setFile(file, Common.resolveMimeType(file)); //Common is a custom class
newEntry.setFilename(entryTitle.getPlainText()); //entryTitle is a TextConstruct
newEntry.setTitle(entryTitle);
newEntry.setDraft(false);
newEntry.setHidden(file.isHidden());
newEntry.setMd5Checksum(Common.getMD5HexDigest(file));
Trust me when I tell you that Common.getMD5HexDigest(file)
returns a valid and unique MD5 Hexadecimal hash.
Now, the file uploads properly yet when retrieving the file and checking the MD5 checksum through the entry.getMd5Checksum()
method, it always returns null
.
I've tried EVERYTHING, even set the ETag
, ResourceID
and VersionID
but they all get override with default values (null
or server generated strings).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我猜您需要将校验和设置为文件内容的md5哈希值,而不是路径名的哈希值。
为什么他们(谷歌)会关心路径?这根本没有意义。如果我误解了您的代码,请原谅我,但我认为您误解了文件校验和的概念。
不管怎样,你需要做的是吃掉(消化)文件而不是路径:
所以上面代码片段中的错误在这里:
现在,我可以证明我的类给出了文件的正确 md5sum 这很可能就是您所需要的。如果您不相信我,请阅读该方法的 javadoc:
http:// /gdata-java-client.googlecode.com/svn/trunk/java/src/com/google/gdata/data/docs/DocumentListEntry.java
它说的是:
* 设置为文档计算的 MD5 校验和。
...这里没有关于路径校验和的信息:)
:
I would guess that you need to set the checksum to the md5 hash of the file's contents, not the hash of the path-name.
Why would they (google) care about the path? It makes no sense at all. Forgive me if I misinterpreted your code, but I think you have misconceived the concept of file checksums.
Anyway, what you need to do is eat (digest) the file and not the path:
So the error in your snippet above is here:
Now, I can show that my class gives the correct md5sum of the file which is most likely what you need. Just read the javadoc of the method if you don't trust me:
http://gdata-java-client.googlecode.com/svn/trunk/java/src/com/google/gdata/data/docs/DocumentListEntry.java
What it says is:
* Set the MD5 checksum calculated for the document.
... nothing about the path's checksum :)
here:
经过几周的 MD5 校验和问题(以验证文件的内容是否随时间变化)后,我想出了一个不依赖于文件的 MD5 校验和而是依赖于客户端
last 的解决方案-update
文件的属性。该解决方案适用于所有想要检查文件是否随时间变化的人。然而,任何操作系统上的“更新”都可以被视为打开文件和保存文件的行为,无论是否对文件内容进行任何更改。因此,它并不完美,但确实节省了一些时间和带宽。
解决方案:
其中
file
是所需文件的File
实例,entry
是与本地文件关联的DocumentListEntry
。After struggling a few weeks with the MD5 checksum problem (to verify if the content of the file changed over time), I came up with a solution that doesn't rely on the MD5 checksum of the file but on the client
last-update
attribute of the file.This solution goes to everyone that wants to check if a file has changed over time. However, "an update" on any operating system can be considered as the act of opening the file and saving the file, with or without making any changes to the content of the file. So, it's not perfect but does save some time and bandwidth.
Solution:
Where
file
is aFile
instance of the desired file andentry
is theDocumentListEntry
associated to the local file.