CGI程序: 获得HTTP POST上传的文件,并保存在本地
2015-01-10 23:31
417 查看
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include"cgic.h"
//author pdfccc@sina.com
#define BufferLen 4096
#define MAX_FILE_SIZE 20000000 // 10M
int cgiMain()
{
cgiFilePtr file;
int targetFile;
mode_t mode;
char name[256];
char fileNameOnServer[256];
char contentType[1024];
char buffer[BufferLen];
char* tmpStr = NULL;
int size;
int got, t;
cgiHeaderContentType("text/html");
//取得html页面中file元素的值,应该是文件在客户机上的路径名
cgiFormResultType ret = cgiSYSErr;
ret = cgiFormFileName("upload_file_name", name , sizeof(name));
if( ret != cgiFormSuccess )
{
fprintf(stderr ,"Could not retrieve filename. ret=%d. \n", ret);
goto FAIL;
}
else
{
printf("File name = %s. \n <br>", name);
}
cgiFormFileSize("upload_file_name", &size);
printf("File size = %d.\n <br>", size);
if ( size > MAX_FILE_SIZE )
{
printf("File size too big. should less than 20M.");
goto FAIL;
}
//取得文件类型,不过本例中并未使用
cgiFormFileContentType("upload_file_name", contentType , sizeof(contentType));
printf("ContentType = %s.\n <br>", contentType);
//web容器把文件存在于系统临时文件夹中,通常为/tmp,通过该命令打开临时文件。
//临时文件的名字与用户文件的名字不同,所以不能通过路径/tmp/userfilename的方式获得文件
if( cgiFormFileOpen("upload_file_name", &file) != cgiFormSuccess)
{
fprintf(stderr ,"could not open the file. \n");
goto FAIL;
}
t = -1;
//从路径名解析出用户文件名
while(1)
{
tmpStr = strstr(name + t + 1 ,"\\");
if(NULL == tmpStr) tmpStr = strstr(name + t + 1 ,"/"); //if"\\"is not path separator, try"/"
if(NULL != tmpStr) t =(int)( tmpStr-name);
else break;
}
strcpy(fileNameOnServer, "uploaded/");
strcat(fileNameOnServer, name + t + 1); //只包含文件名
//mode = S_IRWXU | S_IRGRP | S_IROTH; //0666
printf("ready write to server path: %s. <br>", fileNameOnServer);
mode = 0666;
//在当前目录下建立新的文件,第一个参数实际上是路径名,此处的含义是在cgi程序所在的目录(当前目录)建立新文件
targetFile = open(fileNameOnServer , O_RDWR|O_CREAT|O_TRUNC|O_APPEND , mode);
if( targetFile < 0)
{
fprintf(stderr ,"could not create the new file, %s. \n", fileNameOnServer);
goto FAIL;
}
printf("<br>");
//从系统临时文件中读出文件内容,并放到刚创建的目标文件中
while(cgiFormFileRead(file , buffer , BufferLen , &got) == cgiFormSuccess)
{
if(got > 0) write(targetFile , buffer , got);
printf(".");
}
cgiFormFileClose(file);
close(targetFile);
goto END;
FAIL:
fprintf(stderr ,"Failed to upload.");
return 1;
END:
printf("File %s has been uploaded.", fileNameOnServer);
printf("<br><br> 下载该文件请使用: <H3>http://172.27.194.147:8080/js/%s</H3> <br>", fileNameOnServer);
return 0;
}
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include"cgic.h"
//author pdfccc@sina.com
#define BufferLen 4096
#define MAX_FILE_SIZE 20000000 // 10M
int cgiMain()
{
cgiFilePtr file;
int targetFile;
mode_t mode;
char name[256];
char fileNameOnServer[256];
char contentType[1024];
char buffer[BufferLen];
char* tmpStr = NULL;
int size;
int got, t;
cgiHeaderContentType("text/html");
//取得html页面中file元素的值,应该是文件在客户机上的路径名
cgiFormResultType ret = cgiSYSErr;
ret = cgiFormFileName("upload_file_name", name , sizeof(name));
if( ret != cgiFormSuccess )
{
fprintf(stderr ,"Could not retrieve filename. ret=%d. \n", ret);
goto FAIL;
}
else
{
printf("File name = %s. \n <br>", name);
}
cgiFormFileSize("upload_file_name", &size);
printf("File size = %d.\n <br>", size);
if ( size > MAX_FILE_SIZE )
{
printf("File size too big. should less than 20M.");
goto FAIL;
}
//取得文件类型,不过本例中并未使用
cgiFormFileContentType("upload_file_name", contentType , sizeof(contentType));
printf("ContentType = %s.\n <br>", contentType);
//web容器把文件存在于系统临时文件夹中,通常为/tmp,通过该命令打开临时文件。
//临时文件的名字与用户文件的名字不同,所以不能通过路径/tmp/userfilename的方式获得文件
if( cgiFormFileOpen("upload_file_name", &file) != cgiFormSuccess)
{
fprintf(stderr ,"could not open the file. \n");
goto FAIL;
}
t = -1;
//从路径名解析出用户文件名
while(1)
{
tmpStr = strstr(name + t + 1 ,"\\");
if(NULL == tmpStr) tmpStr = strstr(name + t + 1 ,"/"); //if"\\"is not path separator, try"/"
if(NULL != tmpStr) t =(int)( tmpStr-name);
else break;
}
strcpy(fileNameOnServer, "uploaded/");
strcat(fileNameOnServer, name + t + 1); //只包含文件名
//mode = S_IRWXU | S_IRGRP | S_IROTH; //0666
printf("ready write to server path: %s. <br>", fileNameOnServer);
mode = 0666;
//在当前目录下建立新的文件,第一个参数实际上是路径名,此处的含义是在cgi程序所在的目录(当前目录)建立新文件
targetFile = open(fileNameOnServer , O_RDWR|O_CREAT|O_TRUNC|O_APPEND , mode);
if( targetFile < 0)
{
fprintf(stderr ,"could not create the new file, %s. \n", fileNameOnServer);
goto FAIL;
}
printf("<br>");
//从系统临时文件中读出文件内容,并放到刚创建的目标文件中
while(cgiFormFileRead(file , buffer , BufferLen , &got) == cgiFormSuccess)
{
if(got > 0) write(targetFile , buffer , got);
printf(".");
}
cgiFormFileClose(file);
close(targetFile);
goto END;
FAIL:
fprintf(stderr ,"Failed to upload.");
return 1;
END:
printf("File %s has been uploaded.", fileNameOnServer);
printf("<br><br> 下载该文件请使用: <H3>http://172.27.194.147:8080/js/%s</H3> <br>", fileNameOnServer);
return 0;
}
相关文章推荐
- android 上传文件"Content-Type",为"application/octet-stream" 用php程序在服务端用$GLOBALS['HTTP_RAW_POST_DATA']接受(二)
- Java模块 -- Http发送post数据文件,服务端Servlet接收,保存至本地
- android 上传文件"Content-Type",为"application/octet-stream" 用php程序在服务端用$GLOBALS['HTTP_RAW_POST_DATA']接受(二)
- Android将程序崩溃信息保存本地文件以及上传到服务器
- 用HttpListener做web服务器,简单解析post方式过来的参数、上传的文件
- 接收浏览器上传文件的cgi程序
- 将本地图片上传保存到数据库(理论上支持各种文件的上传)
- C#实现http协议支持上传下载文件的GET、POST请求
- 利用HttpURLConnection发送post请求上传多个文件
- 转:vc中如何通过http的post方式上传文件
- [转]vc++ post http实现文件上传
- 利用HttpURLConnection发送post请求上传文件
- Android 通过HTTP POST 上传图片文件
- 通过 HTTP POST 上传文件到服务器
- 如何用Java客户端/applet通过HTTP POST上传文件
- 直接使用http post的方式上传文件到SharePoint
- 通过 HTTP POST 上传文件到服务器.
- 将文件通过http的post方法上传到服务器
- HttpClient post 上传文件 笔记
- 使用servlet保存用户上传的文件到本地