【问题标题】:Downloaded jar file corrupted下载的jar文件损坏
【发布时间】:2013-06-30 21:51:06
【问题描述】:

我有这段代码,它从特定 URL 下载 .jar 文件并将其放入特定文件夹。下载的 jar 文件是游戏的 mod,这意味着它必须被下载并正确运行而不会损坏。

问题是,每次我尝试下载文件时,它都会以某种方式损坏并在加载时导致错误。

这是我的下载代码:

final static int size=1024;

public static void downloadFile(String fAddress, String localFileName, String destinationDir, String modID) {
    OutputStream outStream = null;
    URLConnection  uCon = null;

    InputStream is = null;
    try {
        URL Url;
        byte[] buf;
        int ByteRead,ByteWritten=0;
        Url= new URL(fAddress);
        outStream = new BufferedOutputStream(new
                FileOutputStream(destinationDir+"/"+localFileName));

        uCon = Url.openConnection();
        is = uCon.getInputStream();
        buf = new byte[size];
        while ((ByteRead = is.read(buf)) != -1) {
            outStream.write(buf, 0, ByteRead);
            ByteWritten += ByteRead;
        }
        System.out.println("Downloaded Successfully.");
        System.out.println("File name:\""+localFileName+ "\"\nNo ofbytes :" + ByteWritten);
        System.out.println("Writing info file");
        WriteInfo.createInfoFile(localFileName, modID);
    }catch (Exception e) {
        e.printStackTrace();
    }
    finally {
        try {
            is.close();
            outStream.close();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
}

你知道这段代码有什么问题吗?

【问题讨论】:

  • 附注:编码约定规定变量名应以小写字母开头。
  • 这段代码没有明显的问题。下载的文件以什么方式损坏? (长度错误,字节被替换,..)你能确定不是服务器破坏了文件吗?
  • 我知道该文件在服务器上很好,因为我尝试使用我的网络浏览器手动下载并安装它。当我尝试时,它工作得很好

标签: java url download corrupt


【解决方案1】:

不确定这是否能解决您的问题,但您应该在最后刷新缓冲区。

outStream.flush();

【讨论】:

    【解决方案2】:

    您的代码看起来很正确;试试这个

    public static void downloadFromUrl(String srcAddress, String userAgent, String destDir, String destFileName, boolean overwrite) throws Exception
       {
          InputStream is = null;
          FileOutputStream fos = null;
    
          try
          {
             File destFile = new File(destDir, destFileName);
             if(overwrite && destFile.exists())
             {
                boolean deleted = destFile.delete();
                if (!deleted)
                {
                   throw new Exception(String.format("d'ho, an immortal file %s", destFile.getAbsolutePath()));
                }
             }
    
             URL url = new URL(srcAddress);
             URLConnection urlConnection = url.openConnection();
    
             if(userAgent != null)
             {
                urlConnection.setRequestProperty("User-Agent", userAgent);
             }
    
             is = urlConnection.getInputStream();
             fos = new FileOutputStream(destFile);
    
             byte[] buffer = new byte[4096];
    
             int len, totBytes = 0;
             while((len = is.read(buffer)) > 0)
             {
                totBytes += len;
                fos.write(buffer, 0, len);
             }
    
             System.out.println("Downloaded successfully");
             System.out.println(String.format("File name: %s - No of bytes: %,d", destFile.getAbsolutePath(), totBytes));
          }
          finally
          {
             try
             {
                if(is != null) is.close();
             }
             finally
             {
                if(fos != null) fos.close();
             }
          }
       }
    

    【讨论】:

    • 另外,我试过这个,但效果不太好。下载的文件和所有内容,但是当我尝试加载它时它似乎已损坏。我得到了java.lang.NoSuchFieldError: field_77777_bU,这不是下载文件的代码问题,因为我可以通过我的网络浏览器很好地下载它并且不会出现任何错误
    猜你喜欢
    • 2014-01-02
    • 2016-11-06
    • 2019-04-17
    • 2015-07-14
    • 2015-07-16
    • 2017-12-04
    • 2019-01-28
    • 1970-01-01
    相关资源
    最近更新 更多