簡體   English   中英

下載的jar文件已損壞

[英]Downloaded jar file corrupted

我有這段代碼,可以從特定的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();
        }
    }
}

任何想法這段代碼有什么問題嗎?

不知道這是否可以解決您的問題,但是您應該最后清除緩沖區。

outStream.flush();

您的代碼看起來很正確; 嘗試這個

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();
         }
      }
   }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM