0

I want to upload a file (GPX file, which is similar to XML) to a URL (http://www.gpsvisualizer.com/map_input). Then wait for the map to be generated and retrieve it. The developer has stated this is possible, but I am not getting anywhere. Here is my code so far:

private static void postData(File fileName) {
    File input = fileName;

    //Setup connection
    URL url = new URL("http://www.gpsvisualizer.com/map");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setDoOutput(true);
    connection.setInstanceFollowRedirects(false);
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Type", "application/xml");
    connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; .NET CLR 1.2.30703)");

    //Send file
    OutputStream os = connection.getOutputStream();
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    FileReader fileReader = new FileReader(input);
    StreamSource source = new StreamSource(fileReader);
    StreamResult result = new StreamResult(os);
    transformer.transform(source, result);
    os.flush();

    //Retrieve response
    BufferedReader br = new BufferedReader(  
    new InputStreamReader(connection.getInputStream()));  
    String line = br.readLine();  
    while ( line != null ) {  
        System.out.println(line);  
        line = br.readLine();  
    }  
    br.close();
}

All this is doing, is setting up the connection, when I get the response, I am only getting the HTML of the initial page, not the (should be) map one.

4

1 回答 1

1

I realised I was almost doing this correctly. After speaking to the developer of the website, I realised I could send the GPX file as a String. (This is very simple) I then put this String into parameters which worked a treat!

String data = URLEncoder.encode("data", "UTF-8") + "=" + URLEncoder.encode("convertedGPX", "UTF-8");

OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
于 2013-03-28T15:18:27.287 回答