Home > Java > HTTP POST File Content in JAVA

HTTP POST File Content in JAVA

In Web scenario we have two method to send content to other page “GET” and “POST”. Though in “GET” method developers enjoys the flexibility of checking the content getting passed but there is limit for sending data. As compare to “GET” method “POST” enjoys the flexibility for sending unlimited data and using this method we can also send huge file content. This article will teach you on how we can send huge file content to remote host. To achieve this we will be using URLConnection class that allows you to connect with the remote URL page.

JSP Page Code

< %@page import="java.util.*,java.net.URL,java.net.URLConnection,java.net.URLEncoder,java.io.*"%>
< %
    URL  url;
    URLConnection urlConn;
    DataOutputStream printout;
    DataInputStream input;
 
    // URL of Remote Script.
    url = new URL ("http://localhost:8080/testpostdata");  //testpostdata is the servlet name
 
    // Open New URL connection channel.
    urlConn = url.openConnection();
 
    // Let the run-time system (RTS) know that we want input.
    urlConn.setDoInput (true);
 
    // we want to do output.
    urlConn.setDoOutput (true);
 
    // We want no caching
    urlConn.setUseCaches (false);
 
    // Specify the header content type.
    urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
 
    // Send POST output.
    printout = new DataOutputStream (urlConn.getOutputStream ());
 
    String content =
    "name=" + URLEncoder.encode ("Hitesh Agrawal") +
    "&profession=" + URLEncoder.encode ("Software Engineer");
 
    printout.writeBytes (content);
    printout.flush ();
    printout.close ();
 
    // Get response data.
    input = new DataInputStream (urlConn.getInputStream ());
    String str;
    while (null != ((str = input.readLine())))
    {
    	System.out.println (str);
	}
    input.close ();
 
%>

Your email:

 


Code Explanation for JSP:

  • Here we create an object of URLConnection by calling the openConnection() method of a URL object
  • After creating the object we set necessary attributes and headers required for sending data
  • After setting the headers i am writing the data on the OutputStream. Finally i am reading the response data.

testpostdata Servlet Code

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
 
public class testing extends HttpServlet {
 
	public void init(ServletConfig config) throws ServletException {	
		super.init(config);
	}
 
	public void destroy() {
	}
 
	public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException{
		String name = request.getParameter("name");
		String profession = request.getParameter("profession");
		OutputStream f0;
 
		byte buf[] = name.getBytes();
 
		f0 = new FileOutputStream("/home/hitesh/Desktop/postcontent.txt");
 
		for(int i=0;i<buf .length;i++) {
			f0.write(buf[i]);
		}
 
		f0.close();
		buf = null;
	}
}

Code Explanation for Servlet:

  • In doPost() i am retrieving the parameter send using request.getParameter
  • To crosscheck whether the data was send successfully i am creating a file and the file content are the data recieved through POST


Custom Search

Popular Articles:

Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • email
  • IndianPad
  • LinkedIn
  • Live
  • MySpace
  • Netvibes
  • RSS
  • Technorati
  • Yahoo! Bookmarks
  • Yahoo! Buzz
  • Reddit
  • Add to favorites
  • PDF
  • Twitter
Categories: Java Tags:
  1. Nahian
    December 16th, 2009 at 11:37 | #1

    how do i transfer files using this method (say a picture (jpeg)). I have tried but only 9-10 bytes get transferred.

  1. No trackbacks yet.