hi want to download a file from a directory where the file location is stored in a database,i have done the coding for retrieving the path from database to a html table,but i dont know hoe to download the file using its path stored in database.here is my code for retrieving the path from database.
<%@ page import="java.io.*,java.sql.*"%> <% String id=request.getParameter("id"); Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ksa","root","root"); Statement st=con.createStatement(); ResultSet rs=st.executeQuery("Select id,file_path,file_date from file12 where id like '"+id+"%'"); %> <table cellpadding="15" border="1"> <% while(rs.next()){ %> <tr> <td><%=rs.getString("id")%> </td> <td><%=rs.getString("file_path")%></td> <td><%=rs.getString("file_date")%> </td> </tr> <%}%> </table>
JSP Upload and download file
The given code allow the user to choose the file and upload it and save the file path into database. Then retrieve the path of the file from database and display it on the browser. On clicking the particular link of file path, you will be able to download that file.
1)form.jsp:
<%@ page language="java" %> <HTML> <FORM ENCTYPE="multipart/form-data" ACTION="uploadFile.jsp" METHOD=POST> <center> <table border="0" bgcolor=#ccFDDEE> <tr> <center><td colspan="2" align="center"><B>UPLOAD THE FILE</B><center></td> </tr> <tr><td colspan="2" align="center"> </td></tr> <tr><td><b>Choose the file To Upload:</b></td> <td><INPUT NAME="file" TYPE="file"></td> </tr> <tr><td colspan="2" align="center"> </td></tr> <tr><td colspan="2" align="center"><input type="submit" value="Send File"> </td></tr> <table> </center> </FORM> </HTML>
2)uploadFile.jsp:
<%@ page import="java.io.*,java.sql.*,java.util.zip.*" %> <% String saveFile=""; String contentType = request.getContentType(); if((contentType != null)&&(contentType.indexOf("multipart/form-data") >= 0)){ DataInputStream in = new DataInputStream(request.getInputStream()); int formDataLength = request.getContentLength(); byte dataBytes[] = new byte[formDataLength]; int byteRead = 0; int totalBytesRead = 0; while(totalBytesRead < formDataLength){ byteRead = in.read(dataBytes, totalBytesRead,formDataLength); totalBytesRead += byteRead; } String file = new String(dataBytes); saveFile = file.substring(file.indexOf("filename=\"") + 10); saveFile = saveFile.substring(0, saveFile.indexOf("\n")); saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\"")); int lastIndex = contentType.lastIndexOf("="); String boundary = contentType.substring(lastIndex + 1,contentType.length()); int pos; pos = file.indexOf("filename=\""); pos = file.indexOf("\n", pos) + 1; pos = file.indexOf("\n", pos) + 1; pos = file.indexOf("\n", pos) + 1; int boundaryLocation = file.indexOf(boundary, pos) - 4; int startPos = ((file.substring(0, pos)).getBytes()).length; int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length; File ff = new File("C:/UploadedFiles/"+saveFile); FileOutputStream fileOut = new FileOutputStream(ff); fileOut.write(dataBytes, startPos, (endPos - startPos)); fileOut.flush(); fileOut.close(); %><br><table border="2"><tr><td><b>You have successfully upload the file:</b> <%out.println(saveFile);%></td></tr></table> <% Connection connection = null; String connectionURL = "jdbc:mysql://localhost:3306/roseindia"; PreparedStatement psmnt = null; try{ Class.forName("com.mysql.jdbc.Driver").newInstance(); connection = DriverManager.getConnection(connectionURL, "root", "root"); psmnt = connection.prepareStatement("insert into file(file_path) values(?)"); psmnt.setString(1, ff.getPath()); int s = psmnt.executeUpdate(); if(s>0){ System.out.println("Uploaded successfully !"); } else{ System.out.println("Error!"); } Statement st=connection.createStatement(); ResultSet rs=st.executeQuery("Select file_path from file"); %> <table cellpadding="15" border="1"> <% while(rs.next()){ %> <tr> <td><a href="download.jsp?f=<%=path%>"><%=rs.getString("file_path")%></a></td> </tr> <%}%> </table> <% } catch(Exception e){ e.printStackTrace(); } } %>
continue..
3)download.jsp:
<%@ page import="java.util.*,java.io.*"%> <%@ page import="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(); String type=getMimeType("file:"+file); response.setContentType (type); response.setHeader ("Content-Disposition", "attachment; filename=\""+filename+"\""); String name = f.getName().substring(f.getName().lastIndexOf("/") + 1,f.getName().length()); InputStream in = new FileInputStream(f); ServletOutputStream outs = response.getOutputStream(); int bit = 256; int i = 0; try { while ((bit) >= 0) { bit = in.read(); outs.write(bit); } } catch (IOException ioe) { ioe.printStackTrace(System.out); } outs.flush(); outs.close(); in.close(); %>