免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3977 | 回复: 5
打印 上一主题 下一主题

求教:简单的HTTP下载文件程序,但是图片文件无法显示 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-09-04 17:08 |只看该作者 |倒序浏览
下载部分的代码如下:
下载其他类型的文件都没问题,但是下载的图片文件却无法显示。我比较了下载的图片文件和源文件大小是一样的,
却无法显示,请大家帮我看看代码有什么问题?

void ServeFile(S8* repos,Socket_t client_sock,S8* file_path)
{
    char sendbuf[MAX_BUF_SIZE];
    memset(sendbuf,0,sizeof(sendbuf));
       FILE *fp;
    fp=repos_open_file(repos, file_path, "rb");
    //to be finished,send 200 reponse

    sprintf(sendbuf,"HTTP/1.1 200\r\n");
    sprintf(sendbuf,"%sContent-Length:%d\r\n",sendbuf,get_file_size(repos, file_path));
    sprintf(sendbuf,"%sContent-Type:%s\r\n",sendbuf,lookup_mime(file_path));
    sprintf(sendbuf,"%sDate:%s\r\n\r\n",sendbuf,timestamp());
    
    
    send(client_sock,sendbuf,strlen(sendbuf),0);//send header

    int read_size;
       while(!feof(fp))
                      {
                        memset(sendbuf,0,sizeof(sendbuf));
                        read_size=fread(sendbuf,sizeof(char),BUF_SIZE,fp);
               send(client_sock,sendbuf,read_size,0);//send file

                        }
    log_http_response(file_path,200,get_file_size(repos,file_path));
    fclose(fp);
}


[ 本帖最后由 diker007 于 2007-9-4 17:09 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2007-09-04 20:03 |只看该作者
光比较大小是没用的,2进制比对。

论坛徽章:
0
3 [报告]
发表于 2007-09-04 22:00 |只看该作者
问题解决了,sprintf(sendbuf,"%sDate:%s\r\n\r\n",sendbuf,timestamp());
       改为:sprintf(sendbuf,"%sDate:%s\r\n",sendbuf,timestamp());。

用UltraEdit比较了一下,发现下载后的文件开头多了个\r\n.
HTTP协议的头域和正文域有一个空行(\r\n),但是对于下载文件应该没有这空行了。

[ 本帖最后由 diker007 于 2007-9-4 22:01 编辑 ]

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:53:172015亚冠之水原三星
日期:2015-06-02 16:34:202015年亚冠纪念徽章
日期:2015-10-19 18:13:37程序设计版块每日发帖之星
日期:2015-11-08 06:20:00
4 [报告]
发表于 2007-09-04 22:23 |只看该作者

  1. HTTP/1.1 200 OK
  2. Date: Tue, 04 Sep 2007 14:22:22 GMT
  3. Server: Apache/2.0.54 (Unix) PHP/5.2.3
  4. Last-Modified: Wed, 04 Apr 2007 09:53:18 GMT
  5. ETag: "472d23-13b-6c7dff80"
  6. Accept-Ranges: bytes
  7. Content-Length: 315
  8. Keep-Alive: timeout=15, max=97
  9. Connection: Keep-Alive
  10. Content-Type: image/gif

  11. GIF89a
  12. ....        .....c)..............J...........................!..NETSCAPE2.0.....!.....        .,....
  13. .....=0.@k.A....G....... @A})."....:....?S..*.]#..yP.)..sR.Z...e+...!.....        .,................uLB.6...Eh.&X.....!.....        .,...............D...9......!...#.        .,......
  14. ....0....'..L....aA".)..."...E..!...K.        .,.............iL..;

复制代码


我抓的包里面有啊?

用FireFox看看正常么? IE是超强纠错的

[ 本帖最后由 xinglp 于 2007-9-6 10:46 编辑 ]

论坛徽章:
0
5 [报告]
发表于 2007-09-05 18:02 |只看该作者

回复 #5 xinglp 的帖子

我自己也抓了几个包确实都有空行(\r\n)。

只可能是timestamp()返回的字符串尾部有(\r\n),看了下timestamp()的代码,发现返回的字符串尾部有一个\n.


const S8* timestamp()
{
    // CAUTION: String returned by ctime has a trailing newline character
    time_t curr_time = time(NULL);
    S8* curr_time_str = ctime(&curr_time);
    return curr_time_str;
}

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:53:172015亚冠之水原三星
日期:2015-06-02 16:34:202015年亚冠纪念徽章
日期:2015-10-19 18:13:37程序设计版块每日发帖之星
日期:2015-11-08 06:20:00
6 [报告]
发表于 2007-09-05 18:27 |只看该作者

  1. Date: Tue, 04 Sep 2007 14:22:22 GMT
  2. Server: Apache/2.0.54 (Unix) PHP/5.2.3
  3. Last-Modified: Wed, 04 Apr 2007 09:53:18 GMT
复制代码

这部分头可以不要的,起码对IE,FireFox,一些下载工具没问题,timestamp开销还是有的,如果需要客户端缓存的话,加个Etag就行,可以自己制定格式,很简单
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP