-4

我在谷歌文档中存储了一个文本文件,这是一个共享的公共文件。我正在尝试通过 url 读取该文件的内容。但它没有显示内容。我在这里附上我的代码。请建议我应该在哪里更改我的代码以读取文件的内容。当我在任何网络浏览器的地址栏中写入内容时,我在 URL 中给出的地址正在显示内容。我实际上不知道在哪里更正代码。我在代码末尾添加了输出

package FileContent;

import java.awt.*; 
import java.net.*; 
import java.io.*; 

public class FileContent extends Frame {
    public static void main(String[] args) {

        BufferedReader br = null;
        TextArea FileText = new TextArea("  Content of the File \'temp1.txt\' :", 11, 24, TextArea.SCROLLBARS_NONE);

        try {
            // here is the url
            URL url = new URL("https://docs.google.com/file/d/0B9MOgXFCss2iSWpicmVKSW9OOWM/edit");

            InputStream is = url.openStream();
            br = new BufferedReader(new InputStreamReader(is));
        } catch (MalformedURLException e) {
            System.out.println("Bad URL");
        } catch (IOException e) {
            System.out.println("IO Error : " + e.getMessage());
        }

        FileText.setBackground(Color.cyan);
        FileText.append(String.valueOf('\n'));

        Frame f = new Frame("File Content");
        f.setSize(200, 200);
        f.add(FileText);
        f.setVisible(true);

        try {
            String s;
            boolean eof = false;
            s = br.readLine();

            while (!eof) {
                FileText.append(s + String.valueOf('\n'));
                try {
                    s = br.readLine();
                    if (s == null) {
                        eof = true;
                        br.close();
                    }
                } catch (EOFException eo) {
                    eof = true;
                } catch (IOException e) {
                    System.out.println("IO Error : " + e.getMessage());
                }
            }

        } catch (IOException e) {
            System.out.println("IO Error : " + e.getMessage());
        }
    }    
}

输出看起来像: Time Temperature ,"docs-ndt":"Untitled Texmex","docs-eit":false,"docs-spfe":true,"docs-mriim":1800000,"docs-ecc":false ,"docs-collaborator-selection-model":false,"docs-csmbocj":false,"ecbsl":true,"ecid":true,"eod":true,"docs-eilb":false,"docs- pedd":true,"docs-evr":false,"docs-enmr":false,"docs-esrd":false,"share_ui":"jfk","server_time_ms":1378944177685,"gaia_session_id":"", "enable_iframed_embed_api":true,"cup":"/folder/d/{folderId}/edit","docs-fut":"//docs.google.com/#folders/{folderId}","esid":真,"esubid":假,"docs-etbs":真,"enable_kennedy":true,"onePickImportDocumentUrl":"","opbu":"https://docs.google.com/picker","opru":"https://docs.google.com/relay.html", "opdu":false,"ophi":"texmex","opuci":"","docs-se":false,"docs-ebcrsct":false,"docs-iror":false,"xdbcmUri":" https://docs.google.com/file/xdbcm.html","xdbcfAllowXpc":true,"docs-corsbc":false,"xdbcfAllowHostNamePrefix":true,"docs-spdy":false,"enable_client_docos":true ,"enable_anchored_docos":true,"enable_docos_tickle":true,"gv_int_native":true,"tpc":true,"enable_pinned_revisions":false,"enable_edit_blob_revisions":false,"upload_url":"https://docs.google.com/upload/resumableupload","enable_toolbar":true,"enable_feedback_button":false,"enable_microscope":true,"enable_manage_timed_text":true,"video_embed_type":"PREFER_FLASH","enable_maps_embed" :true,"maps_api_uri":"https://maps.googleapis.com/maps/api/js?key=AIzaSyBCjpnguVjzi6vS67NdBtyYuvCYz3yBxCY&sensor=false","maps_display_uri":"https://maps.google.com/maps"," docs_abuse_link":"https://docs.google.com/abuse?id=0B9MOgXFCss2iSWpicmVKSW9OOWM","enable_csi":true,"csi_service_name":"texmex","third_party_default_icon_urls":{"icon16":"//ssl.gstatic .com/docs/doclist/images/generic_app_icon_16.png","icon32":"//ssl.gstatic。com/docs/doclist/images/generic_app_icon_32.png","icon64":"//ssl.gstatic.com/docs/doclist/images/generic_app_icon_64.png","icon128":"//ssl.gstatic.com/ docs/doclist/images/generic_app_icon_128.png"},"enable_chrome_webstore_link":true};(function(){(function(){function d(a){this.t={};this.tick=function(a, c,b){b=void 0!=b?b:(new Date).getTime();this.t[a]=[b,c]};this.tick("start",null,a) }var a=new d;window.jstiming={Timer:d,load:a};if(window.performance&&window.performance.timing){var a=window.performance.timing,c=window.jstiming.load,b =a.navigationStart,a=a.responseStart;0=b&&(c.tick("com/docs/doclist/images/generic_app_icon_128.png"},"enable_chrome_webstore_link":true};(function(){(function(){function d(a){this.t={};this.tick=function( a,c,b){b=void 0!=b?b:(new Date).getTime();this.t[a]=[b,c]};this.tick("start",null, a)}var a=new d;window.jstiming={Timer:d,load:a};if(window.performance&&window.performance.timing){var a=window.performance.timing,c=window.jstiming.load ,b=a.navigationStart,a=a.responseStart;0=b&&(c.tick("com/docs/doclist/images/generic_app_icon_128.png"},"enable_chrome_webstore_link":true};(function(){(function(){function d(a){this.t={};this.tick=function( a,c,b){b=void 0!=b?b:(new Date).getTime();this.t[a]=[b,c]};this.tick("start",null, a)}var a=new d;window.jstiming={Timer:d,load:a};if(window.performance&&window.performance.timing){var a=window.performance.timing,c=window.jstiming.load ,b=a.navigationStart,a=a.responseStart;0=b&&(c.tick("计时){var a=window.performance.timing,c=window.jstiming.load,b=a.navigationStart,a=a.responseStart;0=b&&(c.tick("计时){var a=window.performance.timing,c=window.jstiming.load,b=a.navigationStart,a=a.responseStart;0=b&&(c.tick("搜索图像 地图 播放 YouTube 新闻 Gmail 云端硬盘 更多 »举报滥用行为 | 设置 | 登录 temp1.txt 评论 Share您正在使用不受支持的浏览器。某些功能可能无法正常工作。升级到现代浏览器,例如 Google Chrome.Dismiss

FileEditViewHelp DOCS_initializeModules({"core":[],"app":["core"]},{"core":["/static/file/client/js/123932484-edit_core__en_gb.js"],"app": ["/static/file/client/js/1118393582-edit_app__en_gb.js"]}, 'core');_main('/file/d/0B9MOgXFCss2iSWpicmVKSW9OOWM', {'sid': '6249ba6d18211909','id': ' 0B9MOgXFCss2iSWpicmVKSW9OOWM','email':'','title':'temp1.txt','description':'','mimetype':'text/plain','fileExtension':'txt','mediaType':' text', 'revisions': [{"tags":[],"creatorDisplayName":"Kawsar Jahan","pinned":true,"filename":"temp1.txt","downloadUrl":"https://docs.google.com/uc?id=0B9MOgXFCss2iSWpicmVKSW9OOWM&export=download&revid=0B9MOgXFCss2iaWN0a2p2b2x4dW4zcDBVL2lhTGhTajBaS3RnPQ","sizeInBytes":161,"docId":"0B9MOgXFCss2iaWN0a2p2b2x4dW4zcDBVL2lhTGhTajBaS3RnPQ","creationDateString":"10 Sep","creator":{"isMe" :false,"nickname":"Kawsar Jahan","iconUrl":"images/doclist/contact_nopicture.png","editProfileUrl":"editProfile"}}],'obfuscatedUserId':'ANONYMOUS_17612595759507348808','userDomain':' ', 'embedPreviewUri': 'https://docs.google.com/file/d/0B9MOgXFCss2iSWpicmVKSW9OOWM/preview','syncUpdates': [],'contentRenderer': 'gviewembed'},{"description":{"raw ":"","格式化":""},"download":{"isMissingBlobRef":false,"filename":"temp1.txt","url":"https://docs.google.com/uc?id=0B9MOgXFCss2iSWpicmVKSW9OOWM&export=download "},"revision":{"swfUrl":"/static/doclist/client/css/1531528182-uploaderapi.swf","busyIconImageUrl":"https://ssl.gstatic.com/docs/doclist/images/ loading_small.gif"},"sharing":{"is_private":false,"visibility_is_restricted_to_domain":false,"visibility_domain_display_name":""},"basicdetails":{"mimeType":"text/plain","lastModifiedDateString": "10 Sep","creationDateString":"10 Sep","fileSize":"161"},"thumbnail":{"thumbnail_128":"https://lh4.googleusercontent.com/2lZYaNUzET3ObPtUlsycnF6LhTh_q0Q8GAyE8_F9Ic4IE_9o00anRyEAmglCuSwp_Jo2=s128","thumbnail_full":"https://lh4.googleusercontent.com/2lZYaNUzET3ObPtUlsycnF6LhTh_q0Q8GAyE8_F9Ic4IE_9o00anRyEAmglCuSwp_Jo2=s1600"},"gviewembed":{"url":" https://docs.google.com/viewer?srcid=0B9MOgXFCss2iSWpicmVKSW9OOWM&pid=explorer&efh=false&a=v","embeduri":"https://docs.google.com/viewer?srcid=0B9MOgXFCss2iSWpicmVKSW9OOWM&pid=explorer&efh=false&a=v&chrome= false&embedded=true","nonredirectedgviewurl":"https://docs.google.com/viewer?srcid=0B9MOgXFCss2iSWpicmVKSW9OOWM&pid=explorer&efh=false&a=v&chrome=true&redirect=false","isNativeGView":false},"webstoreui":{"mimeType":"text/plain","fileExtension":"txt","moreDriveAppsUrl":"https://chrome.google.com/webstore/category/collection/drive_apps"}});

4

1 回答 1

1

您所采取的仅向 google doc 发出 http 请求的方法可能会导致您被重定向到登录页面或其他内容。如果您能够访问该文档,那么您将不得不提出其 HTML 结构的解析策略,该策略可能会发生变化,并且实际上不会关心事先通知您。

我建议您查看谷歌开发者网站并查看他们可用的不同 api。看起来他们有有用的 REST api,看起来他们也可能有一个纯 Java 客户端。我会查看这个链接:https ://developers.google.com/drive/quickstart-java

于 2013-09-11T21:01:10.920 回答