Google Docs API“setMd5C​​hecksum”不工作

发布于 2025-01-07 08:50:43 字数 1017 浏览 1 评论 0原文

最近,我在 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.getMd5C​​hecksum() 方法检索文件并检查 MD5 校验和时,它始终返回 null

我已经尝试了一切,甚至设置了 ETagResourceIDVersionID,但它们都被默认值覆盖(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只是在用心讲痛 2025-01-14 08:50:43

我猜您需要将校验和设置为文件内容的md5哈希值,而不是路径名的哈希值。

为什么他们(谷歌)会关心路径?这根本没有意义。如果我误解了您的代码,请原谅我,但我认为您误解了文件校验和的概念。

不管怎样,你需要做的是吃掉(消化)文件而不是路径:

   import java.security.*;
   import java.util.*;
   import java.math.*;
   import java.io.*;

   public class MD5 {
       private MessageDigest   mDigest;
       private File            openFile;
       private FileInputStream ofis;
       private int             fSize;  
       private byte[]          fBytes;

       public MD5(String filePath) {
           try { mDigest = MessageDigest.getInstance("MD5"); } 
           catch (NoSuchAlgorithmException e) { System.exit(1); }
           openFile = new File(filePath); 
       }   
       public String toString() {
           try {
               ofis    = new FileInputStream(openFile);
               fSize   = ofis.available();
               fBytes  = new byte[fSize];
               ofis.read(fBytes);
           } catch (Throwable t) {
               return "Can't read file or something";
           }  

           mDigest.update(fBytes);
           return new BigInteger(1, mDigest.digest()).toString(16);
       }  
       public static void main(String[] argv){
           MD5 md5 =  new MD5("someFile.ext");
           System.out.println(md5);
       }  
   } 

所以上面代码片段中的错误在这里:

 messageDigest.update(String.valueOf(file.hashCode()).getBytes());

现在,我可以证明我的类给出了文件的正确 md5sum 这很可能就是您所需要的。如果您不相信我,请阅读该方法的 javadoc:
http:// /gdata-java-client.googlecode.com/svn/trunk/java/src/com/google/gdata/data/docs/DocumentListEntry.java
它说的是:
* 设置为文档计算的 MD5 校验和。
...这里没有关于路径校验和的信息:)

$ echo "Two dogs are sleeping on my couch" > someFile.ext    
$ echo "Two dogs are sleeping on my couch" |md5sum 
1d81559b611e0079bf6c16a2c09bd994  -
$ md5sum someFile.ext 
1d81559b611e0079bf6c16a2c09bd994  someFile.ext
$ javac MD5.java && java MD5 
1d81559b611e0079bf6c16a2c09bd994

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:

   import java.security.*;
   import java.util.*;
   import java.math.*;
   import java.io.*;

   public class MD5 {
       private MessageDigest   mDigest;
       private File            openFile;
       private FileInputStream ofis;
       private int             fSize;  
       private byte[]          fBytes;

       public MD5(String filePath) {
           try { mDigest = MessageDigest.getInstance("MD5"); } 
           catch (NoSuchAlgorithmException e) { System.exit(1); }
           openFile = new File(filePath); 
       }   
       public String toString() {
           try {
               ofis    = new FileInputStream(openFile);
               fSize   = ofis.available();
               fBytes  = new byte[fSize];
               ofis.read(fBytes);
           } catch (Throwable t) {
               return "Can't read file or something";
           }  

           mDigest.update(fBytes);
           return new BigInteger(1, mDigest.digest()).toString(16);
       }  
       public static void main(String[] argv){
           MD5 md5 =  new MD5("someFile.ext");
           System.out.println(md5);
       }  
   } 

So the error in your snippet above is here:

 messageDigest.update(String.valueOf(file.hashCode()).getBytes());

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:

$ echo "Two dogs are sleeping on my couch" > someFile.ext    
$ echo "Two dogs are sleeping on my couch" |md5sum 
1d81559b611e0079bf6c16a2c09bd994  -
$ md5sum someFile.ext 
1d81559b611e0079bf6c16a2c09bd994  someFile.ext
$ javac MD5.java && java MD5 
1d81559b611e0079bf6c16a2c09bd994
椵侞 2025-01-14 08:50:43

经过几周的 MD5 校验和问题(以验证文件的内容是否随时间变化)后,我想出了一个不依赖于文件的 MD5 校验和而是依赖于客户端 last 的解决方案-update 文件的属性。

该解决方案适用于所有想要检查文件是否随时间变化的人。然而,任何操作系统上的“更新”都可以被视为打开文件和保存文件的行为,无论是否对文件内容进行任何更改。因此,它并不完美,但确实节省了一些时间和带宽。

解决方案:

long lastModified = new DateTime(
    new Date(file.lastModified()), TimeZone.getDefault()
).getValue();

if(lastModified > entry.getUpdated().getValue()) {
    //update the file
}

其中 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:

long lastModified = new DateTime(
    new Date(file.lastModified()), TimeZone.getDefault()
).getValue();

if(lastModified > entry.getUpdated().getValue()) {
    //update the file
}

Where file is a File instance of the desired file and entry is the DocumentListEntry associated to the local file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文