将所选文件(PDF,doc)上传到服务器android

时间:2016-10-15 19:15:44

标签: android

我正在尝试将选定的文件上传到服务器,而当我在" uploadbtn"时开始上传活动时,我似乎遇到了问题。单击按钮,所以问题是我应该遵循哪些根成功上传所选文件?任何意见是极大的赞赏。 我已经完成了这个应用程序的php和mysql方面,下面是我的大部分代码,除了php和mysql代码。

 public void onClick(View v) {

    switch (v.getId()) {
        case R.id.filetoupload:

            Intent fileintent = new Intent(Intent.ACTION_GET_CONTENT);
            fileintent.setType("application/*");
            startActivityForResult(fileintent, RESULT_LOAD_FILE);

        break;

        case R.id.uploadbut:


            break;
    }

}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {

        case RESULT_LOAD_FILE:
            if (requestCode == RESULT_LOAD_FILE && resultCode == RESULT_OK
                    && null != data) {
                Uri selectedPdf = data.getData();

                filetoupload.setVisibility(View.VISIBLE);
                if (selectedPdf.getLastPathSegment().endsWith("pdf")) {


                    System.out.println("Uri of selected pdf---->" + selectedPdf.toString());
                } else if (resultCode == RESULT_CANCELED) {
                    Toast.makeText(this, "Invalid file type", Toast.LENGTH_SHORT).show();
                }
            }
    }
}

1 个答案:

答案 0 :(得分:0)

我建议使用apache库将文件下载到服务器中。

http://hc.apache.org/

所以,你需要: https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient/4.5.2 https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore/4.4.5 https://mvnrepository.com/artifact/org.apache.httpcomponents/httpmime/4.5.2 https://mvnrepository.com/artifact/org.apache.james/apache-mime4j-core/0.7.2

代码的一部分:

MultipartEntityBuilder builder = MultipartEntityBuilder.create();  
//some field      
                     builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
         builder.addPart("a_simple_text",  new StringBody("sometext",ContentType.TEXT_PLAIN));
//some file
builder.addPart("upload_file", new ByteArrayBody(b,path));


                 HttpEntity entity = builder.build();

                 HttpPost post = new HttpPost(url);


                 OProgressHttpEntityWrapper.ProgressCallback progressCallback = new OProgressHttpEntityWrapper.ProgressCallback() {

                        @Override
                        public void progress(float progress, float totalprogress , long transferred,
                                long totalBytes, long cut_data, long length) {
                            Log.d(TAG, "progress " + progress + " totalprogress " + totalprogress + " transferred " + transferred + " totalBytes " + totalBytes + " cut_data " + cut_data + " length " + length);
                        }

                    };
                 post.setEntity(new OProgressHttpEntityWrapper(entity, progressCallback, cut_data, length));


 HttpClient client = new DefaultHttpClient();
                 HttpResponse response = client.execute(post);  
                 String html = EntityUtils.toString(response.getEntity());