问题描述
帮助这个特定的服务器端的PHP code,我没有PHP的任何知识,我不得不从Android的三个图像上传到这个PHP页面。
我试过很多方法和搜索,但没有教程或任何东西对我没有帮助我的Android code正常工作。的DNS还配置,但是图像没有在服务器侧所示。请帮我用java code。
PHP:
< PHP
如果($ _FILES [文件1] [错误] 0)
{
标题(HTTP / 1.1 400错误的请求);
回声错误:。 $ _FILES [文件1] [错误。 < BR />中;
}
否则,如果($ _FILES [文件2] [错误] 0)
{
标题(HTTP / 1.1 400错误的请求);
回声错误:。 $ _FILES [文件1] [错误。 < BR />中;
}
否则,如果($ _FILES [文件3] [错误] 0)
{
标题(HTTP / 1.1 400错误的请求);
回声错误:。 $ _FILES [文件1] [错误。 < BR />中;
}
其他
{
如果($ _FILES [文件1] [错误] 0)
{
回声错误:。 $ _FILES [文件1] [错误。 < BR />中;
}
其他
{
回声上载:。 $ _FILES [文件1] [名称]。 < BR />中;
回声类型:。 $ _FILES [文件1] [型。 < BR />中;
回声大小。 ($ _FILES [文件1] [大小] / 1024)。 KB< BR />中;
回声存储位置:。 $ _FILES [文件1] [tmp_name的值。 < BR />中;
}
// $ target_path =上传/;
$ target_path =ELP / pendingimages /;
$ target_path = $ target_path。基本名($ _ FILES ['文件1'] ['名称']);
如果(move_uploaded_file($ _ FILES ['文件1'] ['tmp_name的值'],$ target_path)){
回声文件。基本名($ _ FILES ['文件1'] ['名称'])。
已上载;
}
其他{
回声发生错误上传文件,请重试!;
}
如果($ _FILES [文件2] [错误] 0)
{
回声错误:。 $ _FILES [文件2] [错误。 < BR />中;
}
其他
{
回声上载:。 $ _FILES [文件2] [名称]。 < BR />中;
回声类型:。 $ _FILES [文件2] [型。 < BR />中;
回声大小。 ($ _FILES [文件2] [大小] / 1024)。 KB< BR />中;
回声存储位置:。 $ _FILES [文件2] [tmp_name的值。 < BR />中;
}
// $ target_path =上传/;
$ target_path =ELP / pendingimages /;
$ target_path = $ target_path。基本名($ _ FILES ['文件2'] ['名称']);
如果(move_uploaded_file($ _ FILES ['文件2'] ['tmp_name的值'],$ target_path)){
回声文件。基本名($ _ FILES ['文件2'] ['名称'])。
已上载;
}
其他{
回声发生错误上传文件,请重试!;
}
如果($ _FILES [文件3] [错误] 0)
{
回声错误:。 $ _FILES [文件3] [错误。 < BR />中;
}
其他
{
回声上载:。 $ _FILES [文件3] [名称]。 < BR />中;
回声类型:。 $ _FILES [文件3] [型。 < BR />中;
回声大小。 ($ _FILES [文件3] [大小] / 1024)。 KB< BR />中;
回声存储位置:。 $ _FILES [文件3] [tmp_name的值。 < BR />中;
}
// $ target_path =上传/;
$ target_path =ELP / pendingimages /;
$ target_path = $ target_path。基本名($ _ FILES ['文件3'] ['名称']);
如果(move_uploaded_file($ _ FILES ['文件3'] ['tmp_name的值'],$ target_path)){
回声文件。基本名($ _ FILES ['文件3'] ['名称'])。
已上载;
}
其他{
回声发生错误上传文件,请重试!;
}
}
?>
Java的:
公共类TryprojectActivity延伸活动{
InputStream的是;
INT pic_count = 0;
点阵位图= NULL;
的FileInputStream IN1,IN2,IN3;
的BufferedInputStream BUF;
@覆盖
公共无效的onCreate(包冰柱){
super.onCreate(冰柱);
的setContentView(R.layout.main);
尝试 {
IN1 =新的FileInputStream(/ SD卡/ 1.JPG);
}
赶上(FileNotFoundException异常E2){
// TODO自动生成的catch块
e2.printStackTrace();
}
尝试 {
IN2 =新的FileInputStream(/ SD卡/ 2.JPG);
}赶上(FileNotFoundException异常E1){
// TODO自动生成的catch块
e1.printStackTrace();
}
尝试 {
IN3 =新的FileInputStream(/ SD卡/ 3.JPG);
}
赶上(FileNotFoundException异常E1){
// TODO自动生成的catch块
e1.printStackTrace();
}
位图bitmapOrg1 = BitmapFactory.de codeStream(IN1);
ByteArrayOutputStream bao1 =新ByteArrayOutputStream();
bitmapOrg1.com preSS(Bitmap.Com pressFormat.JPEG,90,bao1);
byte []的imagearray1 = bao1.toByteArray();
字符串BA1 = Base64.en code(imagearray1);
位图bitmapOrg2 = BitmapFactory.de codeStream(平方英寸);
ByteArrayOutputStream bao2 =新ByteArrayOutputStream();
bitmapOrg2.com preSS(Bitmap.Com pressFormat.JPEG,90,bao2);
byte []的imagearray2 = bao2.toByteArray();
字符串BA2 = Base64.en code(imagearray2);
位图bitmapOrg3 = BitmapFactory.de codeStream(立方英寸);
ByteArrayOutputStream bao3 =新ByteArrayOutputStream();
bitmapOrg3.com preSS(Bitmap.Com pressFormat.JPEG,90,bao3);
byte []的imagearray3 = bao3.toByteArray();
字符串BA3 = Base64.en code(imagearray3);
名单<的NameValuePair> namevaluepairs中=新的ArrayList<的NameValuePair>(3);
nameValuePairs.add(新BasicNameValuePair(image1的,BA1));
nameValuePairs.add(新BasicNameValuePair(图像2,BA2));
nameValuePairs.add(新BasicNameValuePair(的Image3,BA3));
尝试{
HttpClient的HttpClient的=新DefaultHttpClient();
HttpPost httppost =新HttpPost(http://helpdesk.cispl.com/upload_file.php);
UrlEn codedFormEntity的obj =新UrlEn codedFormEntity(namevaluepairs中);
obj.setChunked(真正的);
httppost.setEntity(OBJ);
HTT presponse响应= httpclient.execute(httppost);
HttpEntity实体= response.getEntity();
//是= entity.getContent();
。httpclient.getConnectionManager()关闭();
}
赶上(例外五){
。//CommonFunctions.writeLOG(ctx.getClass()的toString(),e.toString());
//CommonFunctions.showToast(ctx,无法投递捕获的图像文件:+
//e.toString());
}
}
看起来你的PHP是正确的。
在与 MultipartEntity
数据类型的设备使用HTTP POST请求。阅读更多here
修改
这是我的链接举例:
您将需要下载额外的库来获得 MultipartEntity
运行!
1)从 http://james.apache.org下载httpcomponents-client-4.1.zip /download.cgi#Apache_Mime4J 并加入Apache的mime4j-0.6.1.jar到您的项目。
2)从 http://hc.apache.org下载httpcomponents-client-4.1-bin.zip /downloads.cgi 并添加HttpClient的-4.1.jar,的HttpCore-4.1.jar和httpmime-4.1.jar到您的项目。
3)使用下面的例子中code。
私人DefaultHttpClient mHttpClient;
公共ServerCommunication(){
的HttpParams PARAMS =新BasicHttpParams();
params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION,HttpVersion.HTTP_1_1);
mHttpClient =新DefaultHttpClient(PARAMS);
}
公共无效uploadUserPhoto(文件此搜索,文件图像2,文件的Image3){
尝试 {
HttpPost httppost =新HttpPost(某些URL);
MultipartEntity multipartEntity =新MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntity.addPart(标题,新StringBody(标题));
multipartEntity.addPart(尼克,新StringBody(尼克));
multipartEntity.addPart(电子邮件,新StringBody(电子邮件));
multipartEntity.addPart(说明,新StringBody(Settings.SHARE.TEXT));
multipartEntity.addPart(文件1,新FileBody(此搜索));
multipartEntity.addPart(文件2,新FileBody(IMAGE2));
multipartEntity.addPart(文件3,新FileBody(的Image3));
httppost.setEntity(multipartEntity);
mHttpClient.execute(httppost,新PhotoUploadResponseHandler());
}赶上(例外五){
Log.e(ServerCommunication.class.getName(),e.getLocalizedMessage(),E);
}
}
私有类PhotoUploadResponseHandler实现ResponseHandler所{
@覆盖
公共对象用handleResponse(Htt的presponse响应)
抛出ClientProtocolException,IOException异常{
HttpEntity r_entity = response.getEntity();
字符串responseString = EntityUtils.toString(r_entity);
Log.d(上传,responseString);
返回null;
}
}
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的服务器上上传多个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!