Получение Ошибка 500 при использовании multipart в Android - PullRequest
0 голосов
/ 19 сентября 2019

Я должен загрузить изображение на сервер, используя multipart.При загрузке изображения с веб-сайта я получаю этот ответ

- ------WebKitFormBoundaryBxdrrt1HbcN7DpvV
Content-Disposition: form-data; name="user_id"

1491
------WebKitFormBoundaryBxdrrt1HbcN7DpvV
Content-Disposition: form-data; name="ssn"

undefined
------WebKitFormBoundaryBxdrrt1HbcN7DpvV
Content-Disposition: form-data; name="profile_pic"; filename="demo.jpg"
Content-Type: image/jpeg


------WebKitFormBoundaryBxdrrt1HbcN7DpvV
Content-Disposition: form-data; name="id_proof_doc"


------WebKitFormBoundaryBxdrrt1HbcN7DpvV
Content-Disposition: form-data; name="address_proof_doc"


------WebKitFormBoundaryBxdrrt1HbcN7DpvV
Content-Disposition: form-data; name="address_proof_doc_2"; filename="demo.jpg"
Content-Type: image/jpeg


------WebKitFormBoundaryBxdrrt1HbcN7DpvV--

, но при загрузке его из моего приложения я получаю 500 ошибок. Я просто не могу понять, что не так с моим кодом, мой код:

class AsynctaskImageUpload extends AsyncTask<String,String,String>{

        @SuppressLint("LongLogTag")
        @Override
        protected String doInBackground(String... strings) {

            HttpURLConnection conn = null;
            DataOutputStream dos = null;
            String lineEnd = "\r\n";
            String twoHyphens = "--";
            String boundary = "*****";
            int bytesRead, bytesAvailable, bufferSize;
            byte[] buffer;
            int maxBufferSize = 1 * 1024 * 1024;
            final File sourceFile = new File(getRealPathFromURI(ProfileImageURI));
            String serverResponseMessage = null;
            String responce = null;


            if (!sourceFile.isFile()) {

                getActivity().runOnUiThread(new Runnable() {
                    public void run() {
                        Toast.makeText(getActivity(), "File not found !", Toast.LENGTH_LONG).show();
                    }
                });

                return "no file";
            }
            else {
                try {
                    FileInputStream fileInputStream = new FileInputStream(sourceFile.getPath());
                    URL url = new URL("https://service.paybito.com/paybito_web_trade/user/UpdateUserDocs");
                    conn = (HttpURLConnection) url.openConnection();
                    conn.setDoInput(true);
                    conn.setDoOutput(true);
                    conn.setUseCaches(false);
                    conn.setRequestMethod("POST");
                    conn.setRequestProperty("Connection", "Keep-Alive");
                    conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
                    conn.setRequestProperty("authorization","BEARER "+strAccess_token);

                    /*dos = new DataOutputStream(conn.getOutputStream());
                    dos.writeBytes(twoHyphens + boundary + lineEnd);
                    dos.writeBytes("Content-Disposition: form-data; name=\"" + "profile_pic"+"\";filename="+ sourceFile.getName() + lineEnd);
                    dos.writeBytes(lineEnd);
                    bytesAvailable = fileInputStream.available();
                    bufferSize = Math.min(bytesAvailable, maxBufferSize);
                    buffer = new byte[bufferSize];
                    bytesRead = fileInputStream.read(buffer, 0, bufferSize);
                    while (bytesRead > 0) {

                        dos.write(buffer, 0, bufferSize);
                        bytesAvailable = fileInputStream.available();
                        bufferSize = Math.min(bytesAvailable, maxBufferSize);
                        bytesRead = fileInputStream.read(buffer, 0, bufferSize);

                    }
                    dos.writeBytes(lineEnd);
                    dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
                    int serverResponseCode = conn.getResponseCode();
                    serverResponseMessage = conn.getResponseMessage();
                    Log.e("uploadFile", "HTTP Response is : "
                            + serverResponseMessage + ": " + serverResponseCode);
                    if (serverResponseCode <= 200) {

                        getActivity().runOnUiThread(new Runnable() {
                            public void run() {

                                Toast.makeText(getActivity(), "File Upload Complete.",
                                        Toast.LENGTH_SHORT).show();
                            }
                        });
                    }*/


                    //========================================================================//

                    dos = new DataOutputStream(conn.getOutputStream());
                    dos.writeBytes(twoHyphens + boundary + lineEnd);
                    dos.writeBytes("Content-Disposition: form-data; name=\"user_id\"" + lineEnd);
                    dos.writeBytes(strUserId);
                    dos.writeBytes(lineEnd);
                    dos.writeBytes(twoHyphens + boundary + lineEnd);


                    dos = new DataOutputStream(conn.getOutputStream());
                    dos.writeBytes(twoHyphens + boundary + lineEnd);
                    dos.writeBytes("Content-Disposition: form-data; name=\"ssn\"" + lineEnd);
                    dos.writeBytes("undefined");
                    dos.writeBytes(lineEnd);
                    dos.writeBytes(twoHyphens + boundary + lineEnd);


                    dos = new DataOutputStream(conn.getOutputStream());
                    dos.writeBytes(twoHyphens + boundary + lineEnd);
                    String content_deposition_profilePic = "Content-Disposition: form-data; name=\"profile_pic\";filename=\"" + sourceFile.getName() +"\"" + lineEnd;
                    Log.e("content_deposition",content_deposition_profilePic);
                    dos.writeBytes("Content-Disposition: form-data; name=\"profile_pic\";filename=\"" + sourceFile.getName() +"\"" + lineEnd);
                    dos.writeBytes("Content-Type: image/jpeg" + lineEnd);
                    dos.writeBytes(lineEnd);
                    dos.writeBytes(twoHyphens + boundary + lineEnd);
                    Log.e("Asynctask File Name",String.valueOf(sourceFile.getName()));


                    dos = new DataOutputStream(conn.getOutputStream());
                    dos.writeBytes(twoHyphens + boundary + lineEnd);
                    String content_deposition = "Content-Disposition: form-data; name=\"id_proof_doc\";filename=\"" + "" +"\"" + lineEnd;
                    Log.e("content_deposition",content_deposition);
                    dos.writeBytes("Content-Type: image/jpeg" + lineEnd);
                    dos.writeBytes("Content-Disposition: form-data; name=\"id_proof_doc\";filename=\"" + "" +"\"" + lineEnd);
                    dos.writeBytes(lineEnd);
                    dos.writeBytes(twoHyphens + boundary + lineEnd);
                    //Log.e("Asynctask File Name",String.valueOf(sourceFile.getName()));


                    dos = new DataOutputStream(conn.getOutputStream());
                    dos.writeBytes(twoHyphens + boundary + lineEnd);
                    dos.writeBytes("Content-Disposition: form-data; name=\"address_proof_doc\";filename=\"" + ""+"\"" + lineEnd);
                    dos.writeBytes("Content-Type: image/jpeg" + lineEnd);
                    dos.writeBytes(lineEnd);
                    dos.writeBytes(twoHyphens + boundary + lineEnd);
                    //Log.e("Asynctask File Name",String.valueOf(sourceFile.getName()));


                    dos = new DataOutputStream(conn.getOutputStream());
                    dos.writeBytes(twoHyphens + boundary + lineEnd);
                    dos.writeBytes("Content-Disposition: form-data; name=\"address_proof_doc_2\";filename=\"" + ""+"\"" + lineEnd);
                    dos.writeBytes("Content-Type: image/jpeg" + lineEnd);
                    dos.writeBytes(lineEnd);
                    dos.writeBytes(twoHyphens + boundary + lineEnd);
                    //Log.e("Asynctask File Name",String.valueOf(sourceFile.getName()));
                    //=======================================================================//


                    int serverResponseCode = conn.getResponseCode();
                    serverResponseMessage = conn.getResponseMessage();
                    Log.e("uploadFile", "HTTP Response is : "
                            + serverResponseMessage + ": " + serverResponseCode);

                    fileInputStream.close();
                    dos.flush();
                    dos.close();




                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (MalformedURLException ex) {

                    ex.printStackTrace();

                    getActivity().runOnUiThread(new Runnable() {
                        public void run() {

                            Toast.makeText(getActivity(), "MalformedURLException",
                                    Toast.LENGTH_SHORT).show();
                        }
                    });
                } catch (IOException e) {

                    e.printStackTrace();

                    getActivity().runOnUiThread(new Runnable() {
                        public void run() {
                            Toast.makeText(getActivity(), "Got Exception : see logcat ",
                                    Toast.LENGTH_SHORT).show();
                        }
                    });
                    Log.e("Upload file to server Exception", "Exception : "
                            + e.getMessage(), e);
                }
            }

            return null;
        }
    }

Пожалуйста, любая помощь будет оценена.

...