2013-03-18 49 views
0

我已經創建了一個應用程序,使用它我可以將存儲在手機畫廊中的圖像上傳到服務器。我使用的服務器端語言是PHP。我面臨的問題有點奇怪。我可以上傳使用手機相機拍攝的尺寸小於2 MB的照片。但某些照片沒有上傳。以下是這並沒有獲得上傳照片的細節:有關從Android上傳圖像文件到服務器的問題

第一張照片:

標題:2013年3月15日09.17.22.jpg

類型:JPEG

尺寸: 2645Kb

第二張照片:

鈦TLE:2013年3月12日11.48.04.jpg

類型:JPEG

大小:2781Kb

的上述照片上傳失敗後,我試着上傳另一張照片和它的工作。這裏有照片,我是能夠上傳細節:

標題:2013年3月14日10.20.16.jpg

類型:JPEG

大小:1238Kb

然後試圖只有少於2000 kb的其他照片以及上傳的所有照片。

然後我從網上下載一張6MB的jpg圖像,並嘗試上傳它。令人驚訝的是它上傳了。現在我完全困惑,真正的問題是什麼。最後,我得出的結論是,問題僅在於使用手機相機拍攝的照片大小超過2000 kb。

這裏是我使用的照片上傳到服務器Android的功能:

public String doFileUpload(String selectedPath,String page) 
     { 
      String response=null; 
      HttpURLConnection conn = null; 
      DataOutputStream dos = null; 
      DataInputStream inStream = null; 
      String lineEnd = "\r\n"; 
      String twoHyphens = "--"; 
      String boundary = "*****"; 
      int bytesRead, bytesAvailable, bufferSize; 
      byte[] buffer; 
      int maxBufferSize = 1*1024*1024; 
      String urlString = Connector.URL+page; 
      try 
      { 
       //------------------ CLIENT REQUEST 
       FileInputStream fileInputStream = new FileInputStream(new File(selectedPath)); 
       // open a URL connection to the Servlet 
       URL url = new URL(urlString); 
       // Open a HTTP connection to the URL 
       conn = (HttpURLConnection) url.openConnection(); 
       // Allow Inputs 
       conn.setDoInput(true); 
       // Allow Outputs 
       conn.setDoOutput(true); 
       // Don't use a cached copy. 
       conn.setUseCaches(false); 
       // Use a post method. 
       conn.setRequestMethod("POST"); 
       conn.setRequestProperty("Connection", "Keep-Alive"); 
       conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary); 
       dos = new DataOutputStream(conn.getOutputStream()); 
       dos.writeBytes(twoHyphens + boundary + lineEnd); 
       dos.writeBytes("Content-Disposition: form-data;name=uploadedfile;filename=" + selectedPath + "" + 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); 
       // close streams 
       fileInputStream.close(); 
       dos.flush(); 
       dos.close(); 
      } 
      catch (MalformedURLException ex) 
      { 
       Log.e("Debug", "error: " + ex.getMessage(), ex); 
      } 
      catch (IOException ioe) 
      { 
       Log.e("Debug", "error: " + ioe.getMessage(), ioe); 
      } 
      //------------------ read the SERVER RESPONSE 
      try 
      { 
       inStream = new DataInputStream (conn.getInputStream()); 
       response = inStream.readLine(); 
       inStream.close(); 

      } 
      catch (IOException ioex){ 
       Log.e("Debug", "error: " + ioex.getMessage(), ioex); 
      } 
      return response; 
     } 

下面是頁面的PHP代碼,我用它來上傳圖片:

<?php 
$tt=$_GET["id"]; 
// Where the file is going to be placed 
$target_path = "uploads/"; 

/* Add the original filename to our target path. 
Result is "uploads/filename.extension" */ 
$target_path = $target_path . basename($_FILES['uploadedfile']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) 
{ 
    include("../../classes/Registration.php"); 
    $Obj=new register(); 
    $Obj->insert_photo_data($tt,basename($_FILES['uploadedfile']['name'])); 
} 
else 
{ 
    echo '[{"flag":"0"}]'; 
} 
?> 

當圖像上傳問題發生時我回來的是:

[{"flag":"0"}] 
+0

嘗試使用位圖來壓縮最終會上傳的圖片的大小。在上傳全尺寸的移動圖像方面用處不大。在將流傳輸到網絡上進行上傳之前,任何服務(例如Google plus)都將進行壓縮 – 2013-03-18 15:25:25

回答

0

我找到了解決方案。這個問題與wamp有關。我已經打開了php。ini和改線

upload_max_filesize = 2M 

upload_max_filesize = 10M 

現在,圖像越來越上傳沒有任何問題。

1

更改。 dos.write(buffer,0,bufferSize);到dos.write(buffer,0,bytesRead)。如果圖片未上傳,到達服務器的內容是什麼?

相關問題