S
S
Saymon_K2015-02-01 20:24:11
Java
Saymon_K, 2015-02-01 20:24:11

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();

Server script:
<?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("Ошибка загрузки файла");
   }
?>

Gives Error loading file.
And more Warning: Missing boundary in multipart/form-data POST data in Unknown on line 0
Please help. Thanks in advance!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
IceJOKER, 2015-02-01
@IceJOKER

Are you sure that none of the pages suggested by Google answers this question?
b2b3c1670c.jpg

O
one pavel, 2015-02-01
@onepavel

You seem to have forgotten about the boundary and IceJOKER is right as always

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question