如何使用volley从Android设备上传.doc,.dox和.pdf文件到服务器

时间:2017-05-24 07:38:05

标签: android android-volley

我正在研究求职门户应用程序,我希望以.doc,.docx和.pdf等所有格式上传简历。目前,一次只支持一种文件格式。以下是我所做的:

Intent intent = new Intent();
            intent.setType("application/pdf,application/msword");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            intent.addCategory(Intent.CATEGORY_BROWSABLE);
            startActivityForResult(intent, REQUEST_CODE_DOC);

请帮我怎么做。谢谢提前

2 个答案:

答案 0 :(得分:0)

试试这个link。基本上你要做的就是读取文件转换为字节,然后在截击请求中覆盖getBody方法。

答案 1 :(得分:0)

  public void upload(){
                    Intent intent = new Intent();
                    intent.setType("application/pdf,application/msword");
                    intent.setAction(Intent.ACTION_GET_CONTENT);
                    intent.addCategory(Intent.CATEGORY_BROWSABLE);
                    startActivityForResult(intent, REQUEST_CODE_DOC);

                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) 
                          {
                                File file=new File(getPath(Payment_upload.this,uri));
                                String path=getPath(context,uri);
                                upload_file=getStringFile(file);
                           }


                VolleyMultipartRequest multipartRequest = new VolleyMultipartRequest(Request.Method.POST, u, new Response.Listener<NetworkResponse>() {
                    @Override
                    public void onResponse(NetworkResponse response) {
                        String resultResponse = new String(response.data);

                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        NetworkResponse networkResponse = error.networkResponse;
                        String errorMessage = "Unknown error";
                        if (networkResponse == null) {
                            if (error.getClass().equals(TimeoutError.class)) {
                                errorMessage = "Request timeout";
                            } else if (error.getClass().equals(NoConnectionError.class)) {
                                errorMessage = "Failed to connect server";
                            }
                        } else {
                            String result = new String(networkResponse.data);
                            System.out.println(result);
                        }
                        Log.i("Error", errorMessage);
                        error.printStackTrace();
                    }
                }) {


                    @Override
                    protected Map<String, String> getParams() throws AuthFailureError {
                        Map<String, String> parameters = new HashMap<String, String>();
                        parameters.put("key", value);

                        return parameters;
                    }
                };

                VolleySingleton.getInstance(getBaseContext()).addToRequestQueue(multipartRequest);

                   }

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public static String getPath(final Context context, final Uri uri) {

    final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;

    // DocumentProvider
    if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
        // ExternalStorageProvider
        if (isExternalStorageDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];

            if ("primary".equalsIgnoreCase(type)) {
                return Environment.getExternalStorageDirectory() + "/" + split[1];
            }


        }
        // DownloadsProvider
        else if (isDownloadsDocument(uri)) {

            final String id = DocumentsContract.getDocumentId(uri);
            final Uri contentUri = ContentUris.withAppendedId(
                    Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));

            return getDataColumn(context, contentUri, null, null);
        }
        // MediaProvider
        else if (isMediaDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];

            Uri contentUri = null;
            if ("image".equals(type)) {
                contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            } else if ("video".equals(type)) {
                contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
            } else if ("audio".equals(type)) {
                contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            }

            final String selection = "_id=?";
            final String[] selectionArgs = new String[] {
                    split[1]
            };

            return getDataColumn(context, contentUri, selection, selectionArgs);
        }
    }
    // MediaStore (and general)
    else if ("content".equalsIgnoreCase(uri.getScheme())) {
        return getDataColumn(context, uri, null, null);
    }
    // File
    else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }

    return null;
}



    @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            // TODO Auto-generated method stub
            super.onActivityResult(requestCode, resultCode, data);

            if (requestCode == REQUEST_CODE_DOC && resultCode == RESULT_OK && data != null && data.getData() != null)
            {
               Uri uri=data.getData();
            }

        }