iOS 上传文件到服务器

来源:互联网 发布:软件项目经理培训视频 编辑:程序博客网 时间:2024/05/16 11:01
-(void)UploadAllRecords:(id)sender{    NSUserDefaults* Defaults = [NSUserDefaults standardUserDefaults];    NSLog(@"Upload All Records...");    NSString *stepMsg = [NSString stringWithFormat:                         @"{"                                                  "\"user\""":"                         "%@,"                         "\"data\""":"                         "\"%@,"                         "%@,"                         "%@,"                         "5,"                         "0.6,"                         "13.22,"                         "3"                         ";\""                         "}",[Defaults objectForKey:@"UserName"],[Defaults objectForKey:@"LoStr"],[Defaults objectForKey:@"Nowtime"],[Defaults objectForKey:@"NowData"]];    NSLog(@"stepMsg:%@",stepMsg);            ZipArchive* zip = [[ZipArchive alloc] init];    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);    NSString *documentpath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;    NSString* l_zipfile = [documentpath stringByAppendingString:@"/test.zip"] ;    NSString* file = [documentpath stringByAppendingString:@"/a_m.txt"] ;    [stepMsg writeToFile:file atomically:YES encoding:NSUTF8StringEncoding error:nil];    NSLog(@"file:%@",file);    BOOL ret = [zip CreateZipFile2:l_zipfile];    ret = [zip addFileToZip:file newname:@"flie.txt"];    if( ![zip CloseZipFile2] )    {        l_zipfile = @"";    }    //http://50.116.37.92:8080/ActiveTravel1.2/GetLastPointidByUserid?userId=ycy    NSURL *url = [NSURL URLWithString: [NSString stringWithFormat:@"http://50.116.37.92:8080/ActiveTravel1.2/GetLastPointidByUserid?userId=%@",[Defaults objectForKey:@"UserName"]]];    // 根据上面的URL创建一个请求    NSLog(@"url====%@",url);        NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];    [req setHTTPMethod:@"POST"];    conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];    if (conn) {        if (webData) {            webData=nil;        }        webData = [NSMutableData data];        NSLog(@"webData:  %@",webData);    }    //http://10.0.2.2:8080/ActiveTravel1.2/GetPointsFileServlet    NSData *data = [NSData dataWithContentsOfFile:l_zipfile];    NSString * newURL =@"http://50.116.37.92:8080";    NSMutableURLRequest *request = [[AFHTTPClient clientWithBaseURL:[NSURL URLWithString:newURL]] multipartFormRequestWithMethod:@"POST" path:[NSString stringWithFormat:@"/ActiveTravel1.2/GetPointsFileServlet"] parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {        [formData appendPartWithFileData:data name:@"test" fileName:@"test.zip" mimeType:@"txt/zip"];     }];    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {float progress = totalBytesWritten / (float)totalBytesExpectedToWrite;NSLog(@"Sent %f ..",progress);    }];[operation start];    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {NSLog(@" ********RequestDidFinish**********:  %@ ",[operation responseString]);        UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"UploadAll" message:@"Success" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];        [alter show];            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {NSLog(@"error: %@",  operation.responseString);        UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"UploadAll" message:@"Server is not avaliable!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];        [alter show];}];}

 

执行这个操作之后 返回错误 


error: <html><head><title>Apache Tomcat/7.0.39 - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 500 - 1</h1><HR size="1" noshade="noshade"><p><b>type</b> Exception report</p><p><b>message</b> <u>1</u></p><p><b>description</b> <u>The server encountered an internal error that prevented it from fulfilling this request.</u></p><p><b>exception</b> <pre>java.lang.ArrayIndexOutOfBoundsException: 1service.point.PointService.assemblingPoint(PointService.java:53)service.point.PointService.addPoint(PointService.java:28)service.MobileService.executeUploadPoints(MobileService.java:13)servlet.GetPointsFileServlet.doPost(GetPointsFileServlet.java:80)javax.servlet.http.HttpServlet.service(HttpServlet.java:647)javax.servlet.http.HttpServlet.service(HttpServlet.java:728)filter.SessionCheckFilter.doFilter(SessionCheckFilter.java:45)</pre></p><p><b>note</b> <u>The full stack trace of the root cause is available in the Apache Tomcat/7.0.39 logs.</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/7.0.39</h3></body></html>



原创粉丝点击