下载文件:适用于Windows,不适用于Linux

时间:2015-10-28 06:11:36

标签: java linux windows download

我正在使用以下代码从互联网上下载文件,更新进度条以显示下载了多少文件,并允许用户取消下载:

final Path tempDirectory = jesterDirectory.resolve( "temp" );
final Path updateFile = tempDirectory.resolve( "update.zip" );

updater.statusArea.append( "Fetch:\t\tDownloading update.zip from " + targetURL + "\n");
URL url = new URL( targetURL );
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
int filesize = connection.getContentLength();
float totalDataRead = 0;
java.io.BufferedInputStream in = new java.io.BufferedInputStream( connection.getInputStream() );
java.io.FileOutputStream fos = new java.io.FileOutputStream( updateFile.toString() );
java.io.BufferedOutputStream bout = new BufferedOutputStream( fos, 1024 );
byte[] data = new byte[1024];
int i = 0;
while ( ( i = in.read ( data, 0, 1024 ) ) >= 0 ) {

    if ( updater.cancelUpdate ) {
        updater.statusArea.append( "Fetch:\t\tCanceled\n");
        unblock();
        bout.close();
        in.close();
        return;
    }
    totalDataRead = totalDataRead+i;
    bout.write( data, 0, i );
    float Percent = ( totalDataRead * 100 )/filesize;
    updater.progressBar.setValue( (int)Percent );
}   
bout.close();
in.close();
updater.statusArea.append( "Fetch:\t\tSuccess!\n"); 

它在Windows上完美运行。在Linux上,它似乎成功下载,但文件已损坏,无法读取。

知道是什么导致了这种差异吗?

0 个答案:

没有答案