嗨,我正在使用android。我創建了一個將文件上傳到服務器的Android應用程序。我用下面的代碼Android文件上傳不起作用
但我不能上傳我的文件。該應用程序顯示上傳開始,但沒有文件上傳到我的服務器位置。
嗨,我正在使用android。我創建了一個將文件上傳到服務器的Android應用程序。我用下面的代碼Android文件上傳不起作用
但我不能上傳我的文件。該應用程序顯示上傳開始,但沒有文件上傳到我的服務器位置。
private class SendHttpRequestTask extends AsyncTask<String, Void, String> {
protected String doInBackground(String... params) {
String url = params[0];
String param1 = params[1];
String param2 = params[2];
Bitmap b = BitmapFactory.decodeResource(UploadActivity.this.getResources(), R.drawable.logo);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
b.compress(CompressFormat.PNG, 0, baos);
try {
HttpClient client = new HttpClient(url);
client.connectForMultipart();
client.addFormPart("param1", param1);
client.addFormPart("param2", param2);
client.addFilePart("file", "logo.png", baos.toByteArray());
client.finishMultipart();
String data = client.getResponse();
}
catch(Throwable t) {
t.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String data) {
item.setActionView(null);
}
}
public class save extends AsyncTask私有MyProgressDialog pDialog;
@Override
protected void onPreExecute() {
pDialog = new MyProgressDialog(Send_message.this);
pDialog.setCancelable(false);
pDialog.show();
super.onPreExecute();
}
@Override
protected String doInBackground(String... paramss) {
try {
String json = messagesend.sendmeesage("Your Url",params);
JSONObject jsonobject = new JSONObject(json);
String result = jsonobject.getString("result");
if (result.equals("TRUE")) {
return "true";
}
System.out.println("json is" + json);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return "false";
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
pDialog.dismiss();
}
}
//發送文件 公共類messagesendwithhttp {
private String page;
public String sendmeesage(String url, List<NameValuePair> nameValuePairs)throws ClientProtocolException, IOException
{
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(url);
try {
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
for (int index = 0; index < nameValuePairs.size(); index++)
{
if (index == 0 || index == 1)
{
// If the key equals to "image", we use FileBody to transfer
// the data
entity.addPart(nameValuePairs.get(index).getName(), new StringBody(nameValuePairs.get(index).getValue()));
} else {
// Normal string data
if(nameValuePairs.get(index).getValue().equals("") || nameValuePairs.get(index).getValue().toString().equals(null))
{
entity.addPart(
nameValuePairs.get(index).getName(),
new StringBody(nameValuePairs.get(index).getValue()));
}
else
{
entity.addPart(nameValuePairs.get(index).getName(),
new FileBody(new File(nameValuePairs.get(index)
.getValue())));
}
}
}
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost, localContext);
HttpEntity resEntity = response.getEntity();
if (resEntity != null)
{
page = EntityUtils.toString(resEntity);
System.out.println("PAGE :" + page);
}
} catch (IOException e) {
e.printStackTrace();
}
return page;
}
}
enter code here
可以使用httpmime庫上傳任何文件(圖片,音頻,視頻等。 ) 請參考下面的代碼。
enter code here
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost postRequest = new HttpPost(your url);
MultipartEntity reqEntity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
try {
File file = new File(filename);
FileBody fileBody = new FileBody(file);
reqEntity.addPart("video_file", fileBody);
postRequest.setEntity(reqEntity);
HttpResponse response = httpClient.execute(postRequest,
localContext);
HttpEntity entity = response.getEntity();
if (entity != null) {
// entity.consumeContent();
BufferedReader rd = new BufferedReader(new InputStreamReader(
entity.getContent()));
// Log.e("response", "response " + rd.toString());
String line;
String result1 = "";
while ((line = rd.readLine()) != null) {
result1 = result1 + line;
// Log.e("result", "line " + line);
}
// Log.e("result", "is " + result1);
return result1;
}
} catch (Exception e) {
return "Exception";
}
return "";
}
}
可以請你給我一個FUL示例代碼? – doubter
她是收發箱鏈接,上傳到服務器程序文件 https://www.dropbox.com/s/qasj5o5mu3dq5ya/file_remote_send.zip 你剛剛更改婁代碼
您的服務器發生在這裏 字符串upLoadServerUri的URL =「http://example.com/UploadToServer.php” ;;
here uploaded_file id php base filename conn.setRequestProperty(「uploaded_file」,fileName);
UploadToServer.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」;
}
?>
什麼是uploaded_file?有沒有我想在這個PHP代碼中做的任何編輯? – doubter
我要上傳喜歡的文本文件,圖像,其它附件,服務器數據庫文件,而不是消息 – doubter