0
我在Windows 10計算機上構建了一個基於WAMP服務器的PHP服務器。我想要做的是當我發送一個GET請求時,show_files.php
應該返回一個JSON對象給我。 JSON對象包含我電腦上路徑F:\NetEaseMusic\download
中的文件名。然後,我使用文件名向download_file.php
發送POST請求,並返回一個數據流,以便我可以下載文件。當我使用HttpURLConnection
時,一切正常。但是,當我嘗試發送POST請求使用套接字時,download_file.php
可以獲得file_name
參數,但它在F:\NetEaseMusic\download
中找不到目標文件。我展示了代碼。 這是PHP在窗口中找不到文件
這是
download_file.php
<?php
if(empty($_POST["file_name"]))
{
echo "NO_FILE_NAME\n";
print_r($_POST);
exit();
}
$path = iconv("utf-8", "GB2312","F:\\NetEaseMusic\\download\\".$_POST["file_name"]);
//$path = "F:\\NetEaseMusic\\download\\".$_POST["file_name"];
if (!file_exists ($path)) {
echo "FILE_NOT_FOUND\n";
echo "F:\\NetEaseMusic\\download\\".$_POST["file_name"]."\n";
print($path);
exit();
}
$file_size = filesize($path);
//header("Content-type: application/octet-stream");
//header("Accept-Ranges: bytes");
//header("Accept-Length:".$file_size);
//header("Content-Disposition: attachment; filename=".$path);
$file = fopen($path, "r");
while(!feof($file))
{
echo fread($file, 1024);
}
exit();
?>
這是我的客戶端代碼來下載文件。首先,我建立一個HTTP POST請求,
private void downloadFileBySocket(String urlString, String fileName)
{
try{
StringBuilder sb = new StringBuilder();
String data = URLEncoder.encode("file_name", "utf-8") + "=" + URLEncoder.encode(fileName, "utf-8") + "\r\n";
//String data = "&file_name="+fileName;
sb.append("POST " + urlString + " HTTP/1.1\r\n");
sb.append("Host: 10.206.68.242\r\n");
sb.append("Content-Type: application/x-www-form-urlencoded\r\n");
sb.append("Content-Length: " + data.length() + "\r\n");
sb.append("\r\n");
sb.append(data + "\r\n");
//sb.append(URLEncoder.encode("file_name", "utf-8") + "=" + URLEncoder.encode(fileName, "utf-8") + "\r\n");
System.out.println(sb.toString());
URL url = new URL(urlString);
Socket socket = new Socket(url.getHost(), url.getPort());
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "utf-8"));
writer.write(sb.toString());
writer.flush();
File file = new File("./" + fileName);
DataOutputStream out = null;
DataInputStream in = null;
try{
out = new DataOutputStream(new FileOutputStream(file));
in = new DataInputStream(socket.getInputStream());
byte[] buffer = new byte[1024];
int readBytes = 0;
while((readBytes = in.read(buffer)) != -1)
{
out.write(buffer, 0, readBytes);
}
out.flush();
}catch (Exception e1)
{
e1.printStackTrace();
}finally {
try{
if(in != null)
{
in.close();
}
if(out != null)
{
out.close();
}
}catch (Exception e2)
{
e2.printStackTrace();
}
}
socket.close();
}catch (Exception e)
{
e.printStackTrace();
}
}
和我的主[]方法
public static void main(String[] args)
{
SocketTest socketTest = new SocketTest();
socketTest.downloadFileBySocket(SocketTest.downloadFileUrl, "小胡仙兒 - 【二胡】霜雪千年.mp3");
}
HTTP:// WWW .csharp-examples.net/download-files/ – 2017-02-27 08:51:42
我發起了它。我只是從'data'中移除「\ r \ n」,它就可以工作。但是,我遇到了另一個問題。我發現'\ r \ n2000 \ r \ n'會在我下載一個大文件時被添加到套接字輸入流中。我寫在插座學習網絡。 – zuguorui
http://stackoverflow.com/questions/25971067/response-xml-contains-2000-and-20a0-characters – 2017-02-27 12:43:28