Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. <%--
  2. Document : downloadtest
  3. Created on : 13.des.2017, 15:33:43
  4. Author : mats, simen & nils
  5. --%>
  6. <%@page import="java.io.OutputStream"%>
  7. <%@page import="java.sql.Blob"%>
  8. <%@page import="java.sql.ResultSet"%>
  9. <%@page import="java.sql.Statement"%>
  10. <%@page import="java.sql.Connection"%>
  11. <%@page import="DB.DataBase"%>
  12. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  13.  
  14. <!DOCTYPE html>
  15. <%
  16.  
  17. DataBase db = new DataBase();
  18. Connection con = null;
  19. Statement st = null;
  20. ResultSet rs = null;
  21.  
  22. con = db.getCon();
  23. st = con.createStatement();
  24.  
  25. String docid = request.getParameter("docid");
  26. String query = "select filename, typefile, file from Documents where docid = " + docid;
  27. rs = st.executeQuery(query);
  28.  
  29.  
  30.  
  31.  
  32.  
  33. rs.next();
  34.  
  35. // clear the response header information.
  36. response.reset();
  37. // check the file type and set the header contentType accordingly..
  38. if(rs.getString(2)==".txt")
  39. {
  40. response.setContentType("application/octet-stream");
  41. }
  42. else if(rs.getString(2)==".pdf")
  43. {
  44. response.setContentType("application/pdf");
  45. }
  46. else if((rs.getString(2)==".doc")||rs.getString(2)==".docx")
  47. {
  48. response.setContentType("application/msword");
  49. }
  50. else if((rs.getString(2)==".xls")||(rs.getString(2)==".xlsx"))
  51. {
  52. response.setContentType("application/vnd.ms-excel");
  53. }
  54. // add header information to response object
  55. response.addHeader("Content-Disposition","attachment; filename="+rs.getString(1));
  56. // create the byte array from Blob
  57. Blob blb = rs.getBlob(3);
  58. byte[] bdata = blb.getBytes(1, (int) blb.length());
  59.  
  60. // get the response Output stream object to write the content of the file into header
  61. OutputStream output = response.getOutputStream();
  62. output.write(bdata);
  63. output.close();
  64. // close the obejct of ResultSet
  65. rs.close();
  66.  
  67. // close the connection object..
  68. con.close();
  69. %>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement