0

I want to upload images and audio files from sd card to server. When run no app crash but image and audio is uploaded proper way. When i go to server and check the image and audio and showing file not found.Means when i click on audio symbol try to play on server it shows file not found.

here is my Image and Audio file upLoad method

uploadFile(uploadFilePath+""+imagePath , imagePath);

public int uploadFile(String uploadFileName ,String upLoadImageAudioName )
    {
        String fileName = uploadFileName;
        String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
        String upLoadFilePath = baseDir+"/classnkk_images/";
        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;
        File sourceFile = new File(uploadFileName);

        if (!sourceFile.isFile())
        {
            Log.e("uploadFile", "Source File not exist :" +upLoadFilePath + "" + upLoadImageAudioName);
            return 0;
        }
        else
        {
            try
            {
                // open a URL connection to the Servlet
                FileInputStream fileInputStream = new FileInputStream(sourceFile);
                URL url = new URL("http://xxxx/xxxx/MobileService.svc/UploadFileStream/");

                // Open a HTTP  connection to  the URL
                conn = (HttpURLConnection) url.openConnection();
                conn.setDoInput(true); // Allow Inputs
                conn.setDoOutput(true); // Allow Outputs
                conn.setUseCaches(false); // Don't use a Cached Copy
                conn.setRequestMethod("POST");
                conn.setRequestProperty("Connection", "Keep-Alive");
                conn.setRequestProperty("ENCTYPE", "multipart/form-data");
                conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
                conn.setRequestProperty("uploaded_file", fileName);

                if(fileName.endsWith(".png"))
                {
                    dos = new DataOutputStream(conn.getOutputStream());
                    dos.writeBytes(twoHyphens + boundary + lineEnd);
                    dos.writeBytes("Content-Disposition: form-data; name=\"file_name\";filename=\"" + fileName + "\"" + lineEnd);
                    dos.writeBytes(lineEnd);
                    // Code for sending the image....
                    dos.writeBytes(lineEnd);
                }

                // create a buffer of  maximum size
                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                buffer = new byte[bufferSize];

                // read file and write it into form...
                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);

                }

                if(fileName.endsWith(".mp3"))
                {
                    dos.writeBytes(twoHyphens + boundary + lineEnd);
                    dos.writeBytes("Content-Disposition: form-data; name=\"file_name_audio\";filename=\""+ fileName + "\"" + lineEnd);
                    dos.writeBytes(lineEnd);
                    // Code for sending the MP3
                    dos.writeBytes(lineEnd);
                    dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

                }
                serverResponseCode = conn.getResponseCode();
                String serverResponseMessage = conn.getResponseMessage();
                Log.i("uploadFile", "HTTP Response is : " + serverResponseMessage + ": " + serverResponseCode);
                if(serverResponseCode == 200)
                {runOnUiThread(new Runnable() {
                        public void run() {}
                    });
                }
                //close the streams //
                fileInputStream.close();
                dos.flush();
                dos.close();

            } catch (MalformedURLException ex)
            {
                ex.printStackTrace();
                Log.e("Upload file to server", "error: " + ex.getMessage(), ex);
            } catch (Exception e)
            {
                e.printStackTrace();
            }
            return serverResponseCode;

        } // End else block
    }

Here is Log information

09-23 05:25:45.178  23676-24640/com.example.tazeen.classnkk W/System.err﹕ java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.DataOutputStream.write(byte[], int, int)' on a null object reference
09-23 05:25:45.178  23676-24640/com.example.tazeen.classnkk W/System.err﹕ at com.example.tazeen.classnkk.AddPost.uploadFile(AddPost.java:798)
09-23 05:25:45.178  23676-24640/com.example.tazeen.classnkk W/System.err﹕ at com.example.tazeen.classnkk.AddPost$7$1$1.run(AddPost.java:419)

Thanks in advanced .

e4c5
  • 52,766
  • 11
  • 101
  • 134
androidTag
  • 5,053
  • 7
  • 19
  • 28
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Henry Sep 23 '15 at 07:39

1 Answers1

0

Near the top of your code you have

    DataOutputStream dos = null;

Later on

if(fileName.endsWith(".png"))
{
   dos = new DataOutputStream(conn.getOutputStream());
   ...
}

Thus you are initializing your DataOutputStream when the filename ends with .png

but still later

if(fileName.endsWith(".mp3"))
{
    dos.writeBytes(twoHyphens + boundary + lineEnd);
}

If the filename ended with .png dos would have been initialized, but then the filename cannot end with .mp3 thus when you get here dos is null, hence the error.

e4c5
  • 52,766
  • 11
  • 101
  • 134
  • I use your code lines in my method but file not found at server side . – androidTag Sep 23 '15 at 09:48
  • The question you asked is about the NPE, which is answered here. The file not found on the server side has absolutely nothing to do with that. Please post a different question. – e4c5 Sep 23 '15 at 09:52