user2724130
user2724130

Reputation: 11

Unable to upload a document using HttpPost

I am trying to upload a document from my local machine to Http using following code but I am getting HTTP 400 Bad request error. My source data is in Json.

URL url = null;
boolean success = false;

try {
        FileInputStream fstream;
        @SuppressWarnings("resource")
        BufferedReader bufferedReader = new BufferedReader(new FileReader("C:\\Users\\Desktop\\test.txt"));
        StringBuffer buffer = new StringBuffer();
        String line = null;

        while ((line = bufferedReader.readLine()) != null) {
            buffer.append(line);
        }

        String request = "http://example.com";
        URL url1 = new URL(request);
        HttpURLConnection connection = (HttpURLConnection) url1.openConnection();
        connection.setDoOutput(true); // want to send
        connection.setRequestMethod("POST");
        connection.setAllowUserInteraction(false); // no user interaction
        connection.setRequestProperty("Content-Type", "application/json");


        DataOutputStream wr = new DataOutputStream(
        connection.getOutputStream());
        wr.flush();
        wr.close();
        connection.disconnect();


        System.out.println(connection.getHeaderFields().toString());

        // System.out.println(response.toString());
} catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
} catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
}

Upvotes: 1

Views: 78

Answers (2)

Jon Lin
Jon Lin

Reputation: 143866

The DataOutputStream is for writing primitive types. This causes it to add extra data to the stream. Why aren't you just flushing the connection?

connection.getOutputStream().flush();
connection.getOutputStream().close();

EDIT:

It also occurs to me that you've not actually written any of your post data, so you probably want a something more like:

OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
wr.write(buffer.toString());
wr.close();

Upvotes: 2

Kris
Kris

Reputation: 5792

Have a look into apache http libraries which will help a lot with that:

File file = new File("path/to/your/file.txt");
try {
         HttpClient client = new DefaultHttpClient();  
         String postURL = "http://someposturl.com";
         HttpPost post = new HttpPost(postURL); 
         FileBody bin = new FileBody(file);
         MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);  
         reqEntity.addPart("myFile", bin);
         post.setEntity(reqEntity);  
         HttpResponse response = client.execute(post);  
         HttpEntity resEntity = response.getEntity();  

         if (resEntity != null) {    
               Log.i("RESPONSE",EntityUtils.toString(resEntity));
         }

} catch (Exception e) {
    e.printStackTrace();
}

The example above is taken from my blog and it should work with standard Java SE as well as Android.

Upvotes: 2

Related Questions