JAVA HTTP multipart Post funktioniert nicht

K

kriizz

Hallo,

ich bin gerade dabei mit JAVA einen FileUpload per HTTP Post zu realisieren.
Meiner Meinung nach wird hierbei auch alles gepostet. Jedoch funktioniert der upload trotzdem nicht.
Habe ich irgendwo, irgendetwas vergessen? Ich komme einfach nicht drauf, wieso der Upload nicht funktioniert.
Die Datei wird einfach nicht hochgeladen.

Das boundary wird beim initialisieren der Klasse erstellt.
Einen HTTP 200 bekomme ich beim Uploaden zurück. Somit sollte es ja eigentlich passen.

Der Post ist so aufgebaut, dass in der URL am anfang gleich die login daten mit gesendet werden.
Anschließend soll dann der Upload erfolgen.
Über ein normales HTML Formular "von Hand" funktioniert dies auch ohne Probleme. Nur mit JAVA bin ich gerade am verzweifeln.

MfG

Chris

public void upload() {

try {
URL urlToConnect = new URL("www.url.com/;internal&param=user;pw");
String paramToSend = "speicherort/online";

File fileToUpload = new File("data/testdoc.txt");

HttpURLConnection connection = (HttpURLConnection) urlToConnect.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
PrintWriter writer = null;
try {

String command1 = "";
command1 += boundary + "\r\n";
command1 += "Content-Disposition: form-data; name=\"speicherort\"\r\n";
command1 += "\r\n";
command1 += paramToSend + "\r\n";
command1 += boundary + "\r\n";
command1 += "Content-Disposition: form-data; name=\"uploadfile1\"; filename=\"testdoc.txt\"\r\n";
command1 += "Content-Type: text/plain\r\n";

String finalString = boundary + "--";

connection.setRequestProperty("POST", "www.url.com/;internal&param=user;pw HTTP/1.1");
connection.setRequestProperty("Host", " url.com:8101");
connection.setRequestProperty("User-Agent", "user agent string");// " Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 (.NET CLR 3.5.30729)");
connection.setRequestProperty("Accept", " text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
connection.setRequestProperty("Accept-Language", " de-de,de;q=0.8,en-us;q=0.5,en;q=0.3");
connection.setRequestProperty("Accept-Encoding", " gzip,deflate");
connection.setRequestProperty("Accept-Charset", " ISO-8859-1,utf-8;q=0.7,*;q=0.7");
connection.setRequestProperty("Keep-Alive", " 115");
connection.setRequestProperty("Connection", " keep-alive");

connection.setRequestProperty("Content-Length", "" + ((int) fileToUpload.length() + command1.length() + finalString.length()));


Map<String, List<String>> header = ((HttpURLConnection) connection).getRequestProperties();
Iterator i = header.entrySet().iterator();
while (i.hasNext()) {
Map.Entry entry = (Map.Entry) i.next();
System.out.println(entry.getKey() + ":"
+ entry.getValue().toString());
}

writer = new PrintWriter(new OutputStreamWriter(connection.getOutputStream(), "ISO-8859-1"));


BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileToUpload), "ISO-8859-1"));
for (String line; (line = reader.readLine()) != null;) {
command1 = command1 + line;

}
command1 = command1 + "\r\n";
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException logOrIgnore) {
}
}
}
command1 = command1 + finalString;
System.out.println(command1);
writer.println(command1);

} finally {
if (writer != null) {
writer.close();


}
}
int responseCode = ((HttpURLConnection) connection).getResponseCode();
String responseMsg = ((HttpURLConnection) connection).getResponseMessage();
String URL = ((HttpURLConnection) connection).getURL().toString();
String requestMethod = ((HttpURLConnection) connection).getRequestMethod();





System.out.println(responseCode + "|" + responseMsg + "|" + URL + "|" + requestMethod);
} catch (Exception ex) {
new Errors(ex);
}
}
 
Zurück