Saturday, May 9, 2015

How to download a file from server in java

To download a file from server is very easy task just get the file name and path and set the file name into response header (Content-Disposition) .
response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");



In below example I have a folder (uploadedFiles) with some pdf files (books). I have displayed these file first in one of the jsp and given download link for each once you click on download link you can download the file.

Step 1- Create folder with some files

uploadedFiles
|
|__ ASP_NET_Web_Developers_Guide.pdf
|
|__ Beginners_Guide_to_SQL_Server_2008



Step 2- Create downloafileList.jsp for displaying all the books list.

downloafileList.jsp

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<%@page import="java.util.StringTokenizer"%>
<%@page import="java.io.File"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Books List</h1>
        <table width="100%" bgcolor="#C5EBB4" border="0">

            <th width="74%" height="36" bgcolor="#F2606F"><div align="center" class="bookTitle">Books Name</div></th>
        <th width="26%" bgcolor="#F2606F"><span class="bookTitle">Downloads</span></th>
            <%
                File f = new File("h:\\uploadedFiles");
                int rowcolor = 1;
                File[] files = f.listFiles();
                for (int i = 0; i < files.length; i++) {
                    String name1 = files[i].getName();
                    String path = files[i].getPath();
                    StringTokenizer st = new StringTokenizer(name1, ".");
                    int count = 1;
                    String name = null;
                    while (st.hasMoreTokens()) {
                        name = st.nextToken();
                        count++;
                        if (count > 1) {
                            break;
                        }
                    }
                    if ((rowcolor % 2 == 0)) {
            %>
        <tr bgcolor="#F3FCF3">
            <% } else {

            %>
        <tr bgcolor="#EBFBE3"> 
            <% }%>
            <td valign="middle">
                <div align="center">
                    </span>
                    <div align="left" class="interviewQuestionFont"><img src="image/book.jpg" class="bookImg" alt="Books" title="Books"> <%=name%></div>
                </div></td>
            <td><div align="center"><a href="download.jsp?f=<%=path%>" class="ast">Download </a></div></td>
        </tr>
        <%
                rowcolor++;
            }

        %>
    </table>
</body>
</html>

Step 3- Create download.jsp for downloading the files.

download.jsp

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<%@page import="java.io.*,java.net.*"%>
<%!
    public static String getMimeType(String fileUrl)
            throws java.io.IOException, MalformedURLException {
        String type = null;
        URL u = new URL(fileUrl);
        URLConnection uc = null;
        uc = u.openConnection();
        type = uc.getContentType();
        return type;
    }
%>
<%
    String file = request.getParameter("f");
    File f = new File(file);
    String filename = f.getName();
%>
<%
    response.setContentType("text/html");

    String filepath = "h:\\uploadedFiles\\";
    response.setContentType("APPLICATION/OCTET-STREAM");
    response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
    java.io.FileInputStream fileInputStream = new java.io.FileInputStream(filepath + filename);
    int i;
    while ((i = fileInputStream.read()) != -1) {
        out.write(i);
    }
    filepath = null;
    fileInputStream=null;
    file=null;
    filename=null;
    f=null;
%>

No comments :

Post a Comment