如何在服务器上上传所有类型的文件,例如.pdf,.doc,mp3等

时间:2019-06-18 07:13:14

标签: java php android json

我的代码:

protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100 && resultCode == RESULT_OK && data != null) {

            //getting the image Uri
            final Uri imageUri = data.getData();
           // File file = new File(selectedFileURI.getPath().toString());

             getFileName(imageUri);
            try {
                //getting bitmap object from uri
                final Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);

                //displaying selected image to imageview



                  final String sno = getIntent().getExtras().getString("sno");
                  final ImageView file = (ImageView)findViewById(R.id.file_img);


                final AlertDialog.Builder builder = new AlertDialog.Builder(ChatActivity.this);
                builder.setTitle("send");
                builder.setMessage(getFileName(imageUri));
                builder.setPositiveButton("OK",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {

                                ChatMessage chatMessage = new ChatMessage();

                                chatMessage.setMessageFile(String.valueOf(getFileDataFromDrawable(bitmap)));
                                chatMessage.setFileName(getFileName(imageUri));
                                chatMessage.setUserId(sno);
                                chatMessage.setReply_msg("0");
                                chatMessage.setReply_user("0");
                                final String time = "now";
                                chatMessage.setMessageTime(time);
                                ((ChatAdapter) listView.getAdapter()).notifyDataSetChanged();
                                Scroll();
                                chatMessages.add(chatMessage);
                                chatAdapter.notifyDataSetChanged();
                                file.setImageBitmap(bitmap);





                                // chatAdapter.getView()

                                //calling the method uploadBitmap to upload image
                                uploadFile(bitmap);
                            }
                        });
                builder.show();







               // chatAdapter.getView()

                //calling the method uploadBitmap to upload image
               // uploadFile(bitmap);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }




 private void uploadFile(final Bitmap bitmap) {

        final String topic_id = getIntent().getExtras().getString("topic_id");

        final String sno = getIntent().getExtras().getString("sno");
        VolleyMultipartRequest volleyMultipartRequest = new VolleyMultipartRequest(Request.Method.POST, URL_INSERT,
                new Response.Listener<NetworkResponse>() {
                    @Override
                    public void onResponse(NetworkResponse response) {
                        try {
                          //  JSONObject obj = new JSONObject(new String(response.data));
                            JSONObject jsonObject = new JSONObject();
                            String success = jsonObject.getString("m_insert_status");
                            JSONArray jsonArray = jsonObject.getJSONArray("data");

                            if (success.equals("true")) {
                                startActivity(getIntent());
                                for (int i = 0; i < jsonArray.length(); i++) {
                                    JSONObject object = jsonArray.getJSONObject(i);
                                    Toast.makeText(getApplicationContext(), object.getString("message"), Toast.LENGTH_SHORT).show();


                                }
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }) {

            /*
             * If you want to add more parameters with the image
             * you can do it here
             * here we have only one parameter with the image
             * which is tags
             * */
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                params.put("sno", sno);
                params.put("topic_id", topic_id);


                return params;
            }

            /*
             * Here we are passing image by renaming it with a unique name
             * */
            @RequiresApi(api = Build.VERSION_CODES.O)
            @Override
            protected Map<String, VolleyMultipartRequest.DataPart> getByteData() {
                Map<String, VolleyMultipartRequest.DataPart> params = new HashMap<>();
                long imagename = System.currentTimeMillis();
               params.put("attach_files", new VolleyMultipartRequest.DataPart(imagename + ".jpg", getFileDataFromDrawable(bitmap)));
                String pdfName = String.valueOf(System.currentTimeMillis() + ".pdf");



                return params;
            }


        };
 volleyMultipartRequest.setRetryPolicy(
                new DefaultRetryPolicy(
                        0,
                        -1,
                        DefaultRetryPolicy.DEFAULT_BACKOFF_MULT
                )
        );

        //adding the request to volley
        Volley.newRequestQueue(this).add(volleyMultipartRequest);

    }

0 个答案:

没有答案