To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
<title>TODO supply a title</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<form action="fileup" method="post" enctype="multipart/form-data">
<h1>File Upload Application</h1>
Select File to Upload : <input type="file" name="ufile">
Destination : <input type="text" name="destination"><br>
<input type="submit" value="Upload FIle">
<h1>File Download Application</h1>
Click <a href="filedown?filename=download.pdf">TYBsc-IT Syllabus</a>
CLick <a href="filedown?filename=upg.pdf">University Exam Question Paper</a>
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.InputStream;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.Part;
public class fileup extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
out.println("<!DOCTYPE html>");
String path = request.getParameter("destination");
Part filePart = request.getPart("ufile");
String filename = filePart.getSubmittedFileName();
out.println("<br><br><hr>File Name: "+ filename);
os = new FileOutputStream(new File(path+File.separator+filename));
is = filePart.getInputStream();
while((read = is.read())!=1)
out.print("<br>File Uploaded Successfully");
catch(FileNotFoundException e)
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
public String getServletInfo() {
return "Short description";
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class filedown extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
out.println("<!DOCTYPE html>");
out.println("<title>Servlet filedown</title>");
response.setContentType("APPLICATION/OCTET-STREAM");
String filename = request.getParameter("filename");
ServletContext context = getServletContext();
try(InputStream is = context.getResourceAsStream("/"+filename)){
response.setHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");
while((i=is.read()) != -1)
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
public String getServletInfo() {
return "Short description";