Http POST in Java (with file upload)

前端 未结 5 1130
野的像风
野的像风 2020-12-14 03:33

What I want to do is submit a web form from a java application. The form I need to fill out is located here: http://cando-dna-origami.org/

When the form is submitte

5条回答
  •  南笙
    南笙 (楼主)
    2020-12-14 03:43

    You should definitively use apaches HTTPClient for that job! It makes life much easier. Here is an example how to upload a file with apaches HttpClient.

    byte[] data = outStream.toByteArray()
    HttpClient client = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost("http://localhost:8080/YourResource");
    
    ByteArrayBody byteArrayBody = new ByteArrayBody(data, "application/json", "some.json");
    MultipartEntity multipartEntity = new MultipartEntity();
    multipartEntity.addPart("upload", byteArrayBody);
    httpPost.setEntity( multipartEntity );
    
    HttpResponse response = client.execute(httpPost);
    Reader reader = new InputStreamReader(response.getEntity().getContent());
    

    Let me know if you have further questions.

提交回复
热议问题