我有Android应用程序,定期将一些文件上传到Web服务器。
问题是有时文件上传成功(我从服务器得到一些响应),但有时我收到FileNotFoundException行
inputStream = connection.getInputStream();
。正如我所知,这意味着文件理论上已上传到服务器但我们无法获得应该给我们响应的流。
请告诉我一些避免此例外的解决方案。服务器是IIS,它工作正常,连接稳定。
我的代码:
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);
}
}
}
异常堆栈跟踪:
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)
答案 0 :(得分:0)
我遇到了同样的问题,并且解决了。问题是由超时引起的。我们唯一能做的就是按如下所示修改web.config。
<configuration>
<system.web>
<httpRuntime targetFramework="4.5.2" maxRequestLength="10240" executionTimeout="300" />
</system.web>
</configuration>