java 远程发送文件内容_JAVA远程传输文件代码片段

packagecom.edsion.down;importjava.io.BufferedInputStream;importjava.io.DataOutputStream;importjava.io.File;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.RandomAccessFile;importjava.net.*;/*** 文件传送客户端:获取远程文件*/publicclassdownload

{publicvoiddownload()

{

}privatebooleanFileExist(String pathAndFile)//确定文件是否已经下载,但没有下载完成{

File file=newFile(pathAndFile);if(file.exists())returntrue;elsereturnfalse;

}privatelongFileSize(String pathAndFile)//确定已经下载了的文件大小{

File file=newFile(pathAndFile);returnfile.length();

}privatevoidFileRename(String fName,String nName)//将下载完全的文件更名,去掉.tp名{

File file=newFile(fName);

file.renameTo(newFile(nName));

file.delete();

}publicstaticvoidmain(String[] args)

{

URL url=null;

HttpURLConnection urlc=null;

DataOutputStream dos=null;

BufferedInputStream bis=null;

FileOutputStream fos=null;///------------------------------String localFile="E:\\123\\Storm2012-3.10.02.05.exe";//文件保存的地方及文件名,具体情况可以改//------------------------------String localFile_bak=localFile+".tp";//未下载完文件加.tp扩展名,以便于区别download gco=newdownload();longfileSize=0;longstart=System.currentTimeMillis();intlen=0;byte[] bt=newbyte[1024];//byte[] buffer=new byte[50*1024];RandomAccessFile raFile=null;longTotalSize=0;//要下载的文件总大小try{//url = new URL("http://www.netbox.cn/download/nbsetup.EXE");//---------------------------url=newURL("http://192.168.1.186:8080/down/new/Storm2012-3.10.02.05.exe");//--------------------------------urlc=(HttpURLConnection) url.openConnection();

TotalSize=Long.parseLong(urlc.getHeaderField("Content-Length"));

System.out.println("下载文件大小为:"+TotalSize);

urlc.disconnect();//先断开,下面再连接,否则下面会报已经连接的错误urlc=(HttpURLConnection) url.openConnection();//确定文件是否存在if(gco.FileExist(localFile_bak))//采用断点续传,这里的依据是看下载文件是否在本地有.tp有扩展名同名文件{

System.out.println("文件续传中...");

fileSize=gco.FileSize(localFile_bak);//取得文件在小,以便确定随机写入的位置System.out.println("fileSize:"+fileSize);//设置User-Agent//urlc.setRequestProperty("User-Agent","NetFox");//设置断点续传的开始位置urlc.setRequestProperty("RANGE","bytes="+fileSize+"-");//urlc.setRequestProperty("RANGE", "bytes="+fileSize);//这样写不行,不能少了这个"-".//设置接受信息urlc.setRequestProperty("Accept","image/gif,image/x-xbitmap,application/msword,*/*");

raFile=newRandomAccessFile(localFile_bak,"rw");//随机方位读取raFile.seek(fileSize);//定位指针到fileSize位置bis=newBufferedInputStream(urlc.getInputStream());while((len=bis.read(bt))>0)//循环获取文件{

raFile.write(bt,0, len);//buffer=buffer+bt;//System.}

System.out.println("文件续传接收完毕!");

}else//采用原始下载{

fos=newFileOutputStream(localFile_bak);//没有下载完毕就将文件的扩展名命名.bakdos=newDataOutputStream(fos);

bis=newBufferedInputStream(urlc.getInputStream());

System.out.println("正在接收文件...");inttest=0;while((len=bis.read(bt))>0)//循环获取文件{

dos.write(bt,0, len);

test++;if(test==50)//这里是测试,你可以删除这里,就可以正常下载了break;

}//System.out.println("文件正常接收完毕!");}

System.out.println("共用时:"+(System.currentTimeMillis()-start)/1000);if(bis!=null)

bis.close();if(dos!=null)

dos.close();if(fos!=null)

fos.close();if(raFile!=null)

raFile.close();

System.out.println("localFile_bak:"+gco.FileSize(localFile_bak));if(gco.FileSize(localFile_bak)==TotalSize)//下载完毕后,将文件重命名{

gco.FileRename(localFile_bak,localFile);

}

System.exit(0);

}catch(Exception e)

{try{if(bis!=null)

bis.close();if(dos!=null)

dos.close();if(fos!=null)

fos.close();if(raFile!=null)

raFile.close();

}catch(IOException f)

{

f.printStackTrace();

}

e.printStackTrace();

}

System.exit(0);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值