明妍宁 2019-07-08 16:14 采纳率: 62.5%
浏览 353
已采纳

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

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条回答 默认 最新

  • 毕小宝 博客专家认证 2019-07-09 10:27
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 鼠标右键,撤销删除 复制 移动,要怎样删除
  • ¥15 使用MATLAB进行余弦相似度计算加速
  • ¥15 服务器安装php5.6版本
  • ¥15 我想用51单片机和数码管做一个从0开始的计数表 我写了一串代码 但是放到单片机里面数码管只闪烁一下然后熄灭
  • ¥20 系统工程中,状态空间模型中状态方程的应用。请猛男来完整讲一下下面所有问题
  • ¥15 我想在WPF的Model Code中获取ViewModel Code中的一个参数
  • ¥15 arcgis处理土地利用道路 建筑 林地分类
  • ¥20 使用visual studio 工具用C++语音,调用openslsx库读取excel文件的sheet问题
  • ¥100 寻会做云闪付tn转h5支付链接的技术
  • ¥15 DockerSwarm跨节点无法访问问题