Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. CREATE TABLE imagen(nombre VARCHAR(100), tipo VARCHAR(10), tamano BIGINT, pixel BLOB(10M))
  2.  
  3. @RequestMapping(path = "/form", method = RequestMethod.POST)
  4. public String handleFormUpload(
  5. @RequestParam("file") MultipartFile file) throws IOException {
  6.  
  7. if (!file.isEmpty()) {
  8.  
  9. String sql = "INSERT INTO imagen (nombre, tipo, tamano, pixel) VALUES(?, ?, ?, ?)";
  10.  
  11. String nombre = file.getOriginalFilename();
  12. String tipo = file.getContentType();
  13. Long tamano = file.getSize();
  14. byte[] pixel = file.getBytes();
  15.  
  16. jdbc.update(sql, nombre, tipo, tamano, pixel);
  17. }
  18.  
  19. return "redirect:/";
  20. }
  21.  
  22. $sql = "select link from tabla";
  23. $result = $conn->query($sql);
  24.  
  25. if ($result->num_rows > 0) {
  26.  
  27. while($row = $result->fetch_assoc()) {
  28. echo "<img src='".$row["link"]."' >";
  29. }
  30. }
  31. $conn->close();
  32.  
  33. public static void main(String[] args) throws Exception, IOException, SQLException {
  34. Class.forName("org.gjt.mm.mysql.Driver");
  35. Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/databaseName", "usuario", "password");
  36. String sqlQuery = "insert into tu_tabla(campo_foto) values ( ?)";
  37.  
  38. FileInputStream fis = null;
  39. PreparedStatement ps = null;
  40. try {
  41. conn.setAutoCommit(false);
  42. File archivo = new File("myPhoto.png");
  43. fis = new FileInputStream(archivo);
  44. ps = conn.prepareStatement(sqlQuery);
  45. ps.setBinaryStream(1, fis, (int) file.length());
  46. ps.executeUpdate();
  47. conn.commit();
  48. } finally {
  49. ps.close();
  50. fis.close();
  51. }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement