Guest User

Untitled

a guest
Feb 21st, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. package Pruebas;
  2.  
  3. import com.mysql.jdbc.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import javax.faces.application.FacesMessage;
  7. import javax.faces.bean.ManagedBean;
  8. import javax.faces.context.FacesContext;
  9.  
  10. import org.primefaces.event.FileUploadEvent;
  11. import org.primefaces.model.UploadedFile;
  12.  
  13. @ManagedBean
  14. public class SubirImagen {
  15.  
  16. private UploadedFile file;
  17.  
  18. public UploadedFile getFile() {
  19. return file;
  20. }
  21.  
  22. public void setFile(UploadedFile file) {
  23. this.file = file;
  24. }
  25.  
  26. public void upload() {
  27. try{
  28. if (file != null) {
  29. Class.forName("com.mysql.jdbc.Driver");
  30. Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/pruebajpa", "root", "admin");
  31.  
  32. PreparedStatement ps = con.prepareStatement("INSERT INTO pruebajpa.img (img) VALUES ('?');");
  33. ps.setBinaryStream(1, file.getInputstream());
  34. ps.executeUpdate();
  35. con.close();
  36. FacesMessage message = new FacesMessage("Succesful", file.getFileName() + " is uploaded.");
  37. FacesContext.getCurrentInstance().addMessage(null, message);
  38. }
  39. }catch(Exception e){
  40. System.out.println("Error " + e);
  41. }
  42. }
  43. }
  44.  
  45. <?xml version="1.0" encoding="UTF-8"?>
  46. <!--
  47. To change this license header, choose License Headers in Project Properties.
  48. To change this template file, choose Tools | Templates
  49. and open the template in the editor.
  50. -->
  51. <!DOCTYPE html>
  52. <html xmlns="http://www.w3.org/1999/xhtml"
  53. xmlns:h="http://java.sun.com/jsf/html"
  54. xmlns:f="http://java.sun.com/jsf/core"
  55. xmlns:ui="http://java.sun.com/jsf/facelets"
  56. xmlns:p="http://primefaces.org/ui">
  57.  
  58.  
  59. <h:head>
  60. <title>TODO supply a title</title>
  61. <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  62. </h:head>
  63.  
  64.  
  65. <h:body>
  66. <h:form>
  67.  
  68. <p:galleria value="#{galeria.images}" var="image" panelWidth="500" panelHeight="313" showCaption="true">
  69. <p:graphicImage name="imagenes/#{image}" alt="Image Description for #{image}" title="#{image}"/>
  70.  
  71. </p:galleria>
  72.  
  73.  
  74. </h:form>
  75.  
  76. <h:form enctype="multipart/form-data">
  77. <p:growl id="messages" showDetail="true" />
  78.  
  79. <p:fileUpload value="#{subirImagen.file}" mode="simple" skinSimple="true"/>
  80.  
  81. <p:commandButton value="Submit2" ajax="false" actionListener="#{subirImagen.upload()}" disabled="false" />
  82. </h:form>
  83.  
  84. <h:form enctype="multipart/form-data">
  85.  
  86. <p:growl showDetail="true" />
  87. <p:fileUpload value="#{uploadBean.file}" mode="simple" />
  88. <p:commandButton ajax="false" value="Subir" actionListener="#{uploadBean.upload()}" />
  89.  
  90. </h:form>
  91.  
  92.  
  93. </h:body>
  94. </html>
Add Comment
Please, Sign In to add comment