1<%@page contentType="text/html" pageEncoding="UTF-8"%>
2<!DOCTYPE html>
3<html>
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
6<title>JSP Page</title>
7</head>
8<body>
9<h1>Details are given below:</h1><hr>
10<table>
11<tr>
12<td>Id</td>
13<td>${id}</td>
14</tr>
15<tr>
16<td>Name</td>
17<td>${name}</td>
18</tr>
19<tr>
20<td>Image</td>
21<td><img src="data:image/jpg;base64,${image} alt="Girl in a jacket" style="width:50px;height:50px;"></img></td>
22</tr>
23</table>
24</body>
25</html>
26
1package com.vasu.SpringBootFileUpload.Model;
2
3import javax.persistence.Column;
4import javax.persistence.Entity;
5import javax.persistence.GeneratedValue;
6import javax.persistence.GenerationType;
7import javax.persistence.Id;
8import javax.persistence.Lob;
9import javax.persistence.Table;
10
11/**
12 *
13 * @author Vasu Rajput
14 */
15@Entity
16@Table(name = "ImageProfile")
17public class MyModel {
18
19 @Id
20 @GeneratedValue(strategy = GenerationType.AUTO)
21 @Column(name = "Id")
22 private long id;
23
24 @Column(name = "Name")
25 private String name;
26
27 @Lob
28 @Column(name = "Image")
29 private byte[] image;
30
31 public MyModel() {
32 super();
33 // TODO Auto-generated constructor stub
34 }
35 public MyModel(String name, byte[] image) {
36 super();
37 this.name = name;
38 this.image = image;
39 }
40 public long getId() {
41 return id;
42 }
43 public void setId(long id) {
44 this.id = id;
45 }
46 public String getName() {
47 return name;
48 }
49 public void setName(String name) {
50 this.name = name;
51 }
52 public byte[] getImage() {
53 return image;
54 }
55 public void setImage(byte[] image) {
56 this.image = image;
57 }
58}
59
1package com.vasu.SpringBootFileUpload.controller;
2
3import com.vasu.SpringBootFileUpload.Model.MyModel;
4import com.vasu.SpringBootFileUpload.Service.MyService;
5import java.util.Base64;
6import org.slf4j.Logger;
7import org.slf4j.LoggerFactory;
8import org.springframework.beans.factory.annotation.Autowired;
9import org.springframework.stereotype.Controller;
10import org.springframework.ui.Model;
11import org.springframework.web.bind.annotation.GetMapping;
12import org.springframework.web.bind.annotation.ModelAttribute;
13import org.springframework.web.bind.annotation.PathVariable;
14import org.springframework.web.bind.annotation.PostMapping;
15import org.springframework.web.bind.annotation.RequestBody;
16import org.springframework.web.bind.annotation.RequestParam;
17import org.springframework.web.bind.annotation.RestController;
18import org.springframework.web.multipart.MultipartFile;
19
20/**
21 *
22 * @author Vasu Rajput
23 */
24@Controller
25public class MyController {
26
27 private static final Logger logger = LoggerFactory.getLogger("MyController.class");
28 @Autowired
29 private MyService myService;
30
31 @GetMapping("/")
32 public String test() {
33 return "index";
34 }
35
36 @PostMapping("/fileupload")
37 public String fileUpload(@RequestParam("name") String name, @RequestParam("file") MultipartFile file) {
38 try {
39 logger.info("Name= " + name);
40 byte[] image = file.getBytes();
41 MyModel model = new MyModel(name, image);
42 int saveImage = myService.saveImage(model);
43 if (saveImage == 1) {
44 return "success";
45 } else {
46 return "error";
47 }
48 } catch (Exception e) {
49 logger.error("ERROR", e);
50 return "error";
51 }
52 }
53
54 @GetMapping("/getDetail/{id}")
55 public String getDbDetils(@PathVariable String id, Model model) {
56 try {
57 logger.info("Id= " + id);
58 MyModel imagesObj = myService.getImages(Long.parseLong(id));
59 model.addAttribute("id", imagesObj.getId());
60 model.addAttribute("name", imagesObj.getName());
61 byte[] encode = java.util.Base64.getEncoder().encode(imagesObj.getImage());
62 model.addAttribute("image", new String(encode, "UTF-8"));
63 return "imagedetails";
64 } catch (Exception e) {
65 logger.error("Error", e);
66 model.addAttribute("message", "Error in getting image");
67 return "redirect:/";
68 }
69 }
70}
71