我写了个网络文件下载程序,可是为什么文件下下来以后是损坏的,而且大小每次都不一样.帮我看看哪错了~
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Down extends Frame implements ActionListener
{
Socket clientSocket=null;
PrintStream outStream;
BufferedReader inStream;
String get_uri;
String host,line;
int port,threadcount,thisthreadid;
long filelength,fileblocklength,buflength;
File file;
RandomAccessFile raf;
int k;
byte readbytes[];
BufferedInputStream bin;
long len=0;long i;
Timer timer=new Timer(5000,this);
long temp=0;
Down()
{threadcount=1;
host="sd-http.skycn.net";
port=8180;
get_uri="/down/videocleanerpro_setup.exe";
try{
clientSocket = new Socket(host, port);//打开要下载文件服务器的Socket
outStream = new PrintStream(clientSocket.getOutputStream());
outStream.println("GET "+get_uri+" HTTP/1.1");
outStream.println("Host: "+host+":"+port);
outStream.println("Accept:*/* ");
outStream.println("Referer: ");
outStream.println();
System.out.println("GET "+get_uri+" HTTP/1.1");
System.out.println("Host: "+host+":"+port);
System.out.println("Accept:*/* ");
System.out.println("Referer: ");
System.out.println();
InputStreamReader in=new InputStreamReader(clientSocket.getInputStream());
inStream=new BufferedReader(in);
while ((line=inStream.readLine())!=null)//将服务器响应状态读到line
{ System.out.println("==="+line);
if(line.equals(""))break;
if(line.substring(0,7).equals("HTTP/1.") )//判断是否支持HTTP/1.1
{if(line.charAt(7)=='0')
{
System.out.println("server use http/1.0");
threadcount=1;
}
if(!(line.substring(9,12)).equals("200"))//判断请求是否成功
{ System.out.println("ERROR:"+line); }
} //if 1.
if(line.substring(0,15).equals("Content-Length:") )
{ filelength=Long.parseLong(line.substring(15).trim());
System.out.println("file length:" +filelength);
} //读取文件大小
}
file=new File(get_uri.substring(get_uri.lastIndexOf("/")+1,get_uri.length()));
System.out.println(get_uri.substring(get_uri.lastIndexOf("/")+1,get_uri.length()));
outStream.println("GET "+get_uri+"HTTP/1.1 ");
outStream.println("Host:"+host);
outStream.println("Accept:*/* ");
outStream.println("RANGE:bytes="+(fileblocklength)*thisthreadid+"-");
outStream.println();
raf=new RandomAccessFile(file,"rw");//以随机存取方式打开文件
bin=new BufferedInputStream(clientSocket.getInputStream());
readbytes=new byte[25000];
timer.start();
while((i=bin.read(readbytes,0,25000))!=-1)
{
len+=(long)i;
System.out.println(len+"/"+filelength);
System.out.println;
raf.write(readbytes);}
System.out.println("END");
}catch(Exception e){System.out.println;}
} //Down
public void actionPerformed(ActionEvent e)
{System.out.println("Spead:"+((len-temp)/5));
temp=len;}
public static void main(String args[])
{new Down(); }
}//class
Please use [ code ] tag.