サーバーに画像をアップロードする方法が頭を悩ませていました。これは私にとって新しいタスクですが、私にとっては非常に混乱しています。しかし、私は問題を抱えています。
私の意図は、SDカードから写真をアップロードし、カメラから写真を撮り、サーバーにアップロードすることです。
ios では、このタスクは既に完了しています。ios php では、次のタイプを取得します。
このように印刷します。
$filedata = $_FILES['photos'];
$f = fopen(time().".txt",'wb');
fwrite($f, print_r($_REQUEST,true));
fclose($f);
[photos] => Array
(
[name] => game.png
[type] => image/png
[tmp_name] => /tmp/phpiQHIXQ
[error] => 0
[size] => 1664972
)
私がやること。
// yourSelectedImage is the bitmap image.
public void SaveImage() throws Exception {
try {
String url = "http://test.com/uploadImage.php";
ByteArrayOutputStream bos = new ByteArrayOutputStream();
yourSelectedImage.compress(CompressFormat.PNG, 75, bos);
byte[] data = bos.toByteArray();
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(url);
ByteArrayBody bab = new ByteArrayBody(data, "image/jpeg");
MultipartEntity reqEntity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("photos", bab);
postRequest.setEntity(reqEntity);
HttpResponse response = httpClient.execute(postRequest);
BufferedReader reader = new BufferedReader(new InputStreamReader(
response.getEntity().getContent(), "UTF-8"));
String sResponse;
StringBuilder s = new StringBuilder();
while ((sResponse = reader.readLine()) != null) {
s = s.append(sResponse);
}
System.out.println("Response: " + s);
} catch (Exception e) {
// handle exception here
Log.e(e.getClass().getName(), e.getMessage());
}
}
PHPサーバーでは、このようになります
$filedata = $_FILES['写真'];
さらに、PHPで実行される機能。
ただし、httppost を使用して、上記のファイル構造タイプのマルチパートに投稿することはありません。ファイルには、画像名、画像タイプ、一時名などが含まれます。
編集:
私は少しコードを変更しました:, しかし、画像ファイル情報をphpサーバーに投稿できませんでした, ユーザーIDはOKですが、画像ファイル情報は投稿できませんでした.
コードは次のとおりです。
public class UploadImageHttpPost {
String testpath="/mnt/sdcard/14111.jpg";
String url = "http://test.com/uploadImage.php";
public static void sendPost(String url, String imagePath,String userId)
throws IOException, ClientProtocolException {
Log.w("TAG", "Start Upload" );
Log.w("TAG", "URL-> "+url );
Log.w("TAG", "Image Path-> "+imagePath );
Log.w("TAG", "User Id-> "+userId );
String responseBody;
MultipartEntity entity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
File file = new File(imagePath);
FileBody encFile = new FileBody(file,"image/jpeg");
Log.w("TAG", "image file-> "+encFile );
entity.addPart("photos", encFile);
entity.addPart("UserId", new StringBody(userId));
HttpPost request = new HttpPost(url);
request.setEntity(entity);
HttpClient client = new DefaultHttpClient();
ResponseHandler<String> responsehandler = new BasicResponseHandler();
responseBody = client.execute(request, responsehandler);
if (responseBody != null && responseBody.length() > 0) {
Log.w("TAG", "Response from image upload->" + responseBody);
}
}
私はこのようになります:
Array
(
[UserId] => 914
)
しかし、ここで欠落している部分が得られないことを意味します。マルチパートポストメソッドに欠落しているコードはありますか?上記のJavaコードを確認してください:
[photos] => Array
(
[name] => game.png
[type] => image/png
[tmp_name] => /tmp/phpiQHIXQ
[error] => 0
[size] => 1664972
)
画像の上記情報、phpへの投稿方法、訂正お願いします。