android上传文件到服务器得到404错误

时间:2014-10-17 14:57:22

标签: php android file-upload http-status-code-404

我有一个Android应用程序将文件上传到服务器,它在本地主机上没有任何问题,但在服务器上没有工作,我检查了路径并且它正确,

日志cat错误:未找到:404

如果我将网址粘贴到浏览器中,则将其复制

我在同一个地址(up3.php)中有另一个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 

Log cat:

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

2 个答案:

答案 0 :(得分:0)

http://live.mysite.com/up-file.php会抛出404错误,这就是它无效的原因

$ wget http://live.mysite.com/up-file.php
--2014-10-17 17:00:40--  http://live.mysite.com/up-file.php
Résolution de live.mysite.com (live.mysite.com)... 64.136.20.37
Connexion vers live.mysite.com (live.mysite.com)|64.136.20.37|:80...connecté.
requête HTTP transmise, en attente de la réponse...404 Not Found
2014-10-17 17:00:41 ERREUR 404: Not Found.

在浏览器中,它显示了一个花哨的404页面,但仍然是404

  

404错误 - 找不到文件   您要查找的页面或文件不在此处。

答案 1 :(得分:-1)

我找到了解决方案,我不知道为什么但httprequest找不到子域名, 而不是http://live.mysite.com/up.php它必须是http://www.live.mysite.com/up.php,如果有人知道为什么会发生这种情况告诉我plz