日期:2014-05-16  浏览次数:20885 次

迅雷不能完全下载文件
公司的设备内置小型嵌入式服务器,里面用的是cgic编写的脚本,其中有一个叫downfile,专门负责下载文件,但是当下载文件時,选择本地保存,文件下载无误;但是当打开迅雷,选择下载被迅雷捕捉后下载,这时有的文件可以下载99.9%然后就停止了说是没有资源,有的则是一直都是0%,停滞不前。。。。。。。。。我先在需要解决这根问题但是不知道从何入手,一片茫然。。。。。。还望哪位大神赐教,下面是downfile 源码:


int cgiMain()
{
int nRec = 0;
char pUserName[BUFLENGTH] = {NULL};
char pVideoName[BUFLENGTH] = {NULL};
char pTemp[BUFLENGTH] = {NULL};
char pPathName[BUFLENGTH] = {NULL};

JYCGISession *pCGISession = new JYCGISession();
nRec = pCGISession->CheckSession(pUserName);
JYCTPLFile *pCTPLFile = new JYCTPLFile();

if(nRec > 0 && NULL != pUserName)
  {
if( strlen(cgiQueryString) > 0)
{
#ifdef _ARM_
strcpy(pPathName,"/video/HardDisk/recordfiles/VideoRecord/");
//取得html页面中file元素的值,应该是文件在客户机上的路径名
cgiFormString("FileName",pTemp,BUFLENGTH);
strcat(pPathName,pTemp);
strcpy( pVideoName, rindex(pTemp ,'/') + 1);
#elif _MIPS_
strcpy(pPathName,"/video/HardDisk/recordfiles/");
//取得html页面中file元素的值,应该是文件在客户机上的路径名
cgiFormString("FileName",pTemp,BUFLENGTH);
strcat(pPathName,pTemp);
strcpy( pVideoName, rindex(pTemp ,'/') + 1);
#else
strcpy(pTemp , cgiQueryString);
if(strstr(pTemp , "="))
{
strcpy(pPathName,"/video/HardDisk/recordfiles/");
strcpy( pVideoName, rindex(pTemp ,'/') + 1);
strcat(pPathName,pVideoName);//拼写完整文件路径
}
#endif
if( NULL != pTemp )
{
FILE *fp;
struct stat64 sb;
stat64(pPathName,&sb);

//下载所需的http头信息
printf("Content-Type:application/binary-stream\n");
printf("Content-Length:%llu\n",sb.st_size);
printf("Content-Disposition:attachment;filename=%s\n\n",pVideoName);

char pathbuff1[256] = {0};
char pathbuff2[256] = {0};
char namebuff[256] = {0};
char *p = strstr(pVideoName,".");
 
strncpy(namebuff, pVideoName, 1);
sprintf(pathbuff1,"/tmp/%s1.log",namebuff);
sprintf(pathbuff2,"/tmp/%s2.log",namebuff);  
 
char systemcmd1[100] = {0};
char systemcmd2[100] = {0};
 
//判断系统中有几个downloadfile.cgi正在运行,并写入临时文件
sprintf(systemcmd1,"ps | grep downloadfile.cgi>%s",pathbuff1);
sprintf(systemcmd2,"wc -l %s>%s",pathbuff1,pathbuff2);
system(systemcmd1);
system(systemcmd2);
 
//从临时文件中读出本进程运行的个数
int fd;
int num = 0;
fd = open(pathbuff2, O_RDWR);
char r_buff[10] = {0};
memset(r_buff, 0, 10);
lseek(fd, 0, SEEK_SET);
sync();
flock(fd, LOCK_SH|LOCK_NB);
read(fd, r_buff, 10);
flock(fd, LOCK_UN);
//num = atoi(r_buff);
int flag = 0;
for(int i=0; i<10 ;i++)
{
if(r_buff[i]>='0'&&r_buff[i]<='9')
{
flag = 1;
num = num*10 + (r_buff[i]-'0');
}
else
{
if(flag)
{
flag = 0;
break;
}
}
}
num = num-2;
unsigned int rs=0;
//打开要下载的文件
if(fp=fopen64(pPathName,"r+b"))
{
char str[50]={0};
char *buf = new char[1024*128];
int timesleep=0;
do
{
rs = fread(buf,1,1024*128,fp);
fwrite(buf,rs,1,stdout);//传递数据到标准输出
//控制下载速