2011-08-03 136 views
-1

這是我在我的Java應用程序中使用的方法。它正確地讀取字節,我已經記錄下來看它是否是。問題是,PHP沒有意識到數據在那裏。我已經測試過,並且.php讀取$ _POST,但它是空的。Java使用php POST數據將.png上傳到服務器

public void screenshot(BufferedImage screenshot) { 
    try { 
     ImageIO.write(screenshot, "png", 
       new File(Environment.getStorageDirectory().toString() 
         .concat(File.separator + SCRIPT_NAME + ".png"))); 

     HttpURLConnection httpUrlConnection; 
     OutputStream outputStream; 
     BufferedInputStream fileInputStream; 
     BufferedReader serverReader; 
     int totalBytes; 
     String response = ""; 
     String serverResponse = ""; 
     String localFileName = Environment.getStorageDirectory().toString() 
       .concat(File.separator + SCRIPT_NAME + ".png"); 

     // Establish a connection 
     httpUrlConnection = (HttpURLConnection) new URL(
       "http://www.scripted.it/scriptoptions/utils/saveScreenshot.php?user=" 
         + SupraCrafter.statHandler.getUser()) 
       .openConnection(); 
     httpUrlConnection.setDoOutput(true); 
     httpUrlConnection.setDoInput(true); 
     httpUrlConnection.setRequestMethod("POST"); 
     httpUrlConnection.setRequestProperty("Content-type", 
       "application/x-www-form-urlencoded"); 
     outputStream = httpUrlConnection.getOutputStream(); 

     // Buffered input stream 
     fileInputStream = new BufferedInputStream(new FileInputStream(
       localFileName)); 

     // Get the size of the image 
     totalBytes = fileInputStream.available(); 

     // Loop through the files data 
     for (int i = 0; i < totalBytes; i++) { 
      // Write the data to the output stream 
      outputStream.write(fileInputStream.read()); 
     } 

     // Close the output stream 
     outputStream.close(); 

     // New reader to get server response 
     serverReader = new BufferedReader(new InputStreamReader(
       httpUrlConnection.getInputStream())); 

     // Read the servers response 
     serverResponse = ""; 
     while ((response = serverReader.readLine()) != null) { 
      serverResponse = serverResponse + response; 
     } 

     System.out.println(serverResponse); 

     // Close the buffered reader 
     serverReader.close(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    try { 
     URL url = new URL(
       "http://scripted.it/scriptoptions/utils/setScreenshotStatus.php?user=" 
         + SupraCrafter.statHandler.getUser() + "&pass=" 
         + SupraCrafter.statHandler.getPass() + "&script=" 
         + SCRIPT_NAME + "&status=1"); 
     BufferedReader in = new BufferedReader(new InputStreamReader(
       url.openStream())); 
     in.close(); 
    } catch (MalformedURLException e) { 
    } catch (IOException e) { 
    } 
} 

這裏是PHP文件:

<? 
// Config 
$uploadBase = "../screenshots/"; 
$uploadFilename = $_GET['user'] . ".png"; 
$uploadPath = $uploadBase . $uploadFilename; 

// Upload directory 
if(!is_dir($uploadBase)) 
mkdir($uploadBase); 

// Grab the data 
$incomingData = file_get_contents('php://input'); 

// Valid data? 
if(!$incomingData) 
die("No input data"); 

// Write to disk 
$fh = fopen($uploadPath, 'w') or die("Error opening file"); 
fwrite($fh, $incomingData) or die("Error writing to file"); 
fclose($fh) or die("Error closing file"); 

echo "Success"; 
?> 

它總是回聲 '沒有輸入數據。'

+0

請把你的HTML表單,以及。 – 2011-08-03 01:06:17

+0

content-type應該是* multipart/form-data * –

+0

沒有html表單。我試着將它改爲multipart/form-data,它也做了同樣的事情。 – Sam

回答

0

您沒有使用application/x-www-form-urlencoded編碼內容。您不應該簡單地將字節複製到HTTP有效負載中,而是將其正確編碼。

application/x-www-form-urlencoded不是編碼它的唯一可能方式,multipart/form-data是另一種常見選擇。幾乎所有的網絡服務器都支持這兩種方式,並因此受到PHP的支持。

如何使用Java編碼教程是在這裏:http://www.devx.com/Java/Article/17679

你爲什麼不使用Apache的HttpClient的或類似的庫已經做繁瑣的工作適合你?

的Apache的HttpClient:http://hc.apache.org/httpcomponents-client-ga/

+0

我不知道該怎麼做。你能把我鏈接到一個教程或網站解釋它嗎?基本上我只是試圖上傳本地.png到服務器。 – Sam

+0

爲您編輯了幾個鏈接。 –

+0

該devx網址並沒有告訴我如何使用java編碼。而且我不確定如何在第3頁使用Apache客戶端 – Sam

相關問題