gpt4 book ai didi

java - Objective C iOS 中文件上传图像文件代码

转载 作者:行者123 更新时间:2023-12-01 14:43:56 25 4
gpt4 key购买 nike

我是 iOS 应用程序开发新手,我只想使用 NSMutableURLRequest 上传图像文件。请求正文应包含在图像中。我只想将如下图像作为java代码发送

 HttpPut mCreatePost = new HttpPut(params[0]+"/data");
FileEntity fileEntity = new FileEntity(uploadFile, "image/jpeg");
mCreatePost.setEntity(fileEntity);
mHttpClient.execute(mCreatePost);

请帮我写一下上面的 Objective C 代码。提前致谢。

最佳答案

流程

  1. 创建 NSMutableURLRequest
  2. 设置 NSMutableURLRequest 的 URL
  3. 为 NSMutableURLRequest 设置 HTTP 方法
  4. 为 NSMutableURLRequest 设置 header 字段
  5. 为 NSMutableURLRequest 设置 HTTP 正文。这将包括图像文件和任何其他参数(下面的示例代码)
  6. 初始化先前声明的 NSMutableData 对象。这用于存储来自服务器的响应(下面的示例代码)
  7. 初始化先前声明的 NSURLConnection 对象并设置其委托(delegate)。在示例代码中,我创建了与委托(delegate)相同的类并实现了委托(delegate)方法。

示例代码

- (void)uploadImage    
NSString *urlStr = @"your link to the server";

NSMutableURLRequest *urlRequest = [[[NSMutableURLRequest alloc] init] autorelease];
[urlRequest setURL:[NSURL URLWithString:urlStr]];

[urlRequest setHTTPMethod:@"POST"];

NSString *myboundary = [NSString stringWithFormat:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",myboundary];
[urlRequest addValue:contentType forHTTPHeaderField: @"Content-Type"];

NSMutableData *postData = [NSMutableData data];

NSString *filePath = @"path to file you want to upload";

NSData *zipData = [NSData dataWithContentsOfFile:filePath];
if(zipData != nil){
[postData appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", myboundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@\"\r\n", @"file.zip"]dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[[NSString stringWithFormat:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[NSData dataWithData:zipData]];
}


NSString *params =@"Any other parameters you want to send with the file";

NSLog(@"%@",params);
[postData appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", myboundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", @"params"] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[[NSString stringWithFormat:@"%@", params] dataUsingEncoding:NSUTF8StringEncoding]];


[postData appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", myboundary] dataUsingEncoding:NSUTF8StringEncoding]];

[urlRequest setHTTPBody:postData];

[urlRequest setValue:[SettingsDataStore getSharedInstance].cooike forHTTPHeaderField:@"Cookie"];

responsedata = [[NSMutableData alloc] initWithData:nil];
conn = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];

}

//NSURLConnection Delegate

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responsedata appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"error occured %@", [error localizedDescription]);
[responseData release];
[conn release];


}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

//Do something with responseData and release it

[responseData release];
[conn release];

}

关于java - Objective C iOS 中文件上传图像文件代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15657666/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com