Skip to content
Snippets Groups Projects
Commit ac510ba1 authored by Usuario's avatar Usuario
Browse files

subir documento

parent f166abea
Branches
No related tags found
No related merge requests found
......@@ -4,9 +4,11 @@
* and open the template in the editor.
*/
package conexionDB;
import java.io.IOException;
import java.sql.*;
import modelo.Nook;
import java.util.ArrayList;
import javax.servlet.http.Part;
public class nookDB {
......@@ -286,5 +288,29 @@ public class nookDB {
return -1.0;
}
}
public static int insertDocu(int idNook, Part documento) throws IOException {
ConnectionPool pool = ConnectionPool.getInstance();
Connection connection = pool.getConnection();
PreparedStatement ps=null;
String consulta="UPDATE Nook SET documento=? WHERE idNook= ?";
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;
}
}
}
......@@ -6,6 +6,7 @@
package modelo;
import java.sql.Blob;
import java.util.Date;
public class Nook {
......@@ -16,6 +17,7 @@ public class Nook {
private String autor;
private Date fechaCreacion;
private Date fechaModificacion;
private Blob documento;
private int descargas;
private double valoracionMedia;
public Nook(){
......@@ -97,6 +99,13 @@ public class Nook {
public void setValoracionMedia(double valoracionMedia) {
this.valoracionMedia = valoracionMedia;
}
public Blob getDocumento() {
return documento;
}
public void setDocumento(Blob documento) {
this.documento = documento;
}
}
......@@ -14,11 +14,13 @@ import java.text.SimpleDateFormat;
import java.sql.Date;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Part;
import modelo.ClasificacionCategorias;
import modelo.Nook;
......@@ -27,6 +29,7 @@ import modelo.Nook;
* @author Fnac
*/
@WebServlet(name = "crearNookSV", urlPatterns = {"/crearNookSV"})
@MultipartConfig
public class crearNookSV extends HttpServlet {
/**
......@@ -45,6 +48,10 @@ public class crearNookSV extends HttpServlet {
String etiquetas = request.getParameter("etiquetas");
String resumen = request.getParameter("resumen");
Part documento = request.getPart("documento");
java.util.Date date = new java.util.Date();
Date fecha = new Date(date.getTime());
......@@ -64,9 +71,14 @@ public class crearNookSV extends HttpServlet {
nookDB.insert(nook);
Nook nookCreado = nookDB.ultimoNook(userName);
int idNook = nookCreado.getIdNook();
ClasificacionCategorias categoria = new ClasificacionCategorias();
categoria.setIdNook(idNook);
......@@ -75,6 +87,9 @@ public class crearNookSV extends HttpServlet {
clasificacionCategoriasDB.insert(categoria);
nookDB.insertDocu(idNook,documento);
String url = "/misNooksSV";
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
dispatcher.forward(request, response);
......
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author Usuario
*/
@WebServlet(name = "descargarNookSV", urlPatterns = {"/descargarNookSV"})
public class descargarNookSV extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet descargarNookSV</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet descargarNookSV at " + request.getContextPath() + "</h1>");
out.println("</body>");
out.println("</html>");
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
......@@ -35,7 +35,7 @@ and open the template in the editor.
<h3 style="margin-top: 3rem;">CREAR NOOK</h3>
<div class="pag-crearNook">
<form class="tablamensajetexto" method="post" action="/crearNookSV">
<form class="tablamensajetexto" method="post" enctype="multipart/form-data" action="/crearNookSV" >
<div class="partes">
<label for="nombre" class="nombreNook" style="padding-right: 2.5em;">Nombre:</label>
......@@ -56,7 +56,7 @@ and open the template in the editor.
<div class="partes" >
<label for="subirarchivo" class="SubirArchivo" style="padding-right: 2em;">Subir Archivo:</label>
</div>
<input type="file" multiple="multiple"/>
<input type="file" name="documento" required> <!--multiple="multiple"-->
<div class="partes" >
......
......@@ -48,6 +48,7 @@ CREATE TABLE Nook(
fechaModificacion DATE,
descargas INTEGER,
valoracionMedia NUMERIC(3,1),
documento BLOB,
PRIMARY KEY (idNook),
CONSTRAINT FK_NOOK FOREIGN KEY (autor)
REFERENCES Usuario (nombreUsuario)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment