[英]Upload file to server Android
I want to upload some mp3 files to my localhost server however I am not sure where I am going wrong. 我想将一些mp3文件上传到我的本地主机服务器,但是我不确定我要去哪里。 I think it is the
php
file, but here is my php
file: 我认为这是
php
文件,但这是我的php
文件:
<?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";
}
?>
This is my java
code: 这是我的
java
代码:
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() {
}
});
}
}
}
}
I got this code online and modified it for my purpose - but it is not working, but as I mentioned it could be the php
file 我将此代码在线获取并出于我的目的对其进行了修改-但它无法正常工作,但正如我所提到的,它可能是
php
文件
Any help will really mean a lot (even a fix for this will let me eat and sleep properly) 任何帮助都将真正起到很大的作用(即使对此进行修复,也可以让我正常饮食和睡眠)
Thanks 谢谢
Change your code with following php code it work perfect. 使用以下php代码更改您的代码,即可正常运行。
<?php
$file_path = "uploads/";
$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";
}
?>
I have tested it work perfect 我已经测试过它工作完美
I think you should try using json parser for this, set your PHP script to output json, to get the success and failures. 我认为您应该为此尝试使用json解析器,将您的PHP脚本设置为输出json,以获取成功和失败的信息。 You need to parse the image file name string from you java code to the web service, and get it to perform the operation.
您需要将Java代码中的图像文件名字符串解析为Web服务,并获取它来执行操作。 You can also use the $_GET to get your file from your java code
您还可以使用$ _GET从Java代码获取文件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.