Memuat Naik Fail dengan Permintaan HTTP
Untuk memuat naik fail ke pelayan HTTP sambil turut menyerahkan parameter tambahan, java.net.URLConnection dan multipart/ pengekodan borang-data biasanya digunakan. Berikut ialah pecahan proses:
Pengekodan Berbilang Bahagian/Data Borang
Data Berbilang Bahagian/Borang direka untuk permintaan POST yang menggabungkan kedua-dua binari (mis., fail) dan data aksara (cth., parameter). Pengekodan melibatkan membahagikan badan permintaan kepada beberapa bahagian, setiap satu diawali dengan rentetan sempadan.
Kod Contoh
<code class="java">import java.io.File; import java.io.OutputStream; import java.io.PrintWriter; import java.net.URLConnection; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; public class FileUpload { public static void main(String[] args) throws Exception { String url = "http://example.com/upload"; String param = "value"; File textFile = new File("/path/to/file.txt"); File binaryFile = new File("/path/to/file.bin"); String boundary = Long.toHexString(System.currentTimeMillis()); URLConnection connection = new URL(url).openConnection(); connection.setDoOutput(true); connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); try (OutputStream output = connection.getOutputStream(); PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, StandardCharsets.UTF_8), true)) { // Send parameter writer.append("--" + boundary).append("\r\n"); writer.append("Content-Disposition: form-data; name=\"param\"").append("\r\n"); writer.append("Content-Type: text/plain; charset=" + StandardCharsets.UTF_8).append("\r\n"); writer.append("\r\n").append(param).append("\r\n").flush(); // Send text file writer.append("--" + boundary).append("\r\n"); writer.append("Content-Disposition: form-data; name=\"textFile\"; filename=\"" + textFile.getName() + "\"").append("\r\n"); writer.append("Content-Type: text/plain; charset=" + StandardCharsets.UTF_8).append("\r\n"); writer.append("\r\n").flush(); Files.copy(textFile.toPath(), output); output.flush(); writer.append("\r\n").flush(); // Send binary file writer.append("--" + boundary).append("\r\n"); writer.append("Content-Disposition: form-data; name=\"binaryFile\"; filename=\"" + binaryFile.getName() + "\"").append("\r\n"); writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(binaryFile.getName())).append("\r\n"); writer.append("Content-Transfer-Encoding: binary").append("\r\n"); writer.append("\r\n").flush(); Files.copy(binaryFile.toPath(), output); output.flush(); writer.append("\r\n").flush(); // End of multipart/form-data writer.append("--" + boundary + "--").append("\r\n").flush(); } int responseCode = ((HttpURLConnection) connection).getResponseCode(); System.out.println("Response Code: " + responseCode); } }</code>
Nota Tambahan
Atas ialah kandungan terperinci Bagaimana untuk memuat naik fail dengan parameter tambahan menggunakan java.net.URLConnection dan pengekodan multipart/form-data?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!