Hallo zusammen,
kann mir bitte jemand helfen den Fehler zu finden?
Ich möchte mit diesem Code (danke an experts-exchange.com) ein File versenden.
Leider kommt es aber nicht an obwohl die Connection aufgebaut ist. Wenn ichs mit einem html-Form über den Browser mache gehts.
Mit Bestem Dank
xrax
kann mir bitte jemand helfen den Fehler zu finden?
Ich möchte mit diesem Code (danke an experts-exchange.com) ein File versenden.
Leider kommt es aber nicht an obwohl die Connection aufgebaut ist. Wenn ichs mit einem html-Form über den Browser mache gehts.
Mit Bestem Dank
xrax
Code:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class TestHttp {
public static void main(String[] args) throws Exception {
String url = "http://www.sport.de:80/api/external_writer.php?Token=2729cd0867d4bf60a1ac6da97375df";
String docPath = "D:\\SSW\\workspace\\Modules\\Test_out_result.xml"; // Document
HttpURLConnection httpcon = (HttpURLConnection) ((new URL(url).openConnection()));
httpcon.setDoOutput(true);
httpcon.setUseCaches(false);
httpcon.setRequestProperty("Content-Type", "multipart/form-data");
httpcon.connect();
System.out.println("Posting " +docPath +"...");
File file = new File(docPath);
FileInputStream is = new FileInputStream(file);
OutputStream os = httpcon.getOutputStream();
// POST
// Read bytes until EOF to write
byte[] buffer = new byte[4096];
int bytes_read; // How many bytes in buffer
while((bytes_read = is.read(buffer)) != -1) {
os.write(buffer, 0, bytes_read);
}
os.close();
is.close();
try {
InputStream in = httpcon.getInputStream();
int x;
while ( (x = in.read()) != -1)
{
System.out.write(x);
}
in.close();
BufferedReader r = new BufferedReader(new InputStreamReader(in));
StringBuffer buf = new StringBuffer();
String line;
while ((line = r.readLine())!=null) {
buf.append(line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Done...");
}
}
HTML:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<meta name="author" content="foo">
<meta name="editor" content="html-editor phase 5">
</head>
<body text="#000000" bgcolor="#FFFFFF" link="#FF0000" alink="#FF0000" vlink="#FF0000">
<form action="http://www.sport.de:80/api/external_writer.php?Token=2729cd0867d4bf60a1ac6da97375df"
enctype="multipart/form-data" method="post">
<p>
Please specify a file:<br>
<input type="file" name="datafile.xml" size="40">
</p>
<div>
<input type="submit" value="Send">
</div>
</form>
</body>
</html>