It’s trivial to use a web form which is made by HTML code for uploading files from web browser to a server. This is commonly referred as form based file upload, and typically suitable for web-based applications. How about standalone or command line-based applications – which sometimes need to upload files to a web server but cannot use an upload form in HTML? Well, this article is going to answer that question by implementing the following solution:

    • Server: uses a Java servlet for receiving file uploaded from client and saves it into a file on server’s disk.
    • Client: is a command line based program that sends an HTTP POST request along with file’s binary data to the servlet.
We can call this method as non-form based file upload. Keep in mind that, this is not FTP upload, because it is still using HTTP protocol for transmitting the file.

Let’s examine how each part is implemented.

 

Coding the File Upload Servlet

Write a Java servlet as follows:

package net.codejava.servlet;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * This servlet demonstrates how to receive file uploaded from the client
 * without using third-party upload library such as Commons File Upload.
 * @author www.codejava.net
 */
public class ReceiveFileServlet extends HttpServlet {
	static final String SAVE_DIR = "E:/Test/Upload/";
	static final int BUFFER_SIZE = 4096;
	
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		// Gets file name for HTTP header
		String fileName = request.getHeader("fileName");
		File saveFile = new File(SAVE_DIR + fileName);
		
		// prints out all header values
		System.out.println("===== Begin headers =====");
		Enumeration<String> names = request.getHeaderNames();
		while (names.hasMoreElements()) {
			String headerName = names.nextElement();
			System.out.println(headerName + " = " + request.getHeader(headerName));			
		}
		System.out.println("===== End headers =====\n");
		
		// opens input stream of the request for reading data
		InputStream inputStream = request.getInputStream();
		
		// opens an output stream for writing file
		FileOutputStream outputStream = new FileOutputStream(saveFile);
		
		byte[] buffer = new byte[BUFFER_SIZE];
		int bytesRead = -1;
		System.out.println("Receiving data...");
		
		while ((bytesRead = inputStream.read(buffer)) != -1) {
			outputStream.write(buffer, 0, bytesRead);
		}
		
		System.out.println("Data received.");
		outputStream.close();
		inputStream.close();
		
		System.out.println("File written to: " + saveFile.getAbsolutePath());
		
		// sends response to client
		response.getWriter().print("UPLOAD DONE");
	}
} 
This servlet handles HTTP POST requests in its doPost()method. There are some important points here in this code:

    • Name of the upload file is retrieved from a HTTP header named as “fileName” – this header is set by the client.
    • We create a saveFile object in order to save the upload file into disk. Its path is constructed from the SAVE_DIR constant and the fileName header. A FileOutputStream object wraps this saveFile to write byte arrays into the file.
    • The most important point is to open an InputStream from the HttpServletRequest in order to read bytes transferred from the client.
    • We use a while loop to repeatedly read byte arrays from the request’s input stream and write them into the saveFile’s output stream. This is trivial.
    • And finally, always close the opened input stream and output stream, then send a response to the client (in this case, a simple text message “UPLOAD DONE”).
Now let’s examine how the client is implemented.

 

Coding the File Upload Client program

Following is code of the client program:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * This program demonstrates how to upload files to a web server
 * using HTTP POST request without any HTML form.
 * @author www.codejava.net
 *
 */
public class NonFormFileUploader {
	static final String UPLOAD_URL = "http://localhost:8080/CodeWeb/ReceiveFileServlet";
	static final int BUFFER_SIZE = 4096;

	public static void main(String[] args) throws IOException {
		// takes file path from first program's argument
		String filePath = args[0];
		File uploadFile = new File(filePath);

		System.out.println("File to upload: " + filePath);

		// creates a HTTP connection
		URL url = new URL(UPLOAD_URL);
		HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
		httpConn.setUseCaches(false);
		httpConn.setDoOutput(true);
		httpConn.setRequestMethod("POST");
		// sets file name as a HTTP header
		httpConn.setRequestProperty("fileName", uploadFile.getName());

		// opens output stream of the HTTP connection for writing data
		OutputStream outputStream = httpConn.getOutputStream();

		// Opens input stream of the file for reading data
		FileInputStream inputStream = new FileInputStream(uploadFile);

		byte[] buffer = new byte[BUFFER_SIZE];
		int bytesRead = -1;

		System.out.println("Start writing data...");

		while ((bytesRead = inputStream.read(buffer)) != -1) {
			outputStream.write(buffer, 0, bytesRead);
		}

		System.out.println("Data was written.");
		outputStream.close();
		inputStream.close();

		// always check HTTP response code from server
		int responseCode = httpConn.getResponseCode();
		if (responseCode == HttpURLConnection.HTTP_OK) {
			// reads server's response
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					httpConn.getInputStream()));
			String response = reader.readLine();
			System.out.println("Server's response: " + response);
		} else {
			System.out.println("Server returned non-OK code: " + responseCode);
		}
	}
}
The important point here is we use an HttpURLConnection object to send an HTTP POST request to the server at URL of the servlet (identified by the constant UPLOAD_URL) and open the OutputStream of this connection to write byte arrays of the upload file. Path of this file is passed as the first argument of the program.



Similarly to the servlet, we use a while loop to read/write byte arrays from/to the file’s input stream/request’s output stream.

Finally, we read response from the server after closing both input/output streams by using the input stream of the request.

 

Testing the File Upload application

Deploy the ReceiveFileServlet on a servlet container like Tomcat and start the server. Then run the client program by executing the following command:

java NonFormFileUploader e:\Test\RecordAudio.wav

Here we pass a WAV audio file to be uploaded. The server will produce some output as the following screenshot:

server output

From this log, we can examine values of all HTTP headers sent from the client. Almost of values are set by Java automatically, except the filename is set from the client manually.

And here is the output from the client program:

client output

NOTES:

    • Neither the server nor the client is playing with multipart request (which is standard for form-based upload). Note that the request’s content type is set to application/x-www-form-urlencoded instead of multipart/form-data.
    • Thus the client program cannot be applied for the server which accepts multipart request. The server side must be implemented like the ReceiveFileServletdoes.
 

Related Java File Upload Tutorials:

 

Other Java Servlet Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Attachments:
Download this file (NonFormFileUploader.java)NonFormFileUploader.java[Source code of the client program]2 kB
Download this file (ReceiveFileServlet.java)ReceiveFileServlet.java[Source code of the servlet]1 kB

Add comment

   


Comments 

#22Ab2017-06-15 10:54
Very useful article. Saved hours of my time
Quote
#21Jorge2017-04-13 18:26
This article is exactly what I needed. However, it leaves me with one question: What if don't want to use the client program and I want to use CURL? How would the http call look like?
Quote
#20enablers2016-04-19 10:18
Can any body tell me if I want to use the client program to send multiple files upload like form based. If I want to change this client to work for multiple file form based, do I need jsp page or I can do it same like run as background console program?
Quote
#19Vinayak2015-12-07 04:57
Very useful one. Saved my hours. Exact what I wanted to have
Quote
#18Nam2015-08-07 23:18
Quoting Gary:
Hello,

I am a beginner in java. how do you deploy ReceiveFileServlet in server? do i have to export it as .war file or something? I keep getting connection exception.

Thanks


Here you go: Quick start guide for Java servlet annotations
Quote