Skip to content
Snippets Groups Projects
Commit 5efa021b authored by andres's avatar andres
Browse files

Descargar documentossssssssssssssssssssssssssssssssss

parent 0b5e0502
Branches
No related tags found
No related merge requests found
......@@ -12,6 +12,7 @@ import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
......@@ -99,15 +100,22 @@ public class documentoDB {
documento.setResumen(rs.getString("resumen"));
documento.setFechaCreacion(rs.getDate("fechaCreacion"));
documento.setFechaModificacion(rs.getDate("fechaModificacion"));
documento.setDocumento(rs.getBlob("documento"));
documentos.add(documento);
InputStream is = rs.getBinaryStream("documento");
String str = convert(is);
new File("temp").mkdir();
File file = new File("temp/"+documento.getNombre());
FileOutputStream outputStream = new FileOutputStream(file);
outputStream.write(str.getBytes());
outputStream.close();
InputStream is = documento.getDocumento().getBinaryStream();
String ruta = "temp" + File.separator + idNook;
new File(ruta).mkdirs();
FileOutputStream os = new FileOutputStream(ruta + File.separator + documento.getNombre());
byte[] buffer = new byte[1024];
int bytesRead;
while((bytesRead = is.read(buffer)) !=-1){
os.write(buffer, 0, bytesRead);
}
is.close();
os.flush();
os.close();
}
rs.close();
ps.close();
......@@ -119,17 +127,17 @@ public class documentoDB {
}
}
public static int insertDocu(int idNook, Part documento) throws IOException {
public static double borrarArchivo(String nombre, int idNook){
ConnectionPool pool = ConnectionPool.getInstance();
Connection connection = pool.getConnection();
PreparedStatement ps=null;
String consulta="UPDATE Documento SET documento=? WHERE nook= ?";
String consulta="DELETE FROM Documento WHERE nombre = ? AND nook = ?";
try {
ps =connection.prepareStatement(consulta);
ps.setBlob(1, documento.getInputStream() );
ps.setString(1, nombre );
ps.setInt(2, idNook );
int res = ps.executeUpdate();
......@@ -140,38 +148,20 @@ public class documentoDB {
e.printStackTrace();
return 0;
}
}
private static String convert(InputStream is) {
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayOutputStream buf = new ByteArrayOutputStream();
int result;
String str = null;
try {
result = bis.read();
while (result != -1) {
buf.write((byte) result);
result = bis.read();
}
str = buf.toString("UTF-8");
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
public static double borrarArchivo(String nombre, int idNook){
public static int insertDocu(int idNook, Part documento) throws IOException {
ConnectionPool pool = ConnectionPool.getInstance();
Connection connection = pool.getConnection();
PreparedStatement ps=null;
String consulta="DELETE FROM Documento WHERE nombre = ? AND nook = ?";
String consulta="UPDATE Documento SET documento=? WHERE nook= ?";
try {
ps = connection.prepareStatement(consulta);
ps.setString(1, nombre );
ps.setBlob(1, documento.getInputStream() );
ps.setInt(2, idNook);
int res = ps.executeUpdate();
......@@ -182,6 +172,5 @@ public class documentoDB {
e.printStackTrace();
return 0;
}
}
}
......@@ -6,12 +6,10 @@
package servlet;
import conexionDB.documentoDB;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
......@@ -48,6 +46,43 @@ 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;
File file = new File(ruta);
OutputStream outStream = null;
FileInputStream inputStream = null;
System.out.println(file.getName());
if(file.exists()){
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition","attachment; filename=\"" + file.getName() + "\"");
try{
outStream = response.getOutputStream();
inputStream = new FileInputStream(file);
byte[] buffer = new byte[1024*100];
int bytesRead = -1;
while((bytesRead= inputStream.read(buffer))!=-1){
outStream.write(buffer, 0, bytesRead);
}
} catch(IOException e){
e.printStackTrace();
} finally{
if(inputStream != null) {
inputStream.close();
}
outStream.flush();
if(outStream != null){
outStream.close();
}
}
} else{
response.setContentType("text/html");
response.getWriter().println("<h3>File \"+ fileName +\" Is Not Present .....!</h3>");
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
......
......@@ -17,7 +17,7 @@ CREATE TABLE Usuario(
clave VARCHAR(15) NOT NULL,
correo VARCHAR(80) NOT NULL,
valoracionMedia NUMERIC(3,1),
imagenPerfil BLOB,
imagenPerfil BLOB(5M),
PRIMARY KEY (nombreUsuario)
);
......@@ -60,7 +60,7 @@ CREATE TABLE Documento(
resumen VARCHAR(400),
fechaCreacion DATE,
fechaModificacion DATE,
documento BLOB,
documento BLOB(16M),
PRIMARY KEY (nook, nombre),
CONSTRAINT FK_DOCUMENTO FOREIGN KEY (nook)
REFERENCES Nook (idNook)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment