gpt4 book ai didi

php - 如何将图片上传到 Php 服务器并存储在 phpmyadmin 中

转载 作者:可可西里 更新时间:2023-11-01 06:56:55 28 4
gpt4 key购买 nike

我基本上是想从 android 上传图片并将其上传到 php 服务器,但在这里我没有与此代码或图片上传有任何联系
。我收到此错误。

Error in http connection java.net.UnknownHostException: host name

但据我所知,我在正确的域中提供了正确的连接和 php 文件。看看我的代码:上传图片.java

public class UploadImage extends Activity {
InputStream inputStream;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);

Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.icon);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream); //compress to which format you want.
byte [] byte_arr = stream.toByteArray();
String image_str = Base64.encodeBytes(byte_arr);
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

nameValuePairs.add(new BasicNameValuePair("image",image_str));

try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://server.com/uploadimage/uploadimage.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
String the_string_response = convertResponseToString(response);
Toast.makeText(UploadImage.this, "Response " + the_string_response, Toast.LENGTH_LONG).show();
}catch(Exception e){
Toast.makeText(UploadImage.this, "ERROR " + e.getMessage(), Toast.LENGTH_LONG).show();
System.out.println("Error in http connection "+e.toString());
}
}

public String convertResponseToString(HttpResponse response) throws IllegalStateException, IOException{

String res = "";
StringBuffer buffer = new StringBuffer();
inputStream = response.getEntity().getContent();
int contentLength = (int) response.getEntity().getContentLength(); //getting content length…..
Toast.makeText(UploadImage.this, "contentLength : " + contentLength, Toast.LENGTH_LONG).show();
if (contentLength < 0){
}
else{
byte[] data = new byte[512];
int len = 0;
try
{
while (-1 != (len = inputStream.read(data)) )
{
buffer.append(new String(data, 0, len)); //converting to string and appending to stringbuffer…..
}
}
catch (IOException e)
{
e.printStackTrace();
}
try
{
inputStream.close(); // closing the stream…..
}
catch (IOException e)
{
e.printStackTrace();
}
res = buffer.toString(); // converting stringbuffer to string…..

Toast.makeText(UploadImage.this, "Result : " + res, Toast.LENGTH_LONG).show();
//System.out.println("Response => " + EntityUtils.toString(response.getEntity()));
}
return res;
}

PHP 代码:

<?php
$base=$_REQUEST['image'];
$binary=base64_decode($base);
header('Content-Type: bitmap; charset=utf-8');
$file = fopen('uploaded_image.jpg', 'wb');
fwrite($file, $binary);
fclose($file);
echo 'Image upload complete!!, Please check your php file directory……';?>

有人知道这个问题吗?如果有人知道如何从 php 文件存储在 mysql 数据库中并获取反之亦然,请在这里建议我...

最佳答案

问题很清楚...

Error in http connection java.net.UnknownHostException: host name

表示 HttpPost 无法使用您提供的主机名建立连接 - 因为您提供的主机名未知。

如果您从此行获取主机名:

HttpPost httppost = new HttpPost("http://server.com/uploadimage/uploadimage.php");

并将其放入同一设备上的浏览器中会发生什么......我建议你会收到一条错误消息,提示无法连接到主机。如果这有效,那么我建议您检查 list 中的以下行:

<uses-permission android:name="android.permission.INTERNET" />

如果您使用的是 JPEG,还要确保 PHP 文件包含以下 header :

header('Content-Type: image/jpg');

关于php - 如何将图片上传到 Php 服务器并存储在 phpmyadmin 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9564538/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com