I have Android application that periodically uploads some files to web server.
The problem is that sometimes file is uploaded successfully (and I am getting some response from server), but sometimes I am getting FileNotFoundException at line
inputStream = connection.getInputStream();
. As I lnow it means that file theoretically was uploaded to server but we could not get stream that should give us response.
Please advice me some solution to avoid this exception. Server is IIS, it works perfectly, connection is stable.
My code:
public class UploadFileAsync extends AsyncTask<String, String, String> {
private final Callback<String> callback;
private HttpURLConnection connection = null;
private DataOutputStream outputStream = null;
private FileInputStream fileInputStream = null;
private InputStream inputStream = null;
public UploadFileAsync(Callback<String> callback) {
this.callback = callback;
}
@Override
protected String doInBackground(String... args) {
String fileURI = args[0];
String filePath = args[1];
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
String result = null;
try {
String fileName = new File(filePath).getName();
fileInputStream = new FileInputStream(filePath);
URL connectURL = new URL(fileURI);
connection = (HttpURLConnection) connectURL.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
outputStream = new DataOutputStream(connection.getOutputStream());
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + fileName + "\"" + lineEnd);
outputStream.writeBytes(lineEnd);
int bytesAvailable = fileInputStream.available();
int maxBufferSize = 1024;
int bufferSize = Math.min(bytesAvailable, maxBufferSize);
byte[] buffer = new byte[bufferSize];
int bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
outputStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
outputStream.flush();
inputStream = connection.getInputStream();
int ch;
StringBuffer stringBuffer = new StringBuffer();
while ((ch = inputStream.read()) != -1) {
stringBuffer.append((char) ch);
}
result = stringBuffer.toString();
} catch (Exception e) {
e.printStackTrace();
Log.e("APP_TAG", "UploadFileAsync. URL: " + fileURI + " Upload FAIL.");
result = e.getMessage();
} finally {
if (connection != null) {
connection.disconnect();
}
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (Exception e) {
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (Exception e) {
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (Exception e) {
}
}
}
return result;
}
@Override
public void onPostExecute(String result) {
if (callback != null) {
callback.process(result);
}
}
}
Exception stacktrace:
java.io.FileNotFoundException: http://myserver.com/files
at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
at myapp.core.utils.network.UploadFileAsync.doInBackground(UploadFileAsync.java:90)
at myapp.core.utils.network.UploadFileAsync.doInBackground(UploadFileAsync.java:17)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)