將upload.php放在Server
內容如下

<?php
  $filename = $_GET['filename'];
  $fileData = file_get_contents('php://input');
  $fhandle = fopen($filename, 'wb');
  fwrite($fhandle, $fileData);
  fclose($fhandle);
  echo("FROM PHP: Done uploading.");
?>


將網址取代為upload.php網址

try {
	HttpURLConnection httpUrlConnection = (HttpURLConnection) new URL("http://xxx.xxx.xx.xx/upload.php?filename=test2.txt").openConnection();
	httpUrlConnection.setDoOutput(true);
	httpUrlConnection.setRequestMethod("POST");
	OutputStream os = httpUrlConnection.getOutputStream();
	Thread.sleep(1000);
	BufferedInputStream fis = new BufferedInputStream(new FileInputStream("res/test1.txt"));

	int buf;
	while ((buf = fis.read()) != -1) {
		os.write(buf);
	}

	os.close();
	BufferedReader in = new BufferedReader(new InputStreamReader(
			httpUrlConnection.getInputStream()));

	String s = null;
	while ((s = in.readLine()) != null) {
		System.out.println(s);
	}
	in.close();
	fis.close();
} catch (IOException e) {
	e.printStackTrace();
} catch (InterruptedException e) {
	e.printStackTrace();
}
創作者介紹
創作者 隨手寫寫 的頭像
Gohome

隨手寫寫

Gohome 發表在 痞客邦 留言(0) 人氣( 49 )