問題描述
此特定服務器端 php 代碼的幫助我對 php 一無所知,我必須將三張圖片從 android 上傳到此 php 頁面.
我嘗試了很多方法并進行了搜索,但沒有任何教程或任何內容對我沒有幫助,我的 android 代碼運行正常.DNS 也已配置,但圖像未顯示在服務器端.請幫我寫java代碼.
PHP:
0){header("HTTP/1.1 400 錯誤請求");回聲錯誤:".$_FILES["file1"]["error"] ."<br/>";}否則如果 ($_FILES["file2"]["error"] > 0){header("HTTP/1.1 400 錯誤請求");回聲錯誤:".$_FILES["file1"]["error"] ."<br/>";}否則如果 ($_FILES["file3"]["error"] > 0){header("HTTP/1.1 400 錯誤請求");回聲錯誤:".$_FILES["file1"]["error"] ."<br/>";}別的{如果 ($_FILES["file1"]["error"] > 0){回聲錯誤:".$_FILES["file1"]["error"] ."<br/>";}別的{回聲上傳:".$_FILES["file1"]["name"] ."<br/>";回聲類型:".$_FILES["file1"]["type"] ."<br/>";回聲大小:".($_FILES["file1"]["size"]/1024)."Kb<br/>";回聲存儲在:".$_FILES["file1"]["tmp_name"]."<br/>";}//$target_path = "上傳/";$target_path = "elp/pendingimages/";$target_path = $target_path .基本名稱($_FILES['file1']['name']);if(move_uploaded_file($_FILES['file1']['tmp_name'], $target_path)) {回聲文件".基本名稱($_FILES['file1']['name'])."已上傳";}別的{echo "上傳文件時出錯,請重試!";}如果 ($_FILES["file2"]["error"] > 0){回聲錯誤:".$_FILES["file2"]["error"] ."<br/>";}別的{回聲上傳:".$_FILES["file2"]["name"] ."<br/>";回聲類型:".$_FILES["file2"]["type"] ."<br/>";回聲大小:".($_FILES["file2"]["size"]/1024)."Kb<br/>";回聲存儲在:".$_FILES["file2"]["tmp_name"]."<br/>";}//$target_path = "上傳/";$target_path = "elp/pendingimages/";$target_path = $target_path .基本名稱($_FILES['file2']['name']);如果(move_uploaded_file($_FILES['file2']['tmp_name'],$target_path)){回聲文件".基本名稱($_FILES['file2']['name'])."已上傳";}別的{echo "上傳文件時出錯,請重試!";}如果 ($_FILES["file3"]["error"] > 0){回聲錯誤:".$_FILES["file3"]["error"] ."<br/>";}別的{回聲上傳:".$_FILES["file3"]["name"] ."<br/>";回聲類型:".$_FILES["file3"]["type"] ."<br/>";回聲大小:".($_FILES["file3"]["size"]/1024)."Kb<br/>";回聲存儲在:".$_FILES["file3"]["tmp_name"]."<br/>";}//$target_path = "上傳/";$target_path = "elp/pendingimages/";$target_path = $target_path .基本名稱($_FILES['file3']['name']);if(move_uploaded_file($_FILES['file3']['tmp_name'], $target_path)) {回聲文件".基本名稱($_FILES['file3']['name'])."已上傳";}別的{echo "上傳文件時出錯,請重試!";}}?>
Java:
public class TryprojectActivity extends Activity {輸入流是;int pic_count = 0;位圖 bitmap=null;FileInputStream in1,in2,in3;BufferedInputStream buf;@覆蓋公共無效onCreate(捆綁冰柱){super.onCreate(冰柱);setContentView(R.layout.main);嘗試 {in1 = new FileInputStream("/sdcard/1.jpg");}抓住(FileNotFoundException e2){//TODO 自動生成的 catch 塊e2.printStackTrace();}嘗試 {in2 = new FileInputStream("/sdcard/2.jpg");} catch (FileNotFoundException e1) {//TODO 自動生成的 catch 塊e1.printStackTrace();}嘗試 {in3 = new FileInputStream("/sdcard/3.jpg");}抓住(FileNotFoundException e1){//TODO 自動生成的 catch 塊e1.printStackTrace();}位圖 bitmapOrg1 = BitmapFactory.decodeStream(in1);ByteArrayOutputStream bao1 = new ByteArrayOutputStream();bitmapOrg1.compress(Bitmap.CompressFormat.JPEG, 90, bao1);字節 [] imagearray1 = bao1.toByteArray();String ba1=Base64.encode(imagearray1);位圖 bitmapOrg2 = BitmapFactory.decodeStream(in2);ByteArrayOutputStream bao2 = new ByteArrayOutputStream();bitmapOrg2.compress(Bitmap.CompressFormat.JPEG, 90, bao2);字節 [] imagearray2 = bao2.toByteArray();String ba2=Base64.encode(imagearray2);位圖 bitmapOrg3 = BitmapFactory.decodeStream(in3);ByteArrayOutputStream bao3 = new ByteArrayOutputStream();bitmapOrg3.compress(Bitmap.CompressFormat.JPEG, 90, bao3);字節 [] imagearray3 = bao3.toByteArray();String ba3=Base64.encode(imagearray3);列表nameValuePairs = new ArrayList(3);nameValuePairs.add(new BasicNameValuePair("image1",ba1));nameValuePairs.add(new BasicNameValuePair("image2",ba2));nameValuePairs.add(new BasicNameValuePair("image3",ba3));嘗試{HttpClient httpclient = new DefaultHttpClient();HttpPost httppost = new HttpPost("http://helpdesk.cispl.com/upload_file.php");UrlEncodedFormEntity obj = new UrlEncodedFormEntity(nameValuePairs);obj.setChunked(true);httppost.setEntity(obj);HttpResponse 響應 = httpclient.execute(httppost);HttpEntity entity = response.getEntity();//is = entity.getContent();httpclient.getConnectionManager().shutdown();}捕獲(異常 e){//CommonFunctions.writeLOG(ctx.getClass().toString(), e.toString());//CommonFunctions.showToast(ctx, "無法發布捕獲的圖像文件:" +//e.toString());}}
看起來你的 PHP 是正確的.
在您的設備上使用帶有 MultipartEntity
數據類型的 HTTP POST 請求.閱讀更多此處
編輯
來自我的鏈接的示例:
您必須下載額外的庫才能運行 MultipartEntity
!
1) 從 http://james.apache 下載 httpcomponents-client-4.1.zip.org/download.cgi#Apache_Mime4J 并將 apache-mime4j-0.6.1.jar 添加到您的項目中.
2) 從 http://hc.apache 下載 httpcomponents-client-4.1-bin.zip.org/downloads.cgi 并將 httpclient-4.1.jar、httpcore-4.1.jar 和 httpmime-4.1.jar 添加到您的項目中.
3) 使用下面的示例代碼.
<前><代碼>私有 DefaultHttpClient mHttpClient;公共服務器通信(){HttpParams params = new BasicHttpParams();params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);mHttpClient = new DefaultHttpClient(params);}public void uploadUserPhoto(File image1, File image2, File image3) {嘗試 {HttpPost httppost = new HttpPost("some url");MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);multipartEntity.addPart("Title", new StringBody("Title"));multipartEntity.addPart("Nick", new StringBody("Nick"));multipartEntity.addPart("Email", new StringBody("Email"));multipartEntity.addPart("Description", new StringBody(Settings.SHARE.TEXT));multipartEntity.addPart("file1", new FileBody(image1));multipartEntity.addPart("file2", new FileBody(image2));multipartEntity.addPart("file3", new FileBody(image3));httppost.setEntity(multipartEntity);mHttpClient.execute(httppost, new PhotoUploadResponseHandler());} 捕獲(異常 e){Log.e(ServerCommunication.class.getName(), e.getLocalizedMessage(), e);}}私有類 PhotoUploadResponseHandler 實現了 ResponseHandler {@覆蓋公共對象 handleResponse(HttpResponse 響應)拋出 ClientProtocolException,IOException {HttpEntity r_entity = response.getEntity();String responseString = EntityUtils.toString(r_entity);Log.d("上傳", responseString);返回空;}}Help for this specific server side php code I don't have any knowledge of php and I have to upload three images from android to this php page.
I have tried many methods and searched for it but no tutorial or anything didn't help me my android code is working properly. DNS also configured but images aren't shown at the server side. Please help me with java code.
PHP:
<?php
if ($_FILES["file1"]["error"] > 0)
{
header("HTTP/1.1 400 Bad Request");
echo "Error: " . $_FILES["file1"]["error"] . "<br />";
}
else if ($_FILES["file2"]["error"] > 0)
{
header("HTTP/1.1 400 Bad Request");
echo "Error: " . $_FILES["file1"]["error"] . "<br />";
}
else if ($_FILES["file3"]["error"] > 0)
{
header("HTTP/1.1 400 Bad Request");
echo "Error: " . $_FILES["file1"]["error"] . "<br />";
}
else
{
if ($_FILES["file1"]["error"] > 0)
{
echo "Error: " . $_FILES["file1"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file1"]["name"] . "<br />";
echo "Type: " . $_FILES["file1"]["type"] . "<br />";
echo "Size: " . ($_FILES["file1"]["size"] / 1024) . " Kb<br />";
echo "Stored in: " . $_FILES["file1"]["tmp_name"]. "<br />";
}
//$target_path = "uploads/";
$target_path = "elp/pendingimages/";
$target_path = $target_path . basename( $_FILES['file1']['name']);
if(move_uploaded_file($_FILES['file1']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['file1']['name']).
" has been uploaded";
}
else{
echo "There was an error uploading the file, please try again!";
}
if ($_FILES["file2"]["error"] > 0)
{
echo "Error: " . $_FILES["file2"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file2"]["name"] . "<br />";
echo "Type: " . $_FILES["file2"]["type"] . "<br />";
echo "Size: " . ($_FILES["file2"]["size"] / 1024) . " Kb<br />";
echo "Stored in: " . $_FILES["file2"]["tmp_name"]. "<br />";
}
//$target_path = "uploads/";
$target_path = "elp/pendingimages/";
$target_path = $target_path . basename( $_FILES['file2']['name']);
if(move_uploaded_file($_FILES['file2']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['file2']['name']).
" has been uploaded";
}
else{
echo "There was an error uploading the file, please try again!";
}
if ($_FILES["file3"]["error"] > 0)
{
echo "Error: " . $_FILES["file3"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file3"]["name"] . "<br />";
echo "Type: " . $_FILES["file3"]["type"] . "<br />";
echo "Size: " . ($_FILES["file3"]["size"] / 1024) . " Kb<br />";
echo "Stored in: " . $_FILES["file3"]["tmp_name"]. "<br />";
}
//$target_path = "uploads/";
$target_path = "elp/pendingimages/";
$target_path = $target_path . basename( $_FILES['file3']['name']);
if(move_uploaded_file($_FILES['file3']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['file3']['name']).
" has been uploaded";
}
else{
echo "There was an error uploading the file, please try again!";
}
}
?>
Java:
public class TryprojectActivity extends Activity {
InputStream is;
int pic_count = 0;
Bitmap bitmap=null;
FileInputStream in1,in2,in3;
BufferedInputStream buf;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
try {
in1 = new FileInputStream("/sdcard/1.jpg");
}
catch (FileNotFoundException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
try {
in2 = new FileInputStream("/sdcard/2.jpg");
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
in3 = new FileInputStream("/sdcard/3.jpg");
}
catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Bitmap bitmapOrg1 = BitmapFactory.decodeStream(in1);
ByteArrayOutputStream bao1 = new ByteArrayOutputStream();
bitmapOrg1.compress(Bitmap.CompressFormat.JPEG, 90, bao1);
byte [] imagearray1 = bao1.toByteArray();
String ba1=Base64.encode(imagearray1);
Bitmap bitmapOrg2 = BitmapFactory.decodeStream(in2);
ByteArrayOutputStream bao2 = new ByteArrayOutputStream();
bitmapOrg2.compress(Bitmap.CompressFormat.JPEG, 90, bao2);
byte [] imagearray2 = bao2.toByteArray();
String ba2=Base64.encode(imagearray2);
Bitmap bitmapOrg3 = BitmapFactory.decodeStream(in3);
ByteArrayOutputStream bao3 = new ByteArrayOutputStream();
bitmapOrg3.compress(Bitmap.CompressFormat.JPEG, 90, bao3);
byte [] imagearray3 = bao3.toByteArray();
String ba3=Base64.encode(imagearray3);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("image1",ba1));
nameValuePairs.add(new BasicNameValuePair("image2",ba2));
nameValuePairs.add(new BasicNameValuePair("image3",ba3));
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://helpdesk.cispl.com/upload_file.php");
UrlEncodedFormEntity obj = new UrlEncodedFormEntity(nameValuePairs);
obj.setChunked(true);
httppost.setEntity(obj);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
//is = entity.getContent();
httpclient.getConnectionManager().shutdown();
}
catch(Exception e){
//CommonFunctions.writeLOG(ctx.getClass().toString(), e.toString());
//CommonFunctions.showToast(ctx, "Unable to post captured image file: " +
//e.toString());
}
}
Looks like your PHP is correct.
On your device use HTTP POST request with MultipartEntity
data type. Read more here
EDIT
Example from my link:
You will have to download additional libraries to get MultipartEntity
running!
1) Download httpcomponents-client-4.1.zip from http://james.apache.org/download.cgi#Apache_Mime4J and add apache-mime4j-0.6.1.jar to your project.
2) Download httpcomponents-client-4.1-bin.zip from http://hc.apache.org/downloads.cgi and add httpclient-4.1.jar, httpcore-4.1.jar and httpmime-4.1.jar to your project.
3) Use the example code below.
private DefaultHttpClient mHttpClient;
public ServerCommunication() {
HttpParams params = new BasicHttpParams();
params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
mHttpClient = new DefaultHttpClient(params);
}
public void uploadUserPhoto(File image1, File image2, File image3) {
try {
HttpPost httppost = new HttpPost("some url");
MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntity.addPart("Title", new StringBody("Title"));
multipartEntity.addPart("Nick", new StringBody("Nick"));
multipartEntity.addPart("Email", new StringBody("Email"));
multipartEntity.addPart("Description", new StringBody(Settings.SHARE.TEXT));
multipartEntity.addPart("file1", new FileBody(image1));
multipartEntity.addPart("file2", new FileBody(image2));
multipartEntity.addPart("file3", new FileBody(image3));
httppost.setEntity(multipartEntity);
mHttpClient.execute(httppost, new PhotoUploadResponseHandler());
} catch (Exception e) {
Log.e(ServerCommunication.class.getName(), e.getLocalizedMessage(), e);
}
}
private class PhotoUploadResponseHandler implements ResponseHandler {
@Override
public Object handleResponse(HttpResponse response)
throws ClientProtocolException, IOException {
HttpEntity r_entity = response.getEntity();
String responseString = EntityUtils.toString(r_entity);
Log.d("UPLOAD", responseString);
return null;
}
}
這篇關于從android在php服務器上上傳多個圖像的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!