【问题标题】:download errors, can't open files下载错误,打不开文件
【发布时间】:2015-06-19 08:09:14
【问题描述】:

我的应用必须连接到 Google Drive。连接工作正常。 我可以看到驱动器中的所有文件。文件的下载工作正常。 不幸的是,当我尝试打开它时,文件已损坏或者我根本无法打开它们。有谁知道这个问题的解决方案吗??

enter code here

URL url = new URL(fileURL);

HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
int responseCode = httpConn.getResponseCode();

// always check HTTP response code first
if (responseCode == HttpURLConnection.HTTP_OK) {
    String fileName = "";
    String disposition = httpConn.getHeaderField("Content-Disposition");
    String contentType = httpConn.getContentType();
    int contentLength = httpConn.getContentLength();

    if (disposition != null) {
        // extracts file name from header field
        int index = disposition.indexOf("filename=");
        if (index > 0) {
            fileName = disposition.substring(index + 10,
                    disposition.length() - 1);
        }
    } else {
        // extracts file name from URL
        fileName = fileURL.substring(fileURL.lastIndexOf("/") + 1,
                fileURL.length());
    }
    System.out.println("Content-Type = " + contentType);
    System.out.println("Content-Disposition = " + disposition);
    System.out.println("Content-Length = " + contentLength);
    System.out.println("fileName = " + fileName);
    fileName = mr.getTitle();
    // opens input stream from the HTTP connection
    // URLConnection connection = url.openConnection();
    String saveFilePath = saveDir + File.separator + fileName;

     InputStream inputStream = httpConn.getInputStream();
     FileOutputStream outputStream = new
     FileOutputStream(saveFilePath);
    // opens an output stream to save into file



    int bytesRead = 0;
    // int read;
    byte[] buffer = new byte[16384];
    // while ((bytesRead = inputStream.read(buffer)) > 0) {
    // outputStream.write(buffer, 0, bytesRead);
    // }
    while ((bytesRead = inputStream.read(buffer)) != -1) {
        outputStream.write(buffer, 0, bytesRead);
    }

     outputStream.flush();  
     outputStream.close();
     inputStream.close();

    System.out.println("File downloaded");
} else {
    System.out
            .println("No file to download. Server replied HTTP code: "
                    + responseCode);
}
httpConn.disconnect();

【问题讨论】:

  • 你比较过文件大小吗?
  • 你是如何初始化saveDir的?
  • 对不起,我是初学者,你的意思是?? public static void downloadFile(Metadata mr, String fileURL, String saveDir) 抛出 IOException {

标签: android download google-drive-android-api


【解决方案1】:

这是您的文件长度和字节缓冲区之间的问题。如需快速,请换成重试

byte[] buffer = new byte[1024];

或者你可以得到输入流的长度然后创建缓冲区

long streamLength = inputStream.available();
byte[] buffer = new byte[streamLength];

玩得开心!

【讨论】:

  • 不...不起作用...当我尝试打开文件时我无法打开,文件格式无效。顺便谢谢您
  • 好的,伙计们,我知道真正的问题,如果您想使用连接到您的谷歌驱动器配置文件并下载文件的应用程序,您必须先在驱动器上将文件设置为公开。否则,当您下载时,您无法打开存储的文件,因为有来自 google 的安全块,并且在 log cat 中,您有“android content-disposition null”“android content-lenght -1”“android content-Type text/plain”对于每一种文件....有人知道如何解决它???
  • 你可以试试这个stackoverflow.com/questions/22627428/…,注意你应该选择你之前有权限下载文件的谷歌账户,使用setSelectedAccountName(String accountName)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-18
  • 1970-01-01
相关资源
最近更新 更多