Skip to content
Snippets Groups Projects
Commit 2c487c53 authored by andres's avatar andres
Browse files

Descargar zip

parent 5efa021b
No related branches found
No related tags found
No related merge requests found
......@@ -50,6 +50,27 @@ public class documentoDB {
return 0;
}
}
public static int insertDocu(int idNook, Part documento, String nombre) throws IOException {
ConnectionPool pool = ConnectionPool.getInstance();
Connection connection = pool.getConnection();
PreparedStatement ps=null;
String consulta="UPDATE Documento SET documento=? WHERE nook= ? AND nombre = ?";
try {
ps = connection.prepareStatement(consulta);
ps.setBinaryStream(1, documento.getInputStream() );
ps.setInt(2, idNook);
ps.setString(3, nombre);
int res = ps.executeUpdate();
ps.close();
pool.freeConnection(connection);
return res;
} catch (SQLException e) {
e.printStackTrace();
return 0;
}
}
public static ArrayList<Documento> getDocumentosNook(int idNook) {
ConnectionPool pool = ConnectionPool.getInstance();
......@@ -150,27 +171,4 @@ public class documentoDB {
}
}
public static int insertDocu(int idNook, Part documento) throws IOException {
ConnectionPool pool = ConnectionPool.getInstance();
Connection connection = pool.getConnection();
PreparedStatement ps=null;
String consulta="UPDATE Documento SET documento=? WHERE nook= ?";
try {
ps = connection.prepareStatement(consulta);
ps.setBlob(1, documento.getInputStream() );
ps.setInt(2, idNook);
int res = ps.executeUpdate();
ps.close();
pool.freeConnection(connection);
return res;
} catch (SQLException e) {
e.printStackTrace();
return 0;
}
}
}
......@@ -48,7 +48,7 @@ public class agregarArchivoSV extends HttpServlet {
HttpSession sesion = request.getSession();
String userName = (String) sesion.getAttribute("usuario");
System.out.println("\n\n\n" + documento.getSize() + "\n\n\n");
java.util.Date date = new java.util.Date();
Date fecha = new Date(date.getTime());
......@@ -67,7 +67,7 @@ public class agregarArchivoSV extends HttpServlet {
documentoDB.insert(doc);
documentoDB.insertDocu(idNook,documento);
documentoDB.insertDocu(idNook,documento, doc.getNombre());
String url = "/agregarArchivo.jsp";
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
......
......@@ -6,14 +6,18 @@
package servlet;
import conexionDB.documentoDB;
import conexionDB.nookDB;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
......@@ -21,6 +25,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import modelo.Documento;
import modelo.Nook;
/**
*
......@@ -46,12 +51,39 @@ public class descargarNookSV extends HttpServlet {
String usuario = (String) session.getAttribute("usuario");
int nook = Integer.parseInt(request.getParameter("idNook"));
ArrayList<Documento> documentos = documentoDB.getFiles(nook);
String nombreArchivo = documentos.get(0).getNombre();
// String rutaAplicacion = getServletContext().getRealPath("");
// String rutaDescarga = rutaAplicacion + File.separator + "temp";
String ruta = "temp"+ File.separator + nook + File.separator + nombreArchivo;
ArrayList<String> archivos = new ArrayList();
Nook nookO = nookDB.getNook(nook);
String zipName = nookO.getNombre() + ".zip";
String rutaZip = "temp" + File.separator + nook + File.separator + zipName;
FileOutputStream fos = new FileOutputStream(rutaZip);
ZipOutputStream zos = new ZipOutputStream(fos);
byte[] bufferZ = new byte[1024];
File file = new File(ruta);
for(int i=0; i<documentos.size(); i++){
archivos.add("temp" + File.separator + nook + File.separator + documentos.get(i).getNombre());
System.out.println(archivos.get(i));
}
for (int i=0; i < archivos.size(); i++) {
File srcFile = new File(archivos.get(i));
FileInputStream fis = new FileInputStream(srcFile);
// begin writing a new ZIP entry, positions the stream to the start of the entry data
zos.putNextEntry(new ZipEntry(srcFile.getName()));
int length;
while ((length = fis.read(bufferZ)) > 0) {
zos.write(bufferZ, 0, length);
}
zos.closeEntry();
// close the InputStream
fis.close();
}
zos.close();
File file = new File(rutaZip);
OutputStream outStream = null;
FileInputStream inputStream = null;
System.out.println(file.getName());
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment