0
我想上傳一些數據到Heroku/PHP服務器,並出於某種原因它的工作不一致。它在我們的辦公室對我來說工作得很好,但是當其他人試圖使用我們的應用程序時,沒有任何文件到達服務器。服務器提供「空文件」錯誤。Android文件上傳工作不一致
下面是Android的上傳代碼:
private class SubmitClientTask extends AsyncTask<URL, Integer, Long> {
AlertDialog.Builder mDialog;
protected void onPreExecute() {
super.onPreExecute();
mDialog = new AlertDialog.Builder(ClientSignActivity.this);
mDialog.setCancelable(false);
}
protected Long doInBackground(URL... urls) {
//int count = urls.length;
long totalSize = 0;
String response = "";
File signatureFile = new File(signaturePhotoPath);
File photoFile = new File(StaticValues.currentPhotoPath);
String boundary = "*****";//Long.toHexString(System.currentTimeMillis());
String CRLF = "\r\n";
String charset = "UTF-8";
try {
URLConnection mConnection = urls[0].openConnection();
mConnection.setDoOutput(true);
((HttpURLConnection)mConnection).setRequestMethod("POST");
//mConnection.setRequestProperty("Connection", "Keep-Alive"); // would this line of code help?
mConnection.setRequestProperty("Cache-Control", "no-cache"); // this one?
mConnection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
OutputStream output = mConnection.getOutputStream();
PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, charset));
writer.append("--" + boundary).append(CRLF);
writer.append("Content-Disposition: form-data; name=\"dentistName\"").append(CRLF);
writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF);
writer.append(CRLF).append("A string here").append(CRLF).flush();
writer.append("--" + boundary).append(CRLF);
writer.append("Content-Disposition: form-data; name=\"patientName\"").append(CRLF);
writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF);
writer.append(CRLF).append("Another string here").append(CRLF).flush();
writer.append("--" + boundary).append(CRLF);
writer.append("Content-Disposition: form-data; name=\"patientPhone\"").append(CRLF);
writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF);
writer.append(CRLF).append("A third string").append(CRLF).flush();
//set up image stuff
InputStream is;
int c;
byte[] buf;
//send image
writer.append("--" + boundary).append(CRLF);
writer.append("Content-Disposition: form-data; name=\"patientPhoto\"; filename=\"" + photoFile.getName() + "\"").append(CRLF);
writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF);
writer.append(CRLF).flush();
c = 0;
is = new FileInputStream(StaticValues.patientBitmapToSend);
buf = new byte[8192];
while ((c = is.read(buf, 0, buf.length)) > 0) {
output.write(buf, 0, c);
output.flush();
}
output.flush();
writer.append(CRLF).flush();
//send another photo
writer.append("--" + boundary).append(CRLF);
writer.append("Content-Disposition: form-data; name=\"signaturePhoto\"; filename=\"" + signatureFile.getName() + "\"").append(CRLF);
writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF);
writer.append(CRLF).flush();
c = 0;
is = new FileInputStream(signatureFile);
buf = new byte[8192];
while ((c = is.read(buf, 0, buf.length)) > 0) {
output.write(buf, 0, c);
output.flush();
}
output.flush();
writer.append(CRLF).flush();
//end of multipart/form-data
writer.append("--" + boundary + "--").append(CRLF).flush();
Log.d("content: ", mConnection.getContent().toString());
int responseCode = ((HttpURLConnection) mConnection).getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
String line;
BufferedReader br = new BufferedReader(new InputStreamReader(mConnection.getInputStream()));
while ((line = br.readLine()) != null) {
response += line;
}
sendStuffResponse = response;
} else {
response = "failed";
}
//finally,
Log.d("Response: ", response);
((HttpURLConnection) mConnection).disconnect();
//cleanup
(new File(StaticValues.currentPhotoPath)).delete();
photoFile.delete();
signatureFile.delete();
} catch (Exception e) {
e.printStackTrace();
}
return totalSize;
}
protected void onProgressUpdate(Integer... progress) {
//setProgressPercent(progress[0]);
}
protected void onPostExecute(Long result) {
super.onPostExecute(result);
progressDialog.dismiss();
mDialog.setTitle(R.string.upload_success_title);
mDialog.setMessage(R.string.upload_success_str); //DEBUG sendStuffResponse
mDialog.setPositiveButton(R.string.ok_str, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
finish();
}
});
mDialog.create().show();
//Toast.makeText(getApplicationContext(), "Downloaded " + result + " bytes", Toast.LENGTH_LONG).show();
}
}
任何幫助,將不勝感激。謝謝!
'catch(Exception e){ e.printStackTrace(); }'。該catch塊是空的。所以如果有一個問題,你如何通知用戶?目前沒有。所以用戶永遠不知道是否出了問題。而你也是。 – greenapps
好點,我應該添加一條錯誤消息,但是我知道沒有圖像到達服務器,因爲我看到只有169個字節,並且該文件是空的。它也給出了500的響應代碼。 –
服務器是否只能從您的內部網絡訪問?外部網絡和你的網絡之間是否有防火牆? –