ios如何实现http协议数据流上传 30
实现了android版本的上传功能,代码大约如下:conn=(HttpURLConnection)url2.openConnection();conn.setDoOutp...
实现了android版本的上传功能,代码大约如下:
conn = (HttpURLConnection) url2.openConnection();conn.setDoOutput(true);conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
conn.setRequestProperty("Charsert", "UTF-8");
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
OutputStream out = new DataOutputStream(conn.getOutputStream());
for (int i = 0; i < params.length; i++){
StringBuilder sb = new StringBuilder();
sb = sb.append("--");
sb = sb.append(BOUNDARY);
。。。
byte[] data = sb.toString().getBytes();
out.write(data);
}
StringBuilder sb = new StringBuilder();
sb.append("--");
byte[] data = sb.toString().getBytes();
out.write(data);
out.flush();
RandomAccessFile accFile = new RandomAccessFile(file, "rw");
//accFile.seek(Long.parseLong((String) rowSet.getValue("START", "0")));
accFile.seek(start);
int bytes = 0;
byte[] bufferOut = new byte[1024];
long temp = 0;
if(pauseBtn != null)
{
pauseBtn.setClickable(true);
}
while ((bytes = accFile.read(bufferOut)) != -1)
{
if(!NetHelper.isConnect(context))
{
pause = true;
Toast.makeText(context, "网络意外终止,请检查网络!", Toast.LENGTH_SHORT).show();
}
if(pause){
break;
}
temp = temp + bytes;
out.write(bufferOut, 0, bytes);
out.flush();
fileInfo.setyLength(String.valueOf(start+temp));
fileInfoDbManager.updateProgress(fileid, String.valueOf(start+temp));
}
就是用了
HttpURLConnection,然后得到输出流
DataOutputStream,然后把图片用它写入到服务器中。
现在需要实现的时ios版本的断点续传的上传功能。我看了下,
ios也有
NSURLConnection,用
NSMutableURLRequest *result;
我想要实现和android一样的功能,一个字节一个字节的写入,这是因为要考虑断点续传,下次传的时候要传从哪个字节开始的,所以必须这么做,而不能用
[urlRequest setHTTPBody:result];,请问我该怎么解决 展开
conn = (HttpURLConnection) url2.openConnection();conn.setDoOutput(true);conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
conn.setRequestProperty("Charsert", "UTF-8");
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
OutputStream out = new DataOutputStream(conn.getOutputStream());
for (int i = 0; i < params.length; i++){
StringBuilder sb = new StringBuilder();
sb = sb.append("--");
sb = sb.append(BOUNDARY);
。。。
byte[] data = sb.toString().getBytes();
out.write(data);
}
StringBuilder sb = new StringBuilder();
sb.append("--");
byte[] data = sb.toString().getBytes();
out.write(data);
out.flush();
RandomAccessFile accFile = new RandomAccessFile(file, "rw");
//accFile.seek(Long.parseLong((String) rowSet.getValue("START", "0")));
accFile.seek(start);
int bytes = 0;
byte[] bufferOut = new byte[1024];
long temp = 0;
if(pauseBtn != null)
{
pauseBtn.setClickable(true);
}
while ((bytes = accFile.read(bufferOut)) != -1)
{
if(!NetHelper.isConnect(context))
{
pause = true;
Toast.makeText(context, "网络意外终止,请检查网络!", Toast.LENGTH_SHORT).show();
}
if(pause){
break;
}
temp = temp + bytes;
out.write(bufferOut, 0, bytes);
out.flush();
fileInfo.setyLength(String.valueOf(start+temp));
fileInfoDbManager.updateProgress(fileid, String.valueOf(start+temp));
}
就是用了
HttpURLConnection,然后得到输出流
DataOutputStream,然后把图片用它写入到服务器中。
现在需要实现的时ios版本的断点续传的上传功能。我看了下,
ios也有
NSURLConnection,用
NSMutableURLRequest *result;
我想要实现和android一样的功能,一个字节一个字节的写入,这是因为要考虑断点续传,下次传的时候要传从哪个字节开始的,所以必须这么做,而不能用
[urlRequest setHTTPBody:result];,请问我该怎么解决 展开
3个回答
展开全部
ios应用中HTTP上传请求协议,用HTTP协议做web上传,这里需要自定义HTTP协议。
HTTP协议应该注意四点:
1、初始化响应服务器地址
NSURL *url = [NSURL URLWithString:@"http://xxx.xxx.xx.xxx/mqupload.jsp"];
[urlRequest setValue: [NSString stringWithFormat:@"%@\r\n", @"http://XXXXXX HTTP/1.1"]]; //可要可无
2、设置提交方法 GET/POST
[urlRequest setHTTPMethod:@"POST"];
3、设置响应内容类型
[urlRequest setValue: [NSString stringWithFormat:@"multipart/form-data;
boundary=---------%@", @"7daaba1e0368"] forHTTPHeaderField:@"Content-Type"];
4、设置响应内容
NSMutableData *postData = [NSMutableData dataWithCapacity:[m_imageData length]];
[postData appendData: [[NSString stringWithFormat:@"---------%@\r\n", @"7daaba1e0368"]
dataUsingEncoding:NSUTF8StringEncoding]];//开始标志
[postData appendData: [[NSString stringWithFormat: @"Content-Disposition:form-data; name="File1";
filename="1.jpg"\r\n Content-type: image/pjpeg\r\n\r\n"]dataUsingEncoding:NSUTF8StringEncoding]];//name是页面文件的参数,type是文件类型
[postData appendData:imageData];// 文件数据
[postData appendData: [[NSString stringWithFormat:@"\r\n---------%@--\r\n", @"7daaba1e0368"]
dataUsingEncoding:NSUTF8StringEncoding]];// 文件结束标志
[urlRequest setHTTPBody:postData];//把数据加载到响应的文件体
只要做到以上几点,一个完整的http请求协议就做好了。
下面附一个简洁版完整的请求协议体
NSURL *url = [NSURL URLWithString:@"http://XX.XX.XX"];//请求服务器路径。
m_imageData = UIImagePNGRepresentation(myImageView.image);
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setValue: [NSString stringWithFormat:@"multipart/form-data; boundary=---------%@",
@"7daaba1e0368"] forHTTPHeaderField:@"Content-Type"];
[urlRequest setValue: [NSString stringWithFormat:@"%@\r\n\r\n",
@" keep-alive"] forHTTPHeaderField:@"Connection"];
NSMutableData *postData = [NSMutableData dataWithCapacity:[m_imageData length]];
[postData appendData: [[NSString stringWithFormat:@"---------%@\r\n",
@"7daaba1e0368"] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData: [[NSString stringWithFormat:
@"Content-Disposition:form-data; name="File1"; filename="1.jpg"\r\n Content-type:
image/pjpeg\r\n\r\n"]dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:m_imageData];
[postData appendData: [[NSString stringWithFormat:@"\r\n---------%@--\r\n", @"7daaba1e0368"]
dataUsingEncoding:NSUTF8StringEncoding]];
[urlRequest setHTTPBody:postData];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
HTTP协议应该注意四点:
1、初始化响应服务器地址
NSURL *url = [NSURL URLWithString:@"http://xxx.xxx.xx.xxx/mqupload.jsp"];
[urlRequest setValue: [NSString stringWithFormat:@"%@\r\n", @"http://XXXXXX HTTP/1.1"]]; //可要可无
2、设置提交方法 GET/POST
[urlRequest setHTTPMethod:@"POST"];
3、设置响应内容类型
[urlRequest setValue: [NSString stringWithFormat:@"multipart/form-data;
boundary=---------%@", @"7daaba1e0368"] forHTTPHeaderField:@"Content-Type"];
4、设置响应内容
NSMutableData *postData = [NSMutableData dataWithCapacity:[m_imageData length]];
[postData appendData: [[NSString stringWithFormat:@"---------%@\r\n", @"7daaba1e0368"]
dataUsingEncoding:NSUTF8StringEncoding]];//开始标志
[postData appendData: [[NSString stringWithFormat: @"Content-Disposition:form-data; name="File1";
filename="1.jpg"\r\n Content-type: image/pjpeg\r\n\r\n"]dataUsingEncoding:NSUTF8StringEncoding]];//name是页面文件的参数,type是文件类型
[postData appendData:imageData];// 文件数据
[postData appendData: [[NSString stringWithFormat:@"\r\n---------%@--\r\n", @"7daaba1e0368"]
dataUsingEncoding:NSUTF8StringEncoding]];// 文件结束标志
[urlRequest setHTTPBody:postData];//把数据加载到响应的文件体
只要做到以上几点,一个完整的http请求协议就做好了。
下面附一个简洁版完整的请求协议体
NSURL *url = [NSURL URLWithString:@"http://XX.XX.XX"];//请求服务器路径。
m_imageData = UIImagePNGRepresentation(myImageView.image);
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setValue: [NSString stringWithFormat:@"multipart/form-data; boundary=---------%@",
@"7daaba1e0368"] forHTTPHeaderField:@"Content-Type"];
[urlRequest setValue: [NSString stringWithFormat:@"%@\r\n\r\n",
@" keep-alive"] forHTTPHeaderField:@"Connection"];
NSMutableData *postData = [NSMutableData dataWithCapacity:[m_imageData length]];
[postData appendData: [[NSString stringWithFormat:@"---------%@\r\n",
@"7daaba1e0368"] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData: [[NSString stringWithFormat:
@"Content-Disposition:form-data; name="File1"; filename="1.jpg"\r\n Content-type:
image/pjpeg\r\n\r\n"]dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:m_imageData];
[postData appendData: [[NSString stringWithFormat:@"\r\n---------%@--\r\n", @"7daaba1e0368"]
dataUsingEncoding:NSUTF8StringEncoding]];
[urlRequest setHTTPBody:postData];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询