src/test/java/org/jsoup/integration/ConnectTest.java
/**
     * Test fetching a form, and submitting it with a file attached.
     */
Test fetching a form, and submitting it with a file attached.
Test fetching a form and submitting it with a file attached .
@Test
    public void postHtmlFile() throws IOException {
        Document index = Jsoup.connect(FileServlet.urlTo("/htmltests/upload-form.html")).get();
        List<FormElement> forms = index.select("[name=tidy]").forms();
        assertEquals(1, forms.size());
        FormElement form = forms.get(0);
        Connection post = form.submit();
        File uploadFile = ParseTest.getFile("/htmltests/google-ipod.html.gz");
        FileInputStream stream = new FileInputStream(uploadFile);
        Connection.KeyVal fileData = post.data("_file");
        assertNotNull(fileData);
        fileData.value("check.html");
        fileData.inputStream(stream);
        Connection.Response res;
        try {
            res = post.execute();
        } finally {
            stream.close();
        }
        Document doc = res.parse();
        assertEquals(ihVal("Method", doc), "POST"); 
        assertEquals(ihVal("Part _file Filename", doc), "check.html");
        assertEquals(ihVal("Part _file Name", doc), "_file");
        assertEquals(ihVal("_function", doc), "tidy");
    }
@ Test public void postHtmlFile ( ) throws IOException { Document index = Jsoup . connect ( FileServlet . urlTo ( "/htmltests/upload-form.html" ) ) . get ( ) ; List < FormElement > forms = index . select ( "[name=tidy]" ) . forms ( ) ; assertEquals ( 1 , forms . size ( ) ) ; FormElement form = forms . get ( 0 ) ; Connection post = form . submit ( ) ; File uploadFile = ParseTest . getFile ( "/htmltests/google-ipod.html.gz" ) ; FileInputStream stream = new FileInputStream ( uploadFile ) ; Connection . KeyVal fileData = post . data ( "_file" ) ; assertNotNull ( fileData ) ; fileData . value ( "check.html" ) ; fileData . inputStream ( stream ) ; Connection . Response res ; try { res = post . execute ( ) ; } finally { stream . close ( ) ; } Document doc = res . parse ( ) ; assertEquals ( ihVal ( "Method" , doc ) , "POST" ) ; assertEquals ( ihVal ( "Part _file Filename" , doc ) , "check.html" ) ; assertEquals ( ihVal ( "Part _file Name" , doc ) , "_file" ) ; assertEquals ( ihVal ( "_function" , doc ) , "tidy" ) ; }
Test fetching a form, and submitting it with a file attached.
Test fetching a form and submitting it with a file attached .
// from form action