我在上传文件到服务器时遇到一些困难,我需要知道使用namevaluepair上传文件的完整代码。听到是我的android代码,我只得到我的文件路径,如何将其上传到服务器的引用为“nameValuePairs.add(新的BasicNameValuePair(”附件“,selectedFilePath));”如何使用Android将.pdf .doc .txt文件上传到服务器(mysql数据库)
String selectedFilePath="downloads/harry.txt" //(My file Path)
InputStream is = null;
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("postiontitle", positiontitle));
nameValuePairs.add(new BasicNameValuePair("description", description));
nameValuePairs.add(new BasicNameValuePair("jobtype", jobtype));
nameValuePairs.add(new BasicNameValuePair("visatype", visatype));
if (selectedFilePath != null)
{
nameValuePairs.add(new BasicNameValuePair("attachment", selectedFilePath));
}
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://10.0.3.2/dfsdsd/postjob");
// HttpPost httpPost = new
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
int code = response.getStatusLine().getStatusCode();
String rescode = String.valueOf(code);
//result=is.toString();
return rescode;
} catch (MalformedURLException e) {
return "No Internet";
} catch (IOException e) {
return "No Internet";
}
}
你为什么不使用这个库,它的方便,快捷,高效,背景 –
工作https://github.com/gotev/android-upload-service –