我正在使用android并尝试创建一个能够将多个图像上传到服务器的应用程序。我曾尝试使用xampp将图像上传到我的localhost,效果很好。但是,当我尝试上传到我的企业服务器时,我找不到我的文件,换句话说。该文件无法写入。我不知道是什么让它失败了?这是我的代码
上传tp XAMPP
连接字符串private static final String url_photo = "http://192.168.7.110/blabla/base.php";
路径static final String path = "C:\\xampp\\htdocs\\psn_asset_oracle\\Images\\";
上传到实际的企业服务器
连接字符串private static final String url_photo = "http://192.168.4.27/oracle/logam/am/data_images/android_image/base.php";
路径static final String path = "http://192.168.4.27/oracle/logam/am/data_images/android_image/";
我上传到服务器的代码
params_p.add(new BasicNameValuePair("image_name_1",
image_name_1));
params_p.add(new BasicNameValuePair("image_name_2",
image_name_2));
params_p.add(new BasicNameValuePair("image_name_3",
image_name_3));
params_p.add(new BasicNameValuePair("image_name_4",
image_name_4));
json_photo = jsonParser.makeHttpRequest(url_photo, "POST", params_p);
ArrayList<NameValuePair> params_p = new ArrayList<NameValuePair>();
PHP代码
if(isset($_POST["image_name_1"]) && isset($_POST["image_name_2"]) && isset($_POST["image_name_3"]) && isset($_POST["image_name_4"])
&& isset($_POST["image_1"]) && isset($_POST["image_2"]) && isset($_POST["image_3"]) && isset($_POST["image_4"]))
{
$image_name_1 = $_POST["image_name_1"];
$image_name_2 = $_POST["image_name_2"];
$image_name_3 = $_POST["image_name_3"];
$image_name_4 = $_POST["image_name_4"];
$image_1 = $_POST["image_1"];
$image_2 = $_POST["image_2"];
$image_3 = $_POST["image_3"];
$image_4 = $_POST["image_4"];
/*---------base64 decoding utf-8 string-----------*/
$binary_1=base64_decode($image_1);
$binary_2=base64_decode($image_2);
$binary_3=base64_decode($image_3);
$binary_4=base64_decode($image_4);
/*-----------set binary, utf-8 bytes----------*/
header('Content-Type: bitmap; charset=utf-8');
/*---------------open specified directory and put image on it------------------*/
$file_1 = fopen($image_name_1, 'wb');
$file_2 = fopen($image_name_2, 'wb');
$file_3 = fopen($image_name_3, 'wb');
$file_4 = fopen($image_name_4, 'wb');
/*---------------------assign image to file system-----------------------------*/
fwrite($file_1, $binary_1);
fclose($file_1);
fwrite($file_2, $binary_2);
fclose($file_2);
fwrite($file_3, $binary_3);
fclose($file_3);
fwrite($file_4, $binary_4);
fclose($file_4);
$response["message"] = "Success";
echo json_encode($response);
}
我已经联系了我的DBA,并要求我允许编写该文件,但它仍然无效。错误是json没有将“Success”作为指示文件写入失败的消息。我将不胜感激任何帮助。谢谢。
答案 0 :(得分:0)
文件服务器是否允许写访问权限? chmod的示例,http://catcode.com/teachmod/
您的企业服务器是否在线? IP地址对我来说是私密的,192.168.4.27。
答案 1 :(得分:0)
不要使用json使用http post。
见下面的代码
HttpClient client = new DefaultHttpClient();
String postURL = "your url";
HttpPost post = new HttpPost(postURL);
try {
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
ByteArrayBody bab = new ByteArrayBody(img, "image.jpg");
reqEntity.addPart("image", bab);
post.setEntity(reqEntity);
HttpResponse response = client.execute(post);
这里img是ByteArray格式的图像
答案 2 :(得分:0)
在我的DBA给我另一个文件路径后问题解决了。