Advertisement
Guest User

Untitled

a guest
Feb 3rd, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.48 KB | None | 0 0
  1. enter code here
  2.  
  3.  
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.OutputStream;
  7. import java.sql.Blob;
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.PreparedStatement;
  11. import java.sql.ResultSet;
  12. import java.sql.SQLException;
  13.  
  14. import javax.servlet.ServletContext;
  15. import javax.servlet.ServletException;
  16. import javax.servlet.annotation.WebServlet;
  17. import javax.servlet.http.HttpServlet;
  18. import javax.servlet.http.HttpServletRequest;
  19. import javax.servlet.http.HttpServletResponse;
  20.  
  21.  
  22. @WebServlet("/downloadFileServlet")
  23. public class DBFileDownloadServlet extends HttpServlet {
  24.  
  25. // size of byte buffer to send file
  26. private static final int BUFFER_SIZE = 4096;
  27.  
  28. // database connection settings
  29. private String dbURL = "jdbc:mysql://localhost:3306/test";
  30. private String dbUser = "root";
  31. private String dbPass = "secret";
  32.  
  33. protected void doGet(HttpServletRequest request,
  34. HttpServletResponse response) throws ServletException, IOException {
  35. // get upload id from URL's parameters
  36. int uploadId = Integer.parseInt(request.getParameter("id"));
  37.  
  38. Connection conn = null; // connection to the database
  39.  
  40. try {
  41. // connects to the database
  42. DriverManager.registerDriver(new com.mysql.jdbc.Driver());
  43. conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","ROOT");
  44.  
  45. // queries the database
  46. String sql = "SELECT * FROM files_upload WHERE upload_id = ?";
  47. PreparedStatement statement = conn.prepareStatement(sql);
  48. statement.setInt(1, uploadId);
  49.  
  50. ResultSet result = statement.executeQuery();
  51. if (result.next()) {
  52. // gets file name and file blob data
  53. String fileName = result.getString("file_name");
  54. Blob blob = result.getBlob("file_data");
  55. InputStream inputStream = blob.getBinaryStream();
  56. int fileLength = inputStream.available();
  57.  
  58. System.out.println("fileLength = " + fileLength);
  59.  
  60. ServletContext context = getServletContext();
  61.  
  62. // sets MIME type for the file download
  63. String mimeType = context.getMimeType(fileName);
  64. if (mimeType == null) {
  65. mimeType = "application/octet-stream";
  66. }
  67.  
  68. // set content properties and header attributes for the response
  69. response.setContentType(mimeType);
  70. response.setContentLength(fileLength);
  71. String headerKey = "Content-Disposition";
  72. String headerValue = String.format("attachment; filename="%s"", fileName);
  73. response.setHeader(headerKey, headerValue);
  74.  
  75. // writes the file to the client
  76. OutputStream outStream = response.getOutputStream();
  77.  
  78. byte[] buffer = new byte[BUFFER_SIZE];
  79. int bytesRead = -1;
  80.  
  81. while ((bytesRead = inputStream.read(buffer)) != -1) {
  82. outStream.write(buffer, 0, bytesRead);
  83. }
  84.  
  85. inputStream.close();
  86. outStream.close();
  87. } else {
  88. // no file found
  89. response.getWriter().print("File not found for the id: " + uploadId);
  90. }
  91. } catch (SQLException ex) {
  92. ex.printStackTrace();
  93. response.getWriter().print("SQL Error: " + ex.getMessage());
  94. } catch (IOException ex) {
  95. ex.printStackTrace();
  96. response.getWriter().print("IO Error: " + ex.getMessage());
  97. } finally {
  98. if (conn != null) {
  99. // closes the database connection
  100. try {
  101. conn.close();
  102. } catch (SQLException ex) {
  103. ex.printStackTrace();
  104. }
  105. }
  106. }
  107. }
  108.  
  109. SEVERE: Servlet.service() for servlet [Download] in context with path [/Download111] threw exception
  110. java.lang.NumberFormatException: null
  111. at java.lang.Integer.parseInt(Unknown Source)
  112. at java.lang.Integer.parseInt(Unknown Source)
  113. at Download.doGet(Download.java:42)
  114. at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
  115. at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
  116. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
  117. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
  118. at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
  119. at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
  120. at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
  121. at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
  122. at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
  123. at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
  124. at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
  125. at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
  126. at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
  127. at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
  128. at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
  129. at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
  130. at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
  131. at java.lang.Thread.run(Unknown Source)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement