Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I'm very new to geoserver (and GIS in general) so any help will be appreciated.

I have an imagemosaic datastore and an associated layer in geoserver. geoserver generated my .shp file and a number of other files, i only supplied the geotiffs. now i need to add imagery to the datastore programmatically.

i can be flexible with my solution. i can get the new imagery on to my server if i need to, but a solution that uploads it automatically would be nice.

i've been fighting on two fronts: java and curl. java is preferable but i would prefer to not use external libs.

my curl command is looking like this:

    curl.exe --user admin:geoserver -v -XPOST -H "Content-type: text/plain" -d "file:///c:\dev\mosaictest" http://localhost:8080/geoserver/rest/workspaces/cite/coveragestore/mosaic_test/external.imagemosaic?recalculate=nativebbox,latlonbox

I get 202 accepted, and the timestamps on my .shp, etc are updated. but when i preview the layer, no new imagery is visible.

in java, i'm trying to send a zip file like so:

    httpPost("http://localhost:8080/geoserver/rest/workspaces/cite/coveragestores/mosaic_test/external.imagemosaic?recalculate=nativebbox,latlonbbox", "C:\\dev\\GeoserverCommunicator\\imagery2.zip");
    public static boolean httpPost(String urlToConnect, String path) throws IOException {
        File fileToUpload = new File(path);
        String boundary = Long.toHexString(System.currentTimeMillis()); // Just generate some unique random value.

        String CRLF = "\r\n"; // Line separator required by multipart/form-data.
        HttpURLConnection connection = (HttpURLConnection)(new URL(urlToConnect).openConnection());
        String login = "admin:geoserver";
        String encodedLogin = DatatypeConverter.printBase64Binary(login.getBytes());
        connection.setRequestProperty("Authorization", "Basic " +encodedLogin);
        connection.setDoOutput(true); // This sets request method to POST.
        connection.setRequestMethod("PUT"); //type: POST, PUT, DELETE, GET
        connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
        OutputStream stream = connection.getOutputStream();
        PrintWriter writer = null;
        try {
            writer = new PrintWriter(new OutputStreamWriter(connection.getOutputStream(), "UTF-8"), true);

            writer.append("--" + boundary).append(CRLF);
            writer.append("Content-Disposition: form-data").append(CRLF);
            writer.append("Content-Type: application/zip").append(CRLF);

            InputStream input = new FileInputStream(fileToUpload);
            try {
                byte[] buffer = new byte[1024];
                for (int length = 0; (length = input.read(buffer)) > 0;) {
                    stream.write(buffer, 0, length);
                stream.flush(); // Important! Output cannot be closed. Close of
                                // writer will close output as well.
            } finally {
                try { input.close(); } catch (IOException logOrIgnore) {}
            writer.append("--" + boundary + "--");
        } finally {
            if (writer != null) writer.close();

        // Connection is lazily executed whenever you request any status.
//      int responseCode = ((HttpURLConnection) connection).getResponseCode();

        InputStream error = ((HttpURLConnection) connection).getInputStream();
        if (error != null)
            byte[] errBytes = new byte[1024];
            StringBuilder str = new StringBuilder(); 
            for (int length = 0; (length = error.read(errBytes)) > 0;) 
                str.append(new String(errBytes));

//      System.out.println(responseCode); // Should be 200
        return true;

i'm getting error 500. i think i'm close, but i'm clearly missing something.

share|improve this question
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.