使用httpurlconnection发送图片
Send picture with httpurlconnection
您好,我正在制作一个将图片发送到服务器的应用程序。具有 Apache 弃用功能的版本可以工作,但我不知道为什么我无法使更新的解决方案工作。有人知道这里的错误在哪里吗?
最新解决方案:它不会在 logcat 中给出错误,但是当我转到服务器时,没有任何内容被上传。起初我以为错误在于我如何传递参数,但我尝试了几种不同的解决方案,例如使用 Uri.builder,使用 HashMap 和 stringBuilder 对参数进行编码的方法,像这样传递字符串......没有任何效果。我需要帮助这真的让我发疯
@Override
protected Void doInBackground(Void... params) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
String encodedImage = Base64.encodeToString(byteArrayOutputStream.toByteArray(), Base64.DEFAULT);
try {
byteArrayOutputStream.flush();
byteArrayOutputStream.close();
}catch (IOException e){
}
HttpURLConnection connection;
try {
String urlSt = "http://phoenixcoding.tk/SavePicture.php";
URL url = new URL(urlSt);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
/*Uri.Builder builder = new Uri.Builder()
.appendQueryParameter("name", name)
.appendQueryParameter("image", encodedImage);
String query = builder.build().getEncodedQuery();*/
OutputStream os = connection.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write("name=example&image=" + encodedImage);
writer.flush();
writer.close();
os.close();
connection.connect();
}catch (IOException e){
e.printStackTrace();
}
return null;
}
以前的解决方案:效果很好
@Override
protected Void doInBackground(Void... params) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
String encodedImage = Base64.encodeToString(byteArrayOutputStream.toByteArray(), Base64.DEFAULT);
try {
byteArrayOutputStream.flush();
byteArrayOutputStream.close();
}catch (IOException e){
}ArrayList<NameValuePair> dataToSend = new ArrayList<NameValuePair>();
dataToSend.add(new BasicNameValuePair("name", name));
dataToSend.add(new BasicNameValuePair("image", encodedImage));
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://phoenixcoding.tk/SavePicture.php");
try{
post.setEntity(new UrlEncodedFormEntity(dataToSend));
client.execute(post);
}catch (Exception e){
e.printStackTrace();
}
return null;
}
SavePhoto.php 文件:
<?php
$name = $_POST["name"];
$image = $_POST["image"];
$decodedImage = base64_decode("$image");
file_put_contents("pictures/" . $name . ".JPG", $decodedImage);
?>
在您的 php 代码中试试这个:
if( isset($_POST["image"]) && !empty($_POST["image"])){
$profile_pic = '';
$data= $_POST['image'];
$data = str_replace('data:image/png;base64,', '', $data);
$data = str_replace(' ', '+', $data);
$binary=base64_decode($data);
header('Content-Type: bitmap; charset=utf-8');
// Images will be saved under 'www/pictures' folder
$new_name = $_POST['name'] . '.png';
$success =file_put_contents('pictures/'.$new_name,$binary);
$profile_pic = $new_name;
}
我猜这行有问题 :- $decodedImage = base64_decode("$image");
你必须这样写 $decodedImage = base64_decode($image);
要调试,请执行此操作:-
<?php
file_put_contents("post.txt",print_r($_POST,true));
$name = $_POST["name"];
.....
?>
认为:- http://phoenixcoding.tk/post.txt
( 如果文件未保存,那么在这种情况下存在权限问题创建一个目录 "test" 并赋予它权限 755 即使它不起作用将该目录设置为 777 然后你 url 将是 http://phoenixcoding.tk/test/post.txt )
您要做的是将所有传入的 $_POST
收集到文件中,然后您将知道收到的 post 数据,这将阐明错误的位置,在 android 方面或 php 方面,如果 post 没问题,那么 android 代码没问题,问题出在 php 代码中。
我希望它能帮助您解决问题...
谢谢
感谢大家的回答,我终于成功了。我不知道这是为什么,但是在打开连接后添加 InputStream 对象后,图片上传正确。我所做的就是添加这些行:
InputStream is = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder str = new StringBuilder();
String strLine;
while ((strLine = reader.readLine()) != null) {
str.append(strLine + "\n");
}
is.close();
您好,我正在制作一个将图片发送到服务器的应用程序。具有 Apache 弃用功能的版本可以工作,但我不知道为什么我无法使更新的解决方案工作。有人知道这里的错误在哪里吗?
最新解决方案:它不会在 logcat 中给出错误,但是当我转到服务器时,没有任何内容被上传。起初我以为错误在于我如何传递参数,但我尝试了几种不同的解决方案,例如使用 Uri.builder,使用 HashMap 和 stringBuilder 对参数进行编码的方法,像这样传递字符串......没有任何效果。我需要帮助这真的让我发疯
@Override
protected Void doInBackground(Void... params) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
String encodedImage = Base64.encodeToString(byteArrayOutputStream.toByteArray(), Base64.DEFAULT);
try {
byteArrayOutputStream.flush();
byteArrayOutputStream.close();
}catch (IOException e){
}
HttpURLConnection connection;
try {
String urlSt = "http://phoenixcoding.tk/SavePicture.php";
URL url = new URL(urlSt);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
/*Uri.Builder builder = new Uri.Builder()
.appendQueryParameter("name", name)
.appendQueryParameter("image", encodedImage);
String query = builder.build().getEncodedQuery();*/
OutputStream os = connection.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write("name=example&image=" + encodedImage);
writer.flush();
writer.close();
os.close();
connection.connect();
}catch (IOException e){
e.printStackTrace();
}
return null;
}
以前的解决方案:效果很好
@Override
protected Void doInBackground(Void... params) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
String encodedImage = Base64.encodeToString(byteArrayOutputStream.toByteArray(), Base64.DEFAULT);
try {
byteArrayOutputStream.flush();
byteArrayOutputStream.close();
}catch (IOException e){
}ArrayList<NameValuePair> dataToSend = new ArrayList<NameValuePair>();
dataToSend.add(new BasicNameValuePair("name", name));
dataToSend.add(new BasicNameValuePair("image", encodedImage));
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://phoenixcoding.tk/SavePicture.php");
try{
post.setEntity(new UrlEncodedFormEntity(dataToSend));
client.execute(post);
}catch (Exception e){
e.printStackTrace();
}
return null;
}
SavePhoto.php 文件:
<?php
$name = $_POST["name"];
$image = $_POST["image"];
$decodedImage = base64_decode("$image");
file_put_contents("pictures/" . $name . ".JPG", $decodedImage);
?>
在您的 php 代码中试试这个:
if( isset($_POST["image"]) && !empty($_POST["image"])){
$profile_pic = '';
$data= $_POST['image'];
$data = str_replace('data:image/png;base64,', '', $data);
$data = str_replace(' ', '+', $data);
$binary=base64_decode($data);
header('Content-Type: bitmap; charset=utf-8');
// Images will be saved under 'www/pictures' folder
$new_name = $_POST['name'] . '.png';
$success =file_put_contents('pictures/'.$new_name,$binary);
$profile_pic = $new_name;
}
我猜这行有问题 :- $decodedImage = base64_decode("$image");
你必须这样写 $decodedImage = base64_decode($image);
要调试,请执行此操作:-
<?php
file_put_contents("post.txt",print_r($_POST,true));
$name = $_POST["name"];
.....
?>
认为:- http://phoenixcoding.tk/post.txt
( 如果文件未保存,那么在这种情况下存在权限问题创建一个目录 "test" 并赋予它权限 755 即使它不起作用将该目录设置为 777 然后你 url 将是 http://phoenixcoding.tk/test/post.txt )
您要做的是将所有传入的 $_POST
收集到文件中,然后您将知道收到的 post 数据,这将阐明错误的位置,在 android 方面或 php 方面,如果 post 没问题,那么 android 代码没问题,问题出在 php 代码中。
我希望它能帮助您解决问题...
谢谢
感谢大家的回答,我终于成功了。我不知道这是为什么,但是在打开连接后添加 InputStream 对象后,图片上传正确。我所做的就是添加这些行:
InputStream is = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder str = new StringBuilder();
String strLine;
while ((strLine = reader.readLine()) != null) {
str.append(strLine + "\n");
}
is.close();