我有一個android應用程序上傳文件到服務器,它的工作沒有任何問題在本地主機,但沒有在服務器上工作,我檢查了路徑,它正確,android上傳文件到服務器得到404錯誤
日誌貓錯誤:未找到:404
,如果我在複製粘貼的URL在瀏覽器中它的工作
我在同一個地址的其他PHP文件(up3.php)和一個工作沒有任何問題,但在那一個我使用Json發送文本,而不是上傳文件
我已經尋找解決方案,但沒有發現任何東西,
這是我的代碼:
String upLoadServerUri = "http://live.mysite.com/up-file.php";
public int uploadFile(String sourceFileUri) {
String fileName = sourceFileUri;
HttpURLConnection conn = null;
DataOutputStream dos = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
File sourceFile = new File(sourceFileUri);
if (!sourceFile.isFile()) {
dialog.dismiss();
runOnUiThread(new Runnable() {
public void run() {
}
});
return 0;
}
else
{
try {
// open a URL connection to the Servlet
FileInputStream fileInputStream = new FileInputStream(sourceFile);
URL url = new URL(upLoadServerUri);
// Open a HTTP connection to the URL
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true); // Allow Inputs
conn.setDoOutput(true); // Allow Outputs
conn.setUseCaches(false); // Don't use a Cached Copy
conn.setRequestMethod("POST");
conn.setRequestProperty("Accept-Charset", "UTF-8");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("ENCTYPE", "multipart/form-data");
conn.setRequestProperty("Content-Type", "multipart/form-data ; charset=utf-8 ;boundary=" + boundary);
conn.setRequestProperty("uploaded_file", fileName);
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"post_id\""+ lineEnd);
dos.writeBytes(lineEnd);
dos.writeBytes(post_id);
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"username\""+ lineEnd);
dos.writeBytes(lineEnd);
dos.writeBytes(username);
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data ; name=\"txt\""+ lineEnd);
dos.writeBytes(lineEnd);
dos.writeUTF(up_txt);
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\""
+ fileName + "\"" + 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);
// Responses from the server (code and message)
serverResponseCode = conn.getResponseCode();
String serverResponseMessage = conn.getResponseMessage();
Log.i("uploadFile", "HTTP Response is : "
+ serverResponseMessage + ": " + serverResponseCode);
if(serverResponseCode == 200){
runOnUiThread(new Runnable() {
public void run() {
}
});
}
//close the streams //
fileInputStream.close();
dos.flush();
dos.close();
} catch (MalformedURLException ex) {
dialog.dismiss();
ex.printStackTrace();
runOnUiThread(new Runnable() {
public void run() {
}
});
Log.e("Upload file to server", "error: " + ex.getMessage(), ex);
} catch (Exception e) {
dialog.dismiss();
e.printStackTrace();
runOnUiThread(new Runnable() {
public void run() {
}
});
Log.e("Upload file to server Exception", "Exception : "
+ e.getMessage(), e);
}
dialog.dismiss();
return serverResponseCode;
} // End else block
登錄貓:
10-16 06:08:53.632: W/IInputConnectionWrapper(2571): showStatusIcon on inactive InputConnection
10-16 06:08:58.541: D/dalvikvm(2571): GC_CONCURRENT freed 169K, 3% free 9758K/10055K, paused 15ms+1ms, total 18ms
10-16 06:08:58.551: D/dalvikvm(2571): GC_FOR_ALLOC freed 608K, 10% free 9150K/10055K, paused 2ms, total 2ms
10-16 06:08:58.551: I/dalvikvm-heap(2571): Grow heap (frag case) to 10.340MB for 1440012-byte allocation
10-16 06:08:58.571: D/dalvikvm(2571): GC_CONCURRENT freed 0K, 8% free 10557K/11463K, paused 12ms+1ms, total 15ms
10-16 06:09:03.742: I/Choreographer(2571): Skipped 30 frames! The application may be doing too much work on its main thread.
10-16 06:09:11.862: I/uploadFile(2571): HTTP Response is : Not Found: 404
修復您的DNS ... – 323go 2014-10-17 16:56:01
在瀏覽器子域中的工作沒有任何問題 – 2014-10-17 17:40:38