1
我正在將其他人的iOS應用程序轉換爲android,並且遇到了無法制作此多部分實體的正面或反面的問題創建。試圖從Objective-C文本構建多部分構建Java中的MultiPart實體
NSMutableData *body = [NSMutableData data];
// file
NSData *imageData = UIImageJPEGRepresentation(image, 90);
NSString *boundary = [NSString stringWithFormat:@"---------------------------14737809831466499882746641449"];
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: attachment; name=\"imagefile\"; filename=\"user_%@.jpg\"\r\n",userID] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
// UserID
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userid\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:userID] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
// close form
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
,我覺得看起來像這樣
---------------------------14737809831466499882746641449
Content-Disposition: attachment;
name=\"imagefile\"; filename=\"user_" + userID +".jpg\"
Content-Type: application/octet-stream
imagedata
---------------------------14737809831466499882746641449
Content-Disposition: form-data;
name=\"userid\"
userID
---------------------------14737809831466499882746641449--
現在我嘗試使用MultiPartEntity
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(url);
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, null, Charset.forName("UTF-8"));
重建這在Java和我被困在盤算相當於多文本。
任何幫助,將不勝感激。