Код: Выделить всё
public class HttpUpload {
public static String uploadFile(File file) throws IOException {
String boundary = Long.toHexString(System.currentTimeMillis());
String charset = "UTF-8";
String filePath = file.getAbsolutePath();
String fileData = FileWorker.read(file);
URL url = new URL("http://localhost/demo_post/index_script.php");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
OutputStream os = connection.getOutputStream();
PrintWriter writer = new PrintWriter(new OutputStreamWriter(os, charset), true);
writer.println("--" + boundary);
writer.println("Content-Disposition: form-data; name=\"file\"; filename=\"" + filePath + "\"");
writer.println("Content-Type: image/x-png");
writer.println("Content-Transfer-Encoding: binary");
writer.println();
writer.println(fileData);
writer.println("--" + boundary + "--");
os.flush();
os.close();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
return in.readLine();
}
}
Приведите пожалуйста рабочий код для отправки картинки на сервер. Заранее благодарен
P.S: я гуглил и нечего не нашёл