Searching...
Monday, 13 January 2014

How to upload file using Servlet 3.0 ?

09:18

javax.servlet.http.Part class represents a part as uploaded to the server as part of a multipart/form-data request body. The part may represent either an uploaded file or form data. Here is one simple example to upload file using Servlet 3.0

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <form method="post" action="/FileUploadServletExample/fileupload" enctype="multipart/form-data">  
        Choose file <input type="file" name="file">
        <input type="submit" value="submit">
    </form>
</body>
</html>

FileUploadServlet.java

package com.pretech;
 
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
 
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;
import javax.servlet.http.Part;
 
@MultipartConfig         
@WebServlet("/fileupload")
public class FileUploadServlet extends HttpServlet {
 
	private static final long serialVersionUID = -7205406034336084784L;
	protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        Part filePart = request.getPart("file");
        System.out.println("filename"+ filePart.getName());
        String fileName = getFileName(filePart);
        String fileLocation="c:/temp/";
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            File outputFilePath = new File(fileLocation + fileName);
            inputStream = filePart.getInputStream();
            outputStream = new FileOutputStream(outputFilePath);
            int read = 0;
            final byte[] bytes = new byte[1024];
            while ((read = inputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, read);
            }
        } catch (FileNotFoundException fne) {
            fne.printStackTrace();
        } finally {
            if (outputStream != null) {
                outputStream.close();
            }
            if (inputStream != null) {
                inputStream.close();
            }
        } 
        response.setContentType("text/html;UTF-8");
        PrintWriter writer = response.getWriter();
        writer.write("File upload completed");     
        writer.close();
    }
 
    private String getFileName(Part part) {
        for (String content : part.getHeader("content-disposition").split(";")) {
            if (content.trim().startsWith("filename")) {
                return content.substring(content.indexOf('=') + 1).trim()
                        .replace("\"", "");
            }
        }
        return null;
    }
}

Output



image


image


0 comments: