jQuery in Action, 3rd edition
The moose likes Servlets and the fly likes How to Upload Files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to Upload Files" Watch "How to Upload Files" New topic

How to Upload Files

sovan chatt
Ranch Hand

Joined: Aug 09, 2010
Posts: 43
Can anybody give me a link to a tutorial that describes file uploading in a simple way?
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63540

Have you looked through the Servlet and JSP FAQs?

[Asking smart questions] [About Bear] [Books by Bear]
Eugene Rabii
Ranch Hand

Joined: Apr 24, 2009
Posts: 30
Servlet 3.0 @MultiPartConfig

JSP page:

<html xmlns="http://www.w3.org/1999/xhtml">
<title>Upload File</title>
<form action="uploadTest" method="post" enctype="multipart/form-data">
<td>Select File : </td>
<td><input name="fileToUpload" type="file"/> </td>
<input type="submit" value="Upload File"/>

Actual Servlet:

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MySecondServlet extends HttpServlet {

private static final long serialVersionUID = 11234354643L;

public void doPost(HttpServletRequest request, HttpServletResponse response){
try {
String fileName = MySecondServlet.getFileName(request.getPart("fileToUpload").getHeader("content-disposition"));
String outPutFile = this.getServletContext().getRealPath(fileName);
FileOutputStream os = new FileOutputStream(outPutFile);

InputStream is = request.getPart("fileToUpload").getInputStream();
int ch = is.read();
while(ch != -1){
ch = is.read();

} catch (IOException e) {
} catch (ServletException e) {

private static String getFileName(String neededHeader){
String fileName = null;
for(String onePiece : neededHeader.split(";") ){
String myPieces[] = onePiece.split("=");
fileName = myPieces[1].replaceAll("\"", "").trim();
// If we reach this if, then either the Header is deformated or it does not have the needed format
if(null == fileName) throw new IllegalArgumentException("The Header provided seems to be Invalid!");
return fileName;
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63540

Eugene Rabii, Please be sure to use code tags when posting code to the forums. Unformatted or unindented code is extremely hard. Also, be sure that someone is using a Servlet 3.0 container before posting a Servlets 3.0-specific solution. Such containers are not yet in wide use. Thanks.
Eugene Rabii
Ranch Hand

Joined: Apr 24, 2009
Posts: 30
Yes sir!
Consider Paul's rocket mass heater.
subject: How to Upload Files
jQuery in Action, 3rd edition