Ich sende Dateien per Post-Methode mit HttpURLConnection. Ich sende mit der Datei einen Parameter, der 'student_id' ist. Der Code funktioniert einwandfrei, wenn eine Datei in jeder Postanforderung gesendet wird. Aber wie kann ich den folgenden Code aktualisieren, um mehrere Dateien in einer Postanforderung zu senden, in der alle Dateien derselben 'student_id' gehören?Senden mehrerer Dateien in einem Beitrag Anfrage in HttpURLConnection in JAVA
try{
File textFile2 = new File("info.txt");
URL url = new URL("htttp://wwww.students.com");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setChunkedStreamingMode(0);
urlConnection.setDoOutput(true);
String boundary = Long.toHexString(System.currentTimeMillis()); // Just generate some unique random value.
urlConnection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
OutputStream output = urlConnection.getOutputStream();
PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, charset), true);
writer.append("--" + boundary).append(CRLF);
writer.append("Content-Disposition: form-data; name=\"student_id\"").append(CRLF);
writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF);
writer.append(CRLF).append("25").append(CRLF).flush();
writer.append("--" + boundary).append(CRLF);
writer.append("Content-Disposition: form-data; name=\"newfile\"; filename=\"" + textFile2.getName() + "\"").append(CRLF);
writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF); // Text file itself must be saved in this charset!
writer.append(CRLF).flush();
Files.copy(textFile2.toPath(), output);//copies all bytes in a file to the output stream
output.flush(); // Important before continuing with writer!
writer.append(CRLF).flush();
writer.append("--" + boundary + "--").append(CRLF).flush();
InputStream responseStream;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Ich versuchte ‚multiple‘ mit dem Parameter in ‚newfile‘ hinzufügen, aber es funktioniert nicht
writer.append("--" + boundary).append(CRLF);
writer.append("Content-Disposition: form-data; name=\"newfile\" multiple; filename=\"" + textFile2.getName() + "\"").append(CRLF);
writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF); // Text file itself must be saved in this charset!
writer.append(CRLF).flush();
Files.copy(textFile2.toPath(), output);//copies all bytes in a file to the output stream
output.flush(); // Important before continuing with writer!
writer.append(CRLF).flush();
writer.append("--" + boundary + "--").append(CRLF).flush();
Das Mischen eines Wrappers eines OutputStream mit direkten Schreibvorgängen in den ursprünglichen OutputStream erfordert Probleme. Verwenden Sie PrintStream anstelle von PrintWriter und übergeben Sie PrintStream an Files.copy. Sobald Sie einen OutputStream in etwas wie PrintStream oder OutputStreamWriter einschließen, sollten Sie nicht mehr auf diesen OutputStream verweisen. – VGR
Wenn es möglich ist, können Sie mir bitte ein Beispiel dafür zeigen, was Sie vorschlagen – ryh12
Ändern Sie 'PrintWriter writer = ... 'in' PrintStream writer = new PrintStream (Ausgabe, true, Zeichensatz); '. Ändern Sie die Anweisung Files.copy in 'Files.copy (textFile2.toPath(), writer);'. Auf diese Weise schreiben alle Zeilen auf das exakt gleiche Ausgabeobjekt. Entfernen Sie Ihren Aufruf von output.flush(). Und denken Sie daran, 'writer.close()' aufzurufen. – VGR