package Servlet;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
 
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
/**
 * A servlet that retrieves a file from MySQL database and lets the client
 * downloads the file.
 * @author www.codejava.net
 */
@WebServlet("/downloadServlet")
public class FileDownloadDBServlet extends HttpServlet {
 
    // size of byte buffer to send file
    private static final int BUFFER_SIZE = 4096;   
     
    // database connection settings
    private String dbURL = "jdbc:mysql://localhost:3306/FileDW";
    private String dbUser = "root";
    private String dbPass = "Warstar123";
     
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // get upload id from URL's parameters
        int uploadid = Integer.parseInt(request.getParameter("upload_id"));
         
        Connection conn = null; // connection to the database
         
        try {
            // connects to the database
            DriverManager.registerDriver(new com.mysql.jdbc.Driver());
            conn = DriverManager.getConnection(dbURL, dbUser, dbPass);
 
            // queries the database
            String sql = "Select * from file_upload where upload_id  = ?";
            PreparedStatement statement = conn.prepareStatement(sql);
            statement.setInt(1, uploadid);
 
            ResultSet result = statement.executeQuery();
            if (result.next()) {
                // gets file name and file blob data
                String fileName = result.getString("file_name");
                Blob blob = result.getBlob("file_data");
                InputStream inputStream = blob.getBinaryStream();
                int fileLength = inputStream.available();
                 
                System.out.println("fileLength = " + fileLength);
 
                ServletContext context = getServletContext();
 
                // sets MIME type for the file download
                String mimeType = context.getMimeType(fileName);
                if (mimeType == null) {        
                    mimeType = "application/octet-stream";
                }              
                 
                // set content properties and header attributes for the response
                response.setContentType(mimeType);
                response.setContentLength(fileLength);
                String headerKey = "Content-Disposition";
                String headerValue = String.format("attachment; filename=\"%s\"", fileName);
                response.setHeader(headerKey, headerValue);
 
                // writes the file to the client
                OutputStream outStream = response.getOutputStream();
                 
                byte[] buffer = new byte[BUFFER_SIZE];
                int bytesRead = -1;
                 
                while ((bytesRead = inputStream.read(buffer)) != -1) {
                    outStream.write(buffer, 0, bytesRead);
                }
                 
                inputStream.close();
                outStream.close();             
            } else {
                // no file found
                response.getWriter().print("File not found for the id: " + uploadid);  
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
            response.getWriter().print("SQL Error: " + ex.getMessage());
        } catch (IOException ex) {
            ex.printStackTrace();
            response.getWriter().print("IO Error: " + ex.getMessage());
        } finally {
            if (conn != null) {
                // closes the database connection
                try {
                    conn.close();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }          
        }
    }
}

@@ -536,130 +536,55 @@
 %0A * 
-A s
+S
 ervlet 
-that retrieves a file from MySQL database and lets the client%0A * downloads the file.%0A * @author www.codejava.net
+som laster ned fra databasen til pcen. 
 %0A */
@@ -596,16 +596,31 @@
 Servlet(
+urlPatterns = %7B
 %22/downlo
@@ -629,16 +629,17 @@
 Servlet%22
+%7D
 )%0Apublic
@@ -702,40 +702,26 @@
  // 
-size of byte buffer to send
+Buffer for
  file
+n. 
 %0A   
@@ -782,25 +782,25 @@
 %0A    // 
-d
+D
 atabase 
 connecti
@@ -795,23 +795,11 @@
 ase 
-connection 
 set
-t
 ings
@@ -860,17 +860,17 @@
 06/FileD
-W
+B
 %22;%0A    p
@@ -1097,43 +1097,33 @@
  // 
-get
+Henter
  upload
- 
+_
 id fr
-om URL's parameters
+a url.
 %0A   
@@ -1123,24 +1123,26 @@
 rl.%0A        
+//
 int uploadid
@@ -1246,25 +1246,24 @@
  // 
-connection to the
+Forbindelsen til
  dat
@@ -1259,32 +1259,35 @@
 sen til database
+n. 
 %0A         %0A     
@@ -1314,23 +1314,23 @@
  // 
-connects to the
+Forbindelse til
  dat
@@ -1326,32 +1326,35 @@
 lse til database
+n. 
 %0A            Dri
@@ -1562,32 +1562,32 @@
 rom 
-file_upload where upload
+%60contacts%60 where contact
 _id 
@@ -1699,19 +1699,87 @@
 (1, 
-uploadid);%0A
+1); //TODO: Fix uploadId (line 37 in html, and parese this in here)%0A           
  %0A  
@@ -1886,29 +1886,156 @@
     
-// gets file name and
+System.out.println(%22sp%C3%B8rringggg%22 + statement);%0A                System.out.println(%22resultttt%22 + result);//testing%0A                // Henter filen og
  fil
@@ -2040,21 +2040,18 @@
 ile blob
+.
  
-data
 %0A       
@@ -2160,13 +2160,8 @@
 file
-_data
 %22);%0A
@@ -3652,21 +3652,25 @@
  // 
+Inge
 n
-o
  fil
-e
  f
-o
 un
-d
+net.
 %0A   
@@ -3743,18 +3743,8 @@
 : %22 
-+ uploadid
 );  
@@ -4117,18 +4117,31 @@
  // 
-closes the
+Lukker forbindelsen til
  dat
@@ -4149,19 +4149,36 @@
 base
- connection
+n n%C3%A5r arbeidet er fullf%C3%B8rt. 
 %0A   
@@ -4374,10 +4374,207 @@
 %7D%0A    %7D%0A
-%7D
+    @Override%0A    protected void doPost(HttpServletRequest request,%0A            HttpServletResponse response) throws ServletException, IOException %7B%0A        System.out.println(%22nullll%22);%0A    %7D%0A   %7D%0A
 %0A

Update FileDownloadDBServlet.java