将.mp3文件上传到android中的服务器时出现FileNotFoundException

时间:2016-06-06 07:55:27

标签: android filenotfoundexception

将.mp3文件上传到android中的服务器时

FileNotFoundException。请帮帮我。

 String sep = File.separator; // Use this instead of hardcoding the "/"
            String newFolder = "folderName";
            String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
            File myNewFolder = new File(extStorageDirectory + sep + newFolder);
            myNewFolder.mkdir();
            outputFile = Environment.getExternalStorageDirectory().toString() 
              + sep + newFolder + sep + Company_Name + "_"+dte+hh+"_"+mm+"_"+ss +".mp3";




HttpURLConnection conn = null;
                DataOutputStream dos = null;
                DataInputStream inStream = null;
                String existingFileName = Environment.getExternalStorageDirectory().getAbsolutePath() + outputFile;
                String lineEnd = "\r\n";
                String twoHyphens = "--";
                String boundary = "*****";
                int bytesRead, bytesAvailable, bufferSize;
                byte[] buffer;
                int maxBufferSize = 1 * 1024 * 1024;
                String responseFromServer = "";
                String urlString = "http://172.16.10.64:8080/plugleadservices/rest/feedbackmanagement/upload?company_id=1";

                try {

                    //------------------ CLIENT REQUEST
                    FileInputStream fileInputStream = new FileInputStream(new File(outputFile));
                    // open a URL connection to the Servlet
                    URL url = new URL(urlString);
                    // Open a HTTP connection to the URL
                    conn = (HttpURLConnection) url.openConnection();
                    // Allow Inputs
                    conn.setDoInput(true);
                    // Allow Outputs
                    conn.setDoOutput(true);
                    // Don't use a cached copy.
                    conn.setUseCaches(false);
                    // Use a post method.
                    conn.setRequestMethod("POST");
                    conn.setRequestProperty("Connection", "Keep-Alive");
                    conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
                    dos = new DataOutputStream(conn.getOutputStream());
                    dos.writeBytes(twoHyphens + boundary + lineEnd);
                    dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + outputFile + "\"" + lineEnd);
                    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);

                    }

                    // send multipart form data necesssary after file data...
                    dos.writeBytes(lineEnd);
                    dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
                    // close streams
                    Log.e("Debug", "File is written");
                    fileInputStream.close();
                    dos.flush();
                    dos.close();

                } catch (MalformedURLException ex) {
                    Log.e("Debug", "error: " + ex.getMessage(), ex);
                } catch (IOException ioe) {
                    Log.e("Debug", "error: " + ioe.getMessage(), ioe);
                }

                //------------------ read the SERVER RESPONSE
                try {

                    inStream = new DataInputStream(conn.getInputStream());
                    String str;

                    while ((str = inStream.readLine()) != null) {

                        Log.e("Debug", "Server Response " + str);

                    }

                    inStream.close();

                } catch (IOException ioex) {
                    Log.e("Debug", "error: " + ioex.getMessage(), ioex);
                }

logcat的:

06-06 13:11:46.389: I/System.out(25970): $$$$$$$ Output File : /storage/emulated/0/Plugleads/test6_06-06-201601_11_32.mp3
06-06 13:11:48.132: V/RenderScript(25970): Application requested CPU execution
06-06 13:11:48.145: V/RenderScript(25970): 0xb91082d0 Launching thread(s), CPUs 2
06-06 13:11:54.871: E/Debug(25970): File is written
06-06 13:11:58.529: E/Debug(25970): error: http://172.16.10.64:8080/plugleadservices/rest/feedbackmanagement/upload?company_id=1
06-06 13:11:58.529: E/Debug(25970): java.io.FileNotFoundException: http://172.16.10.64:8080/plugleadservices/rest/feedbackmanagement/upload?company_id=1
06-06 13:11:58.529: E/Debug(25970):     at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:206)
06-06 13:11:58.529: E/Debug(25970):     at com.plugleads.feedback.record.Frag_Record.doFileUpload(Frag_Record.java:656)
06-06 13:11:58.529: E/Debug(25970):     at com.plugleads.feedback.record.Frag_Record.onClick(Frag_Record.java:524)
06-06 13:11:58.529: E/Debug(25970):     at android.view.View.performClick(View.java:4785)
06-06 13:11:58.529: E/Debug(25970):     at android.view.View$PerformClick.run(View.java:19884)
06-06 13:11:58.529: E/Debug(25970):     at android.os.Handler.handleCallback(Handler.java:739)
06-06 13:11:58.529: E/Debug(25970):     at android.os.Handler.dispatchMessage(Handler.java:95)
06-06 13:11:58.529: E/Debug(25970):     at android.os.Looper.loop(Looper.java:135)
06-06 13:11:58.529: E/Debug(25970):     at android.app.ActivityThread.main(ActivityThread.java:5343)
06-06 13:11:58.529: E/Debug(25970):     at java.lang.reflect.Method.invoke(Native Method)
06-06 13:11:58.529: E/Debug(25970):     at java.lang.reflect.Method.invoke(Method.java:372)
06-06 13:11:58.529: E/Debug(25970):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
06-06 13:11:58.529: E/Debug(25970):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
06-06 13:11:58.530: I/Choreographer(25970): Skipped 228 frames!  The application may be doing too much work on its main thread.

2 个答案:

答案 0 :(得分:1)

如果您的代码找不到该文件,则抛出FileNotFoundException。

由于您尚未将文件的位置添加到您的问题中,因此我们只能猜测您放置它的位置。

确保它与Javafile位于同一路径中,或将代码更改为其所在的路径。

答案 1 :(得分:0)

  

java.io.FileNotFoundException:   http://172.16.10.64:8080/plugleadservices/rest/feedbackmanagement/upload?company_id=1

此错误表示网址不存在。