从网上下的开源代码boa+cgi,移植到arm里做个在线升级,曾以为最难的flash擦写先搞定了,却卡在上传文件这块。
问题是超过1024K的文件不能上传,连.cgi文件也没有执行,或者执行却失败了(至少没有执行到.cgi的main函数第一句)。既然小文件能上传,那么可以判定不是代码的问题,何况代码是开源下载基本没做实质性的更改;内存也够了,还有20多兆空间,分区虽然还是稀里糊涂,但上传的过程不涉及flash分区。
那么唯一的可能就是哪里没有配置好。
后来在网上找到一点提示,原来问题出现在这里
1. 修改源代码的defines.h里面的宏SINGLE_POST_LIMIT_DEFAULT
2. 修改boa.conf里面的SinglePostLimit
cgic文件命令为:upload.c
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/stat.h>
#include"cgic.h"
#define BufferLen 1024
int cgiMain(void){
cgiFilePtr file;
int targetFile;
mode_t mode;
char name[128];
char fileNameOnServer[64];
char contentType[1024];
char buffer[BufferLen];
char *tmpStr=NULL;
int size;
int got,t;
cgiHeaderContentType("text/html");
//取得html页面中file元素的值,应该是文件在客户机上的路径名
if (cgiFormFileName("file", name, sizeof(name)) !=cgiFormSuccess) {
fprintf(stderr,"could not retrieve filename\n");
goto FAIL;
}
cgiFormFileSize("file", &size);
//取得文件类型,不过本例中并未使用
cgiFormFileContentType("file", contentType, sizeof(contentType));
//目前文件存在于系统临时文件夹中,通常为/tmp,通过该命令打开临时文件。临时文件的名字与用户文件的名字不同,所以不能通过路径/tmp/userfilename的方式获得文件
if (cgiFormFileOpen("file", &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,"./getfile/");
strcat(fileNameOnServer,name+t+1);
mode=S_IRWXU|S_IRGRP|S_IROTH;
//在当前目录下建立新的文件,第一个参数实际上是路径名,此处的含义是在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;
}
//从系统临时文件中读出文件内容,并放到刚创建的目标文件中
while (cgiFormFileRead(file, buffer, BufferLen, &got) ==cgiFormSuccess){
if(got>0)
write(targetFile,buffer,got);
}
cgiFormFileClose(file);
close(targetFile);
goto END;
FAIL:
fprintf(stderr,"Failed to upload");
return 1;
END:
printf("File \"%s\" has been uploaded",fileNameOnServer);
return 0;
}
html文件命名为:upload.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://blog.csdn.net/faihung">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Test Upload</title>
<meta name="author" content="Jack">
<!-- Date: 2007-08-30 -->
</head>
<body>
<form action="cgi-bin/upload.cgi" method="post" enctype="multipart/form-data" target="_blank">
<input type="file" name="file" value="" />
<input type="submit" name="submit" value="OK">
</form>
</body>
</html>
这个比较具有代表性,就是boa默认上传文件大小为1M,但这里已经改为16M。在/boa/defines.h里配置上传文件大小限制
#define SINGLE_POST_LIMIT_DEFAULT 1024 * 1024 *16/* 1 MB */
之后再按照前面的操作,发现没什么问题。