由于视频文件相对图片文件较大,所以不能够一次性加载到内存中去,需要一点一点的读取视频文件(利用到了NSInputStream)
项目例子:
/**
* @method + (NSString *)uploadVideoFilePath:(NSString *)filePath withFileName:(NSString *)fileName withFileExt:(NSString *)fileExt withErrorCode:(NSInteger *)errorCode
*
* @abstract 上传文件
*
* @param fileData 文件数据
* @param errorCode [out]错误代码
*
* @return 如果上传成功则返回资源路径
*/
+ (NSString *)uploadVideoFilePath:(NSString *)filePath
withFileName:(NSString *)fileName
withFileExt:(NSString *)fileExt
withErrorCode:(NSInteger *)errorCode
{
NSMutableString * uploadFileURL = [[NSMutableString alloc] initWithCapacity:20];
[uploadFileURL appendFormat:@"http://%@/upload_file",UPLOAD_HOST];
[uploadFileURL appendFormat:@"?%@",[self getURLSuffixWithURL]];
//创建Request对象
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:uploadFileURL]];
[request setHTTPMethod:@"POST"];
NSMutableData *body = [NSMutableData data];
//设置表单项分隔符
NSString *boundary = @"---------------------------20140601chrrsmusic1703";
//设置内容类型
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
//写入图片的内容
NSString * fileFullName = [NSString stringWithFormat:@"%@.%@",fileName,fileExt];
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n",fileName,fileFullName] dataUsingEncoding:NSUTF8StringEncoding]];
NSString * contentTypeStr = [NSString stringWithFormat:@"Content-Type: video/%@\r\n\r\n",fileExt];
[body appendData:[contentTypeStr dataUsingEncoding:NSUTF8StringEncoding]];
// NSString * appendDataStr = [[NSString alloc] initWithData:body encoding:NSUTF8StringEncoding]];
// DELOG(@"appendDataStr: \r\n%@",appendDataStr);
// 视频数据
NSInputStream * fileStream = [[NSInputStream alloc] initWithFileAtPath:filePath];
[fileStream open];
NSInteger maxLength = 128;
uint8_t readBuffer [maxLength];
BOOL endOfStreamReached = NO;
while (! endOfStreamReached)
{
NSInteger bytesRead = [fileStream read:readBuffer maxLength:maxLength];
if (bytesRead == 0)
{//文件读取到最后
endOfStreamReached = YES;
}else if (bytesRead == -1)
{//文件读取错误
endOfStreamReached = YES;
}else
{
[body appendBytes:readBuffer length:bytesRead];
}
}
[fileStream close];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
//写入尾部内容
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSHTTPURLResponse *urlResponese = nil;
NSError *error = [[NSError alloc]init];
NSData* resultData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponese error:&error];
if (!resultData) {
*errorCode = -1;
return nil;
}
NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:resultData options:NSJSONReadingMutableLeaves error:nil];
NSInteger statusCode = [[NSString ensureStringFor:[jsonData objectForKey:@"state"]] integerValue];
*errorCode = statusCode;
if (statusCode == 402) {
// 发出需要登录的通知
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_NEED_LOGINTOKEN
object:nil];
});
}
if (statusCode != 0) {
*errorCode = statusCode;
return nil;
}else{
NSDictionary * data = [jsonData objectForKey:@"data"];
if (![data isKindOfClass:[NSDictionary class]]) {
return nil;
}else{
NSString * fileURL = [NSString ensureStringFor:[data objectForKey:@"uri"]];
return fileURL;
}
}
}