繁体   English   中英

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

[英]Uploading selected file(PDF, doc) to a server 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();
                }
            }
    }
}

我建议使用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());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM