Answer the question
In order to leave comments, you need to log in
How to send a POST request to the server?
I'm trying to send a request to the server to send a file. I wanted to do this: parse the file into bytes, and then pass this array of bytes in the output stream to the script on the server. However, it looks like my request is not correct. And I have already broken my head trying to find a solution on the Internet.
Here is my attempt:
FileInputStream fis = new FileInputStream("c:\\test.txt");
int c = fis.available();
byte[] b = new byte[c];
fis.read(b);
fis.close();
URL url = new URL("http://en.gymn20.by/load.php");
HttpURLConnection htp = (HttpURLConnection) url.openConnection();
htp.setDoOutput(true);
htp.setRequestMethod("POST");
htp.setRequestProperty("Content-Type", "multipart/form-data");
htp.setRequestProperty("Content-Length", "254");
htp.connect();
OutputStream out = htp.getOutputStream();
out.write(b);
out.close();
<?php
echo $_FILES["filename"]["size"];
if($_FILES["filename"]["size"] > 1024*3*1024)
{
echo ("Размер файла превышает три мегабайта");
exit;
}
// Проверяем загружен ли файл
if(is_uploaded_file($_FILES["filename"]["tmp_name"]))
{
// Если файл загружен успешно, перемещаем его
// из временной директории в конечную
move_uploaded_file($_FILES["filename"]["tmp_name"], "/home/gymn20by/public_html/en/".$_FILES["filename"]["name"]);
} else {
echo("Ошибка загрузки файла");
}
?>
Answer the question
In order to leave comments, you need to log in
Are you sure that none of the pages suggested by Google answers this question?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question