您好我正在使用android。我创建了一个Android应用程序,它将文件上传到服务器。我使用了以下代码
但我无法上传文件。该应用程序显示已开始上传,但没有文件上传到我的服务器位置。
答案 0 :(得分:0)
public class save扩展了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
答案 1 :(得分:0)
你可以使用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 "";
}
}
答案 2 :(得分:0)
她是文件上传到服务器程序的Drop box链接 https://www.dropbox.com/s/qasj5o5mu3dq5ya/file_remote_send.zip 你只需改变Bellow Code
您的服务器的URl放在这里 String upLoadServerUri =“http://example.com/UploadToServer.php” ;;
这里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”;
}
?>
答案 3 :(得分:0)
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);
}
}