Bazı mp3 dosyalarını localhost sunucusuna yüklemek istiyorum ancak nereye gideceğimden emin değilim. Bu benim java
kodDosyaya sunucu gönder Android
<?php
$file_path = "/storage/emulated/0/sample.txt";
$file_path = $file_path . basename($_FILES['uploaded_file']['name']);
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) {
echo "success";
} else{
echo "fail";
}
?>
geçerli:: Ben php
dosyasıdır, ama burada benim php
dosya olduğunu düşünüyorum
public class UploadFile extends Activity {
int serverResponseCode = 0;
String serverResponseMessage;
String uploadFileName;
String uploadFilePath;
String upLoadServerUri = null;
ProgressBar loading;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.uploads);
uploadFilePath = "/storage/emulated/0/";
uploadFileName = "sample.txt";
upLoadServerUri = "http://10.0.3.2/files/uploadfile.php";
new Thread(new Runnable() {
public void run() {
uploadFile(uploadFilePath + "" + uploadFileName);
}
}).start();
}
public void uploadFile(String sourceFileUri) {
String fileName = sourceFileUri;
HttpURLConnection conn = null;
DataOutputStream dos = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int code;
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
File sourceFile = new File(sourceFileUri);
if (!sourceFile.isFile()) {
code = 0;
this.runOnUiThread(new Runnable() {
public void run() {
}
});
}
else {
try {
FileInputStream fileInputStream = new FileInputStream(sourceFile);
URL url = new URL(upLoadServerUri);
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true); // Allow Inputs
conn.setDoOutput(true); // Allow Outputs
conn.setUseCaches(false); // Don't use a Cached Copy
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("ENCTYPE", "multipart/form-data");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
conn.setRequestProperty("uploaded_file", fileName);
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name='uploaded_file';filename='"+fileName+"'"+lineEnd);
dos.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
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);
}
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
serverResponseCode = conn.getResponseCode();
code = serverResponseCode;
if (code==200) {
this.runOnUiThread(new Runnable() {
public void run() {
}
});
}
else {
this.runOnUiThread(new Runnable() {
public void run() {
}
});
}
serverResponseMessage = conn.getResponseMessage();
fileInputStream.close();
dos.flush();
dos.close();
}
catch (MalformedURLException ex) {
this.runOnUiThread(new Runnable() {
public void run() {
}
});
}
catch (Exception e) {
this.runOnUiThread(new Runnable() {
public void run() {
}
});
}
}
}
}
İnternetten kod var ve benim bir amaç için modifiye - ama öyle çalışma, ama belirtildiği gibi herhangi bir yardım gerçekten çok şey ifade edecektir
php
dosya olabilir değil
Teşekkürler
Sadece çalışmadığını bize söyleme .. aslında bunun yerine ne olduğunu söyleyin. Bu sorunu düzeltmek için ne denediniz? Bunu yaptığınızda ne oldu? –
Onun php dosyası. Bence hiçbir şey yapmıyor. Dosyayı php dosyasına nasıl gönderirim? – Sandezonpr
*** Biz sihirbaz değiliz! *** Kodunuzda neyin yanlış olduğunu gerçekten anlayamıyorum ve ne kadar hata ayıklamanın berbat olduğunu biliyoruz. Kodunuzun çoğunu yorumlamaya başlayın ve bir yanıt alıp alamayacağınızı görmek için sunucunuzu 'ping 'yapın. Ardından dosya yüklemeye doğru ilerleyin. –