Netbeans Jsp Projects Download

Posted on by

Servlet Upload File and Download File Example. Servlet Upload File and Download File is a common task in java web application. Since I have written a lot about java servlet recently, I thought to provide a sample example of servlet file upload to server and then download from server to client. Servlet Upload File. JSP vs. JSF 2 Servlets and JSP JavaServer Pages Original, widelydeployed standard Used by google. I am trying to use Notepad as my allinone tool edit, run, compile, etc. I have JRE installed, and I have setup my path variable to the. When I. Netbeans Jsp Projects DownloadOur use case is to provide a simple HTML page where client can select a local file to be uploaded to server. On submission of request to upload the file, our servlet program will upload the file into a directory in the server and then provide the URL through which user can download the file. For security reason, user will not be provided direct URL for downloading the file, rather they will be given a link to download the file and our servlet will process the request and send the file to user. We will create a dynamic web project in Eclipse and the project structure will look like below image. Lets look into all the components of our web application and understand the implementation. HTML Page for Java Uploading File to Server. AlrnM2iY1E/0.jpg' alt='Netbeans Jsp Projects Download' title='Netbeans Jsp Projects Download' />We can upload a file to server by sending a post request to servlet and submitting the form. We cant use GET method for uploading file. Another point to note is that enctype of form should be multipartform data. To select a file from user file system, we need to use input element with type as file. Creating/Netbeans/images/Netbeans01.png' alt='Netbeans Jsp Projects Download' title='Netbeans Jsp Projects Download' />So we can have a simple HTML page index. Upload. Download. File. Servlet methodpost enctypemultipartform data. Select File to Upload lt input typefile namefile. Name. lt input typesubmit valueUpload. Server File Location for File Upload. We need to store file into some directory at server, we can have this directory hardcoded in program but for better flexibility, we will keep it configurable in deployment descriptor context params. Also we will add our upload file html page to the welcome file list. Our web. xml file will look like below. UTF 8. lt web app xmlns xsihttp www. XMLSchema instance xmlnshttp java. Locationhttp java. Servlet. File. Upload. Download. Examplelt display name. Servlet. Context. Listener for File Upload Location. Since we need to read context parameter for file location and create a File object from it, we can write a Servlet. Context. Listener to do it when context is initialized. We can set absolute directory location and File object as context attribute to be used by other servlets. Our Servlet. Context. Listener implementation code is like below. File. import javax. Servlet. Context. Servlet. Context. Event. import javax. Servlet. Context. Listener. import javax. Web. Listener. public class File. Location. Context. Listener implements Servlet. Context. Listener. InitializedServlet. Context. Event servlet. Context. Event. String root. Path System. Propertycatalina. Servlet. Context ctx servlet. Context. Event. get. Servlet. Context. String relative. Path ctx. Init. Parametertempfile. File file new Fileroot. Path File. separator relative. Path. if file. System. File Directory created to be used for storing files. AttributeFILESDIRFILE, file. AttributeFILESDIR, root. Path File. separator relative. Path. public void context. DestroyedServlet. Context. Event servlet. Context. Event. File Upload Download Servlet. Update Servlet Specs 3 added support to upload files on server in the API, so we wont need to use any third party API. Please check out Servlet 3 Upload File. For File upload, we will use Apache Commons File. Upload utility, for our project we are using version 1. File. Upload depends on Apache Commons IO jar, so we need to place both in the lib directory of the project, as you can see that in above image for project structure. We will use Disk. File. Item. Factory factory that provides a method to parse the Http. Servlet. Request object and return list of File. Item. File. Item provides useful method to get the file name, field name in form, size and content type details of the file that needs to be uploaded. To write file to a directory, all we need to do it create a File object and pass it as argument to File. Item write method. Since the whole purpose of the servlet is to upload file, we will override init method to initialise the Disk. File. Item. Factory object instance of the servlet. We will use this object in the do. Post method implementation to upload file to server directory. Once the file gets uploaded successfully, we will send response to client with URL to download the file, since HTML links use GET method,we will append the parameter for file name in the URL and we can utilise the same servlet do. Get method to implement file download process. For implementing download file servlet, first we will open the Input. Stream for the file and use Servlet. Context. get. Mime. Type method to get the MIME type of the file and set it as response content type. We will also need to set the response content length as length of the file. To make sure that client understand that we are sending file in response, we need to set Content Disposition header with value as attachment filenamefile. Name. Once we are done with setting response configuration, we can read file content from Input. Stream and write it to Servlet. Output. Stream and the flush the output to client. Our final implementation of Upload. Download. File. Servlet servlet looks like below. Church Administration Tools more. File. import java. File. Input. Stream. IOException. import java. Input. Stream. import java. Print. Writer. import java. Iterator. import java. List. import javax. Servlet. Context. Servlet. Exception. Servlet. Output. Stream. Web. Servlet. import javax. Http. Servlet. import javax. Http. Servlet. Request. Http. Servlet. Response. File. Item. import org. File. Upload. Exception. Disk. File. Item. Factory. import org. Servlet. File. Upload. Web. ServletUpload. Download. File. Servlet. Upload. Download. File. Servlet extends Http. Servlet. private static final long serial. Version. UID 1. L. Servlet. File. Upload uploader null. Servlet. Exception. Disk. File. Item. Factory file. Factory new Disk. File. Item. Factory. File files. Dir File get. Servlet. Context. AttributeFILESDIRFILE. Factory. set. Repositoryfiles. Dir. this. uploader new Servlet. File. Uploadfile. Factory. protected void do. GetHttp. Servlet. Request request, Http. Servlet. Response response throws Servlet. Exception, IOException. String file. Name request. Parameterfile. Name. Name null file. Name. Servlet. ExceptionFile Name cant be null or empty. File file new Filerequest. Servlet. Context. AttributeFILESDIRFile. Name. if file. Servlet. ExceptionFile doesnt exists on server. System. out. printlnFile location on server file. Absolute. Path. Servlet. Context ctx get. Servlet. Context. Input. Stream fis new File. Input. Streamfile. String mime. Type ctx. Mime. Typefile. get. Absolute. Path. Content. Typemime. Type null mime. Type applicationoctet stream. Content. Lengthint file. HeaderContent Disposition, attachment filename file. Name. Servlet. Output. Stream os response. Output. Stream. Data new byte1. Data 1. os. Data, 0, read. System. File downloaded at client successfully. PostHttp. Servlet. Request request, Http. Servlet. Response response throws Servlet. Exception, IOException. Servlet. File. Upload. Multipart. Contentrequest. Servlet. ExceptionContent type is not multipartform data. Content. Typetexthtml.