Reputation: 983
FileNotFoundException
when uploading .mp3 file to server in android. Please help me.
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.
Upvotes: 0
Views: 537
Reputation: 146
A FileNotFoundException is thrown if your code can not find the file.
Since you have not added the location of your file to your question we can only guess where you put it.
Make sure it is in the same path as your Javafile or change your code to the path where it is located.
Upvotes: 1
Reputation: 4702
java.io.FileNotFoundException: http://172.16.10.64:8080/plugleadservices/rest/feedbackmanagement/upload?company_id=1
This error means, the URL doesn't exist.
Upvotes: 0