0

I have a desktop app that call a servlet through the method:

    public int[] identify(String path) {

    try {

        URL url = new URL("http://localhost:8084/my-webapp/upload");
        URLConnection conn = url.openConnection();
        conn.setDoOutput(true);

        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream()));

        out.write("path=" + path);
        out.flush();
        out.close();
        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));

        String response;
        while ((response = in.readLine()) != null) {
            int[] numbers = convertOutput(response);
            s.showOutput(numbers);
            return numbers;
        }
        in.close();
    } catch (MalformedURLException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        return null;
    }

I have no jsp file. It's executed by the desktop app and it works. But now when I call my servlet I need to pass to the servlet an object that I've previously instantiated. How can i do that?

Max
  • 1
  • 1
  • Just write it to out..? – BalusC Jun 06 '16 at 10:48
  • How can I write an object through a BufferedWriter? – Max Jun 06 '16 at 10:53
  • Upon closer inspection it appears that you want to programmatically perform a file upload over HTTP, is this true? If to, then you can find an example near the bottom of the top answer of: http://stackoverflow.com/questions/2793150/using-java-net-urlconnection-to-fire-and-handle-http-requests Let me know if that indeed solved your problem. – BalusC Jun 06 '16 at 10:57
  • Yes, I upload an image, process it on server side and then i get an output through the String response – Max Jun 06 '16 at 11:00
  • OK, as I guessed. This was already asked and answered before. – BalusC Jun 06 '16 at 11:12
  • It didn't solve my problem. Can you explain me how I can write an object through my BufferedWriter? – Max Jun 06 '16 at 11:42
  • Get an `InputStream` of it and write it to `OutputStream`. It can really be any `InputStream` you want, such as `FileInputStream` as shown in the duplicate which exactly fits your "upload file" use case, or even `ObjectInputStream` if it's a `Serializable` Java object. – BalusC Jun 06 '16 at 11:43

0 Answers0