2013-06-30 184 views
1

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

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

这是我的下载代码:

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

任何想法是错误的代码?

+0

附注:编码约定表示变量名应该以小写字母开头。 – abbas

+0

这段代码没有什么明显的错误。下载的文件以何种方式损坏? (长度错误,字节替换,..)你能确定它不是破坏文件的服务器吗? – Joni

+0

我知道这个文件在服务器上很好,因为我试图用我的网页浏览器手动下载并安装它。当我尝试过,它工作正常 – JamEngulfer

回答

0

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

outStream.flush(); 
+0

不,没有解决问题。获取相同的错误 – JamEngulfer

0

你的代码看起来很不错;试一试

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

只是想知道,userAgent字符串应该是什么? – JamEngulfer

+0

此外,我尝试了这一点,它并没有工作。下载的文件和一切,但它似乎已损坏,当我尝试加载它。我得到了'java.lang.NoSuchFieldError:field_77777_bU',这对下载文件的代码没有问题,因为我可以通过我的网络浏览器很好地下载它,并且不会出现任何错误 – JamEngulfer

+0

这里有一些例子:http:// home -1.worldonline.nl/~bmc88/java/sbook/045.html - ua字符串:http://it.wikipedia.org/wiki/User_agent – mrddter