首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

多线程上载exe,文件无法执行,多谢各位大神解答

2013-03-01 
多线程下载exe,文件无法执行,谢谢各位大神解答自己编写了一个多线程的代码,从localhost上面下载一个exe文

多线程下载exe,文件无法执行,谢谢各位大神解答
自己编写了一个多线程的代码,从localhost上面下载一个exe文件,下载完成之后,对比文件的总长度是没有问题的,但是exe文件无法执行,请大神帮忙看看

public class MulThreadDownLoadTest {

private File file ;

public static void main(String args []) throws Exception{

String path = "http://localhost:8080/test/chrome.exe";

 new MulThreadDownLoadTest().download(path,3);

}
public void download(String path, int threadCount ) throws Exception{
URL url = new URL(path);
HttpURLConnection  con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setConnectTimeout(500);
if(con.getResponseCode() == 200){
String fileName = getFileName(path); //获取文件名

file = new File(fileName); 
 int ContentLength = con.getContentLength();//获取响应的长度

 System.out.println("下载文件大小"+ContentLength);
RandomAccessFile accessFile = new RandomAccessFile(file,"rwd");
accessFile.setLength(ContentLength);

//计算每个线程的下载长度为多少
 int tasksize = (ContentLength%threadCount == 0 ? ContentLength/threadCount :  ContentLength/threadCount+ 1);

 accessFile.close();
 for(int threadid = 0; threadid < threadCount ;threadid++ ){
new MulThreadDownLoad(path,file,tasksize,threadid).start();
}
}
}


public static   String getFileName(String path){

return  path.substring(path.lastIndexOf("/")+1);
}
}



public class MulThreadDownLoad extends Thread {

private String path;
private File file;
private  int tasksize;
private int threadid;

public MulThreadDownLoad(String path ,File file , int tasksize, int threadid){
this.path = path;   //文件路径
this.file = file;//文件
this.tasksize = tasksize;//线程的下载量
this.threadid = threadid;//线程的id
}
@Override
public void run() {
try {


int startPoint = threadid * tasksize;     //每条线程写入文件的起点
int endPoint = (threadid + 1 ) * tasksize -1 ; //每条线程写入文件的终点

RandomAccessFile accessFile = new RandomAccessFile(file,"rwd");
accessFile.seek(startPoint);

HttpURLConnection con = (HttpURLConnection)  new URL(path).openConnection();
con.setConnectTimeout(500);
con.setRequestMethod("GET");
con.setRequestProperty("Range", "bytes = "+startPoint+"-"+endPoint);

if(con.getResponseCode() == 206){


InputStream ins = con.getInputStream();
byte [] buffer = new byte [1024];
int length = 0;
while((length = ins.read(buffer)) != -1 ){  //将读取的字节写入文件中
accessFile.write(buffer, 0, length);
}

accessFile.close();
ins.close();
}

System.out.print((threadid +1 )+"线程已经下载完成 :");


System.out.println("bytes = "+startPoint+"-"+endPoint);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}




多线程上载exe,文件无法执行,多谢各位大神解答

谢谢各位大神帮忙   多线程 java?
[解决办法]
没有看明白一个完整的文件干嘛要多线程传,我第一部分没有传完,第二部分已经传完了,是不是第一部分要家长第二部分后面?完整的序列应该是123 ,结果可能是321或者213,大小是一样,可惜不是原来的你了
[解决办法]
多个线程同时以读写方式打开一个文件肯定是无法打开的。多线程这样下载恐怕不一定行,我觉得可以这样设计,定义一个缓冲区,然后多个线程下载,一个线程写入文件,实现一个类似于生产者消费者的程序,这样应该可以。当然这个缓冲区有点复杂,用字节数组是不行的,不仅要包含下载的内容,还要包含这段内容在文件中的位置。
[解决办法]
你确定你服务端支持http range么?

可以在if(con.getResponseCode() == 206)加个else做测试
另外你的accessFile.setLength(ContentLength);可以去掉试试,因为你seek时是可以超出length的,这样可以看看你的下载后的文件是不是真的和源文件一样大

最后你计算每个chunk大小的算法应该改一改,对于最后一个chunk之外的可以这样算,但是最后一个一定要到最后
[解决办法]
引用:
自己编写了一个多线程的代码,从localhost上面下载一个exe文件,下载完成之后,对比文件的总长度是没有问题的,但是exe文件无法执行,请大神帮忙看看

Java code?123456789101112131415161718192021222324252627282930313233343536373839404142public class MulThrea……

应该是每个下载长度的问题吧,最后一个线程所要下载的长度应该是小于或者等于你代码里的tasksize值


// 计算每个线程需要下载多少byte的文件
long perSize = size % threadCount == 0 ? size / threadCount : (size / threadCount + 1);

// 初始化若干个下载线程
for(int i = 0; i < threadCount; i++){
     if(i != (threadCount - 1)){
         threads[i] = new JFileDownloadThread(urlPath, destFile, 
                        i * perSize, perSize, notificationThread);
            }else{
                threads[i] = new JFileDownloadThread(urlPath, destFile, 
                        i * perSize, size - (threadCount - 1) * perSize, notificationThread);
            }
           threads[i].setPriority(8);
//            threads[i].start();
        }



给你看一个我写的完整示例:
http://www.cnblogs.com/tiantianbyconan/archive/2013/02/20/2919132.html
[解决办法]
引用:
没有看明白一个完整的文件干嘛要多线程传,我第一部分没有传完,第二部分已经传完了,是不是第一部分要家长第二部分后面?完整的序列应该是123 ,结果可能是321或者213,大小是一样,可惜不是原来的你了

可以用RandomAccessFile来解决这个问题,可以把InputStream里的指定的部分写到本地保存文件的相应位置,不会出现序列不一致的情况

raf = new RandomAccessFile(destFile, "rw");


raf.setLength(conn.getContentLength()); // 设置本地保存文件的大小
raf.setLength(conn.getInputStream().available());
// 设置获得的InputStream对象的读入位置
is.skip(startPos);
// 设置本地保存的文件开始写入位置
raf.seek(startPos);

热点排行