خرید بک لینک

من برنامه زیر رو نوشتم که عکس ها رو بفرسته به سرور
می خوام در متغیر k ادرس پوشه بهش بدم
این k رو به فایل رابط پی اچ پی می فرستم اما وقتی برنامه ران می شه عکس به پوشه نمی فرسته توی همون محل اولیه ریخته می شه(زیاد پی اچ پی بلد نیستم فکر کنم مشکل از اینحا باشه یا توی قسمتی که کانکشن ساختم اشتباه کردم)

package com.example.uploadtoserver2;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLCoection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLCoection;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.aotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds. Im;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class Main extends Activity {
public static String picturePath="";
private String[] name;
TextView messageText;
Button button,btn1;
Button uploadButton;
private CheckBox ch1, ch2, ch3;
int serverResponseCode2 = 0;
ProgressDialog dialog = null;
private static final String TAG = null;
private static int RESULT_LOAD_IMAGE = 1;
String k="uploads/";
String upLoadServerUri = null;

/********** File Path *************/
public static String uploadFilePath = "";

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

addListenerOnButton();
button =(Button)findViewById(R.id.button1);

uploadButton = (Button)findViewById(R.id.button2);
messageText = (TextView)findViewById(R.id.textView1);

messageText.setText(uploadFilePath+"'");

/************* Php script path ****************/
upLoadServerUri = "http://udp.primashop.ir/images/up1.php";

button.setOnClickListener(new OnClickListener() {

public void onClick(View arg0) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_ CONTENT_URI);

startActivityForResult(i, RESULT_LOAD_IMAGE);

}

}
);

uploadButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {

dialog = ProgressDialog.show(Main.this, "", "Uploading file...", true);

new Thread(new Ruable() {
public void run() {
runOnUiThread(new Ruable() {
public void run() {
messageText.setText("uploading started.....");
}
});

uploadFile(uploadFilePath + "" );

}
}).start();
}
});
}

public void addListenerOnButton() {

ch1 = (CheckBox) findViewById(R.id.checkBox1);
ch2 = (CheckBox) findViewById(R.id.checkBox2);
ch3 = (CheckBox) findViewById(R.id.checkBox3);
btn1 = (Button) findViewById(R.id.button3);

btn1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

StringBuffer result = new StringBuffer();
result.append("real")
.append(ch1.isChecked());
result.append("barca").append(
ch2.isChecked());
result.append("juve").append(
ch3.isChecked());

}
});

}

public int uploadFile(String sourceFileUri) {

String fileName = sourceFileUri;

HttpURLCoection co = null;
DataOutputStream dos = null;
String lineEnd = "";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
File sourceFile = new File(sourceFileUri);

if (!sourceFile.isFile()) {

dialog.dismiss();

Log.e("uploadFile", "Source File not exist :"
+uploadFilePath + "" );

runOnUiThread(new Ruable() {
public void run() {
messageText.setText("Source File not exist :"
+uploadFilePath + "");
}
});

retu 0;

}
else
{
try {

String data=URLEncoder.encode("fileName","UTF8")+"="+URLE ncoder.encode(k,"UTF8");
FileInputStream fileInputStream = new FileInputStream(sourceFile);
URL url = new URL(upLoadServerUri);

// Open a HTTP coection to the URL
co = (HttpURLCoection) url.openCoection();
co.setDoInput(true); // Allow Inputs
co.setDoOutput(true); // Allow Outputs
co.setUseCaches(false); // Don't use a Cached Copy

co.setRequestMethod("POST");
co.setRequestProperty("Coection", "Keep-Alive");
co.setRequestProperty("ENCTYPE", "multipart/form-data");
co.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
co.setRequestProperty("uploaded_file", fileName);
dos = new DataOutputStream(co.getOutputStream());
OutputStreamWriter wr = new OutputStreamWriter(co.getOutputStream());

wr.write(data);
wr.flush();

dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name="uploaded_file";filename=""
+ fileName + """ + lineEnd);

dos.writeBytes(lineEnd);

// create a buffer of maximum size
bytesAvailable = fileInputStream.available();

bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];

// read file and write it into form...
bytesRead = fileInputStream.read(buffer, 0, bufferSize);

while (bytesRead > 0) {

dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);

}

// send multipart form data necesssary after file data...
dos.writeBytes(lineEnd);

dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

// Responses from the server (code and message)
serverResponseCode2 = co.getResponseCode();
String serverResponseMessage = co.getResponseMessage();

Log.i("uploadFile", "HTTP Response is : "
+ serverResponseMessage + ": " + serverResponseCode2);

if(serverResponseCode2 == 200){

runOnUiThread(new Ruable() {
public void run() {

String msg = "File Upload Completed. See uploaded file here : "

;

messageText.setText(msg);
Toast.makeText(Main.this, "File Upload Complete.",
Toast.LENGTH_SHORT).show();
}
});
}

//close the streams //
fileInputStream.close();
dos.flush();
dos.close();

} catch (MalformedURLException ex) {

dialog.dismiss();
ex.printStackTrace();

runOnUiThread(new Ruable() {
public void run() {
messageText.setText("MalformedURLException Exception : check script url.");
Toast.makeText(Main.this, "MalformedURLException", Toast.LENGTH_SHORT).show();
}
});

Log.e("Upload file to server", "error: " + ex.getMessage(), ex);
} catch (Exception e) {

dialog.dismiss();
e.printStackTrace();

runOnUiThread(new Ruable() {
public void run() {
messageText.setText("Got Exception : see logcat ");
Toast.makeText(Main.this, "Got Exception : see logcat ",
Toast.LENGTH_SHORT).show();
}
});
Log.e("Upload file to server Exception", "Exception : "
+ e.getMessage(), e);
}
dialog.dismiss();
retu serverResponseCode2;

} // End else block

}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };

Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();

int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
uploadFilePath=picturePath;

}

}

}

برچسب: نویسنده: محمد رضا جوادیان تاريخ: دوشنبه 24 اسفند 1394 ساعت: 4:44

صفحه بندی