Create a Servlet application to upload and download a file.

~ index.html ~ 

 <html> 

    <body> 

    <form action="FileUploadServlet" method="post" enctype="multipart/form-data"> 

    Select File to Upload:<input type="file" name="file" id="file"> 

    Destination <input type="text" value="/tmp" name="destination"> 

    <br> 

    <input type="submit" value="Upload file" name="upload" id="upload">     </form> 

    </body> 

</html>

~ FileUploadServlet.java ~ 


package fileservletapp; 
 
import java.io.*; 
import javax.servlet.*; 
import javax.servlet.annotation.MultipartConfig; 
import javax.servlet.http.*; 
 
@MultipartConfig 
public class FileUploadServlet extends HttpServlet { 
public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException 
{     
   res.setContentType("text/html");     
    PrintWriter out = res.getWriter(); 
    String path=req.getParameter("destination"); 
    Part filePart=req.getPart("file"); 
    String filename=filePart.getSubmittedFileName().toString();    
    out.print("<br><br><hr> file name: "+filename); 
    OutputStream os=null;     InputStream is=null;  
try    { 
        os=new FileOutputStream(new File(path+File.separator+filename));             is=filePart.getInputStream(); 
        int read=0; 
        
        while ((read = is.read()) != -1) { 
            os.write(read); 
        } 
        out.println("<br>file uploaded sucessfully...!!!"); 
    } 
    catch(FileNotFoundException e){out.print(e);  
   }
 } 

~ index.html ~ 

<html> 
    <head> 
        <title>File Download Page</title> 
    </head> 
    <body> 
        <h1>File Download Application</h1> 
        Click <a href="DownloadServlet?filename=SampleChapter.pdf">Sample Chapter</a>         <br/><br/> 
        Click <a href="DownloadServlet?filename=TOC.pdf">Table Of Contents</a>    
 </body> 
</html> 

~ DownloadServlet.java ~ 

package filedownloadapp; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.PrintWriter; 
import javax.servlet.ServletContext; 
import javax.servlet.ServletException; 
import javax.servlet.ServletOutputStream; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; 
 
 
public class DownloadServlet extends HttpServlet {  public void doGet(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException { 
                 response.setContentType("APPLICATION/OCTET-STREAM");  
                 String filename = request.getParameter("filename");  
                 ServletContext context = getServletContext(); 
  InputStream is = context.getResourceAsStream("/" + filename); 
                 //ServletOutputStream out = response.getOutputStream(); // any of the two works                  PrintWriter out=response.getWriter();                  response.setHeader("Content-Disposition","attachment; filename=\"" + filename + "\""); // if comment this statement then it wl ask you about the editor with which you want to open the file  
    
                  int i; 
    while ((i=is.read()) != -1) {      
                    out.write(i);  
    }  
        is.close();      
                      out.close();  
 
 

Post a Comment

0 Comments