簡體   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