Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>All above answers are 100% correct. You can also use plain sockets, in which case your method would look like this:</p> <pre><code> // Compose the request header StringBuffer buf = new StringBuffer(); buf.append("POST "); buf.append(uploader.getUploadAction()); buf.append(" HTTP/1.1\r\n"); buf.append("Content-Type: multipart/form-data; boundary="); buf.append(boundary); buf.append("\r\n"); buf.append("Host: "); buf.append(uploader.getUploadHost()); buf.append(':'); buf.append(uploader.getUploadPort()); buf.append("\r\n"); buf.append("Connection: close\r\n"); buf.append("Cache-Control: no-cache\r\n"); // Add cookies List cookies = uploader.getCookies(); if (!cookies.isEmpty()) { buf.append("Cookie: "); for (Iterator iterator = cookies.iterator(); iterator.hasNext(); ) { Parameter parameter = (Parameter)iterator.next(); buf.append(parameter.getName()); buf.append('='); buf.append(parameter.getValue()); if (iterator.hasNext()) buf.append("; "); } buf.append("\r\n"); } buf.append("Content-Length: "); // Request body StringBuffer body = new StringBuffer(); List fields = uploader.getFields(); for (Iterator iterator = fields.iterator(); iterator.hasNext();) { Parameter parameter = (Parameter) iterator.next(); body.append("--"); body.append(boundary); body.append("\r\n"); body.append("Content-Disposition: form-data; name=\""); body.append(parameter.getName()); body.append("\"\r\n\r\n"); body.append(parameter.getValue()); body.append("\r\n"); } body.append("--"); body.append(boundary); body.append("\r\n"); body.append("Content-Disposition: form-data; name=\""); body.append(uploader.getImageFieldName()); body.append("\"; filename=\""); body.append(file.getName()); body.append("\"\r\n"); body.append("Content-Type: image/pjpeg\r\n\r\n"); String boundary = "WHATEVERYOURDEARHEARTDESIRES"; String lastBoundary = "\r\n--" + boundary + "--\r\n"; long length = file.length() + (long) lastBoundary.length() + (long) body.length(); long total = buf.length() + body.length(); buf.append(length); buf.append("\r\n\r\n"); // Upload here InetAddress address = InetAddress.getByName(uploader.getUploadHost()); Socket socket = new Socket(address, uploader.getUploadPort()); try { socket.setSoTimeout(60 * 1000); uploadStarted(length); PrintStream out = new PrintStream(new BufferedOutputStream(socket.getOutputStream())); out.print(buf); out.print(body); // Send the file byte[] bytes = new byte[1024 * 65]; int size; InputStream in = new BufferedInputStream(new FileInputStream(file)); try { while ((size = in.read(bytes)) &gt; 0) { total += size; out.write(bytes, 0, size); transferred(total); } } finally { in.close(); } out.print(lastBoundary); out.flush(); // Read the response BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); while (reader.readLine() != null); } finally { socket.close(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload