如何使用Android将.pdf .doc .txt文件上传到服务器(mysql数据库) [英] How to upload .pdf .doc .txt files to server(mysql database) using Android

查看:530
本文介绍了如何使用Android将.pdf .doc .txt文件上传到服务器(mysql数据库)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将文件上传到服务器时遇到一些困难,我需要知道使用namevaluepair上传文件的完整代码.听到的是我的android代码,我只有我的文件路径,如何将其上传到服务器,引用为"nameValuePairs.add(new BasicNameValuePair("attachment",selectedFilePath));"

I'm facing some difficulties while uploading files to server, I need to know full code of file uploading with namevaluepair. Hear is my android code, I'm getting only my file path, how to upload it to server with reference as "nameValuePairs.add(new BasicNameValuePair("attachment", selectedFilePath));"

       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";
        }
    }

推荐答案

使用Retrofil或Okhttp.

Use Retrofil or Okhttp.

public static final String MULTIPART_FORM_DATA = "multipart/form-data";

 @NonNull
 private RequestBody createPartFromString(String descriptionString) {  
   return RequestBody.create(
        MediaType.parse(MULTIPART_FORM_DATA), descriptionString);
 }

 @NonNull
 private MultipartBody.Part prepareFilePart(String partName, Uri fileUri) {  
// https://github.com/iPaulPro/aFileChooser/blob/master/aFileChooser/src/com/ipaulpro/afilechooser/utils/FileUtils.java
// use the FileUtils to get the actual file by uri
File file = FileUtils.getFile(this, fileUri);

// create RequestBody instance from file
RequestBody requestFile =
    RequestBody.create(MediaType.parse(MULTIPART_FORM_DATA), file);

// MultipartBody.Part is used to send also the actual file name
return MultipartBody.Part.createFormData(partName, file.getName(), requestFile);
}

这篇关于如何使用Android将.pdf .doc .txt文件上传到服务器(mysql数据库)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆