Guest User

Untitled

a guest
Feb 22nd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. public static void main(String[] args) {
  2. File currentDir = new File("C:\Users\luccaskammer\Desktop\Imagens\"); // Define o diretorio a ser lido
  3. displayDirectoryContents(currentDir);
  4.  
  5. }
  6.  
  7. public static void displayDirectoryContents(File dir) {
  8. try {
  9.  
  10. ImagensParaDBController contr = new ImagensParaDBController();
  11. File[] files = dir.listFiles();
  12. DeletePng del = new DeletePng();
  13. //ConvertData conv = new ConvertData();
  14.  
  15. for (File file : files) {
  16.  
  17. ImagensParaBD img = new ImagensParaBD();
  18. ImagensParaBD name = new ImagensParaBD();
  19. ImagensParaBD picture = new ImagensParaBD();
  20.  
  21. if (file.isDirectory())
  22.  
  23. {
  24.  
  25. System.out.println("directory:" + file.getCanonicalPath());
  26. displayDirectoryContents(file);
  27.  
  28. }
  29.  
  30. else if(file.isFile()) {
  31.  
  32. System.out.println(" file:" + file.getCanonicalPath());
  33. img.setName(del.deletePng(file.getName()));
  34. java.nio.file.Path filelocation = Paths.get(file.getCanonicalPath());
  35. img.setPicture(Files.readAllBytes(filelocation));
  36. contr.insertImageDB(img, picture, name);
  37. }
  38.  
  39. else
  40. {
  41.  
  42. System.out.println("invalid");
  43.  
  44. }
  45. }
  46.  
  47. contr.disconnect();
  48.  
  49. }
  50. catch (IOException e) {
  51. }
  52. }
  53.  
  54. }
  55.  
  56. @Entity
  57. @Table(name = "planegeo")
  58.  
  59. public class ImagensParaBD implements Serializable {
  60. @Id
  61. @GeneratedValue(strategy = GenerationType.IDENTITY)
  62. private int id;
  63. //private String pathFolder;
  64. private String name;
  65. @Lob
  66. private byte[] picture;
  67.  
  68. public int getId() {
  69. return id;
  70. }
  71.  
  72. public void setId(int id) {
  73. this.id = id;
  74. }
  75.  
  76. public String getName() {
  77. return name;
  78. }
  79.  
  80. public void setName(String name) {
  81. this.name = name;
  82. }
  83.  
  84. public byte[] getPicture() {
  85. return picture;
  86. }
  87.  
  88. public void setPicture(byte[] picture) {
  89. this.picture = picture;
  90. }
  91.  
  92.  
  93.  
  94. }
  95.  
  96. public class ImagensParaDBController {
  97.  
  98. EntityManagerFactory mf = Persistence.createEntityManagerFactory("mySQLPU");
  99. EntityManager em = mf.createEntityManager();
  100.  
  101. public void insertImageDB(ImagensParaBD img, ImagensParaBD name, ImagensParaBD picture) {
  102.  
  103. em.getTransaction().begin();
  104. em.merge(img);
  105. em.getTransaction().commit();
  106.  
  107. }
  108.  
  109. public void disconnect() {
  110.  
  111. em.close();
  112. }
  113.  
  114.  
  115. public void insertImageDB(ImagensParaBD img) {
  116. throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  117. }
  118.  
  119. }
  120.  
  121. List<ImagensParaBD> all = em.createQuery("Select a From ImagensParaBD a where name=:name",
  122. ImagensParaBD.class)
  123. .setParameter("name", theNameTOFind)
  124. .getResultList();
Add Comment
Please, Sign In to add comment