サムネイル画像をサーブレットにアップロードしようとしています。サーブレット コードは既に存在し、Web サイトで動作しています。
サンプル URL http://abcd.com/upload?param1=abc¶m2=pqr
データはウェブサイトに掲載されますが、画像は掲載されません。
次のコード スニペットを使用して画像をアップロードしようとしました。
コード 1:
NSData *imageData = UIImageJPEGRepresentation(imageSelect.image, 1.0);
//NSData *imageData = UIImagePNGRepresentation(imageSelect.image);
NSMutableData *postbody = [NSMutableData data];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data;boundary=%@",boundary];
NSMutableURLRequest *request= [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:strURL]];
[request setHTTPMethod:@"POST"];
[request addValue:@"Keep-Alive" forHTTPHeaderField:@"Connection"];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSString *strFilename = [self getFilePath];
NSLog(@"filename:[%@]",strFilename);
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"fileing\"; filename=\"%@\"\r\n", strFilename] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[NSData dataWithData:imageData]];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postbody];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
//Return string always returned empty
NSLog(@"%@", returnString);
コード 2
//NSData *imageData = UIImageJPEGRepresentation(imageSelect.image, 0.7);
NSData *imageData = UIImagePNGRepresentation(imageSelect.image);
NSString *filename = [self getFilePath];
//Filename:\\var\\mobile\\Applications\\F2Dxxxx-8656-4208-B398-8D504320FC7B\\Documents\\Image_Photo.jpg
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:strURL ]];
NSString *boundary = [NSString stringWithFormat:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
NSString *contentString = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@\"\r\n",filename];
[request addRequestHeader:@"Connection" value:@"Keep-Alive"];
[request addRequestHeader:@"Content-Type" value:contentType];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[contentString dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setTimeOutSeconds:60]; // 1 minute
[request appendPostData:body];
[request setShowAccurateProgress:YES];
[request setDelegate:self];
[request setDidFinishSelector:@selector(requestDone:)];
[request setDidFailSelector:@selector(requestWentWrong:)];
[request startAsynchronous];
どこが間違っているのか教えてください。
また、 http://insanelycrazy.net/development/faces/viewarticle.xhtml?id= 6 で記事を見つけましたが 、サーバー コードの準備ができているため、このスニペットは使用できません。
以下はAndroidで使用されるコードです
FileInputStream fileInputStream = new FileInputStream(new File(filepath));
URL url = new URL(urlString);
Log.i("", "URL : " + url.toString());
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
conn.setChunkedStreamingMode(maxBufferSize);
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
Log.i("", "Before filepath : " + filepath);
filepath = filepath.replace("/", "\\" + "\\");
Log.i("", "After filepath : " + filepath);
dos.writeBytes("Content-Disposition: form-data; name=\"fileing\";filename=\"" + filepath + "\"" + lineEnd);
dos.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
progressStatus += bytesRead;
publishProgress(progressStatus);
}
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
fileInputStream.close();
dos.flush();
dos.close();
よろしく、 サミット