多线程下载工具编译中出现错误 新手

import java.io.IOException;

import java.io InputStream;

import java.io RandomAccessFile;

import java.net.HttpURLConnection;

import java.net.MalformedURLException;

import java.net.URL;

```public class DownUtil
{
private String path;

private  String  targetFile;

private  int  threadNum;

private   DownThread[]  threads;

private    int   fileSize;

public  DownUtil(String  path,String  targetFile,int  threadNum)

{

this.path=path;

this.threadNum = threadNum;


threads=new DownThread[threadNum];

this.targetFile = targetFile;

}

public void download() throws Exception
{
URL url = new URL(path);

 HttpURLConnection  conn=(HttpURLConnection)url.openConnection();

 conn.setConnectTimeout(5*1000);

 conn.setRequestMethod("GET");

 conn.setRequestProperty("Accept","image/gif,image/jpeg,image/pjpeg,image/pjpeg,"

+"application/x-shockwave-flash,application/xaml+xml,"
+"application/vnd.ms-xpsdocument,application/x-ms-xbap,"
+"application/x-ms-application,application/vnd.ms-excel,"
+"application/vnd.ms-powerpoint,application/msword,*/*");

 conn.setRequestProperty("Accept-Language","zh-CN");

 conn.setRequestProperty("Charset","UTF-8");

 conn.setRequestProperty("Connection","Keep-Alive");

 fileSize = conn.getContentLength();

 conn.disconnect();

 int currentPartSize=fileSize/threadNum+1;

 RandomAccessFile file = new RandomAccessFile(targetFile,"rw");

 file.setLength(fileSize);

 file.close();

 for(int i=0;i<threadNum;i++)

{
int startPos = i*currentPartSize;

 RandomAccessFile  currentPart=new RandomAccessFile(targetFile,"rw");

 currentPart.seek(startPos);

 threads[i] = new DownThread(startPos,currentPartSize,currentPart);

 threads[i].start();

}

}

public double getCompleteRate()
{
int sumSize=0;

  for(int i=0;i<threadNum;i++)

{
sumSize+=threads[i].Length;

}

  return  sumSize*1.0/fileSize;

}

  private  class  DownThread  extends Thread

{
private int startPos;

  private  int  currentPartSize;

  private  RandomAccessFile  currentPart;

  public int Length;

  public  DownThread(int  startPos,int currentPartSize,RandomAccessFile  currentPart)

{
this.startPos = startPos;

  this.currentPartSize = currentPartSize;

  this.currentPart = currentPart;

}

  public  void  run()

{

try
{
URL url = new URL(path);

       HttpURLConnection  conn = (HttpURLConnection)url.openConnection();

       conn.setConnectionTimeout(5*1000);

       conn.setRequestMethod("GET");

       conn.setRequestProperty("Accept",

       "image/gif,image/jpeg,image/pjpeg,image/pjpeg,"

+"application/x-shockwave-flash,application/xaml+xml,"
+"application/vnd.ms-xpsdocument,application/x-ms-xbap,"
+"application/x-ms-application,application/vnd.ms-excel,"
+"application/vnd.ms-powerpoint,application/msword,*/*");

      conn.setRequestProperty("Accept-Language","zh-CN");

      conn.setRequesProperty("Charset","UTF-8");

      InputStream  instream = conn.getInputStream();

      inStream.skip(this.startPos);

      byte[] buffer = new  byte[1024];

      int  hasRead=0;

      while(Length<currentPartSize&&(hasRead=inStream.read(buffer))!=-1)

{
currentPart.write(buffer,0,hasRead);

      Length+=hasRead;

}
currentPart.close();

      inStream.close();

}

      catch(Exception e)

{

e.printStackTrace();
}
}
}
}

编译时显示:
d:\documents>java -d . DownUtil.java

DownUtil:java:130错误:找不到符号
conn.setConnectionTimeout(5*1000);
符号:方法setConnectionTimeout(int)
位置:类型为HttpURLConnection的变量conn
DownUtil:java:144错误:找不到符号
conn.setRequestProperty("Charser","UTF-8");
符号:方法setRequestProperty(String,String);
位置:类型为HttpURLConnection的变量conn
DownUtil:java:148错误:找不到符号

inStream.skip(this.startPos);
符号:类inStream
位置:类DownUtil.DownThread
DownUtil:java:154错误:找不到符号

while(Length<currentPartSize&&(hasread=inStream.read(buffer))!=-1)
符号:类inStream
位置:类DownUtil.DownThread
DownUtil:java:162错误:找不到符号

inStream.close();
符号:类inStream
位置:类DownUtil.DownThread

5个错误

4个回答

建议学习下使用 Eclipse或 IDEA 等 IDE ,那样方便很多,而且能快速定位编译错误的原因。
纯手工用 java 命令和文本编辑器学习 Java 在熟练后再用感觉就会好多了。

你应该是想要引入第三方工具,但是引入包失败了,找不到你想用的类

要事先导入DownUtil这个类才可以编译

你的main方法对应的类呢?怎么不贴出来,你写的DownUtil这个类在哪里用的?报的这个错是说你DownUtil这个类没找到,检查下main函数调用DownUtil这个类是否import了对应的包,如果是引入的第三方jar包,则看下是否有正确引入

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐