Skip to content

Commit a61ef91

Browse files
committed
refactor(post): change Blob for byte[]
1 parent 588551c commit a61ef91

File tree

4 files changed

+18
-23
lines changed

4 files changed

+18
-23
lines changed

src/main/java/controllers/admin/PostController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import java.util.ArrayList;
1212
import java.util.List;
1313
import java.util.Locale;
14-
import javax.sql.rowset.serial.SerialBlob;
1514
import javax.validation.Valid;
1615
import models.FileImage;
1716
import models.Post;
@@ -101,12 +100,13 @@ public String processPost(@RequestPart("postImage") MultipartFile postImage, @Mo
101100
if(errors.hasErrors()){
102101
return "admin/post/create";
103102
}
103+
104104
if(postImage != null){
105105
FileImage image = new FileImage();
106106
image.setName(postImage.getName());
107107
image.setContentType(postImage.getContentType());
108108
image.setSize(postImage.getSize());
109-
image.setContent(new SerialBlob(postImage.getBytes()));
109+
image.setContent(postImage.getBytes());
110110
post.setImage(image);
111111
}
112112
post.setAuthor(activeUser);

src/main/java/controllers/frontend/PostController.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
package controllers.frontend;
77

88
import exceptions.PostNotFoundException;
9-
import java.sql.Blob;
109
import java.sql.SQLException;
1110
import models.FileImage;
1211
import org.slf4j.Logger;
@@ -50,14 +49,9 @@ public String show(@PathVariable Long postId, Model model){
5049
public ResponseEntity<byte[]> downloadPostImage(@PathVariable Long postId) throws SQLException {
5150
FileImage postImage = postService.getImageByPostId(postId);
5251
logger.info("Post Image Information: " + postImage.toString());
53-
Blob blob = postImage.getContent();
54-
int blobLength = (int) blob.length();
55-
byte[] blobAsBytes = blob.getBytes(1, blobLength);
56-
//release the blob and free up memory. (since JDBC 4.0)
57-
blob.free();
5852
return ResponseEntity.ok()
5953
.contentLength(postImage.getSize())
6054
.contentType(MediaType.parseMediaType(postImage.getContentType()))
61-
.body(blobAsBytes);
55+
.body(postImage.getContent());
6256
}
6357
}

src/main/java/models/FileImage.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
package models;
77

88
import java.io.Serializable;
9-
import java.sql.Blob;
109
import javax.persistence.Column;
1110
import javax.persistence.Entity;
1211
import javax.persistence.GeneratedValue;
1312
import javax.persistence.GenerationType;
1413
import javax.persistence.Id;
14+
import javax.persistence.Lob;
1515
import javax.persistence.OneToOne;
1616
import javax.persistence.Table;
1717

@@ -32,7 +32,8 @@ public class FileImage implements Serializable {
3232
@Column(nullable = true)
3333
private Long size;
3434
@Column(nullable = false)
35-
private Blob content;
35+
@Lob
36+
private byte[] content;
3637
@OneToOne(mappedBy = "image")
3738
private Post post;
3839

@@ -68,14 +69,13 @@ public void setSize(Long size) {
6869
this.size = size;
6970
}
7071

71-
public Blob getContent() {
72+
public byte[] getContent() {
7273
return content;
7374
}
7475

75-
public void setContent(Blob content) {
76+
public void setContent(byte[] content) {
7677
this.content = content;
7778
}
78-
7979

8080
public Post getPost() {
8181
return post;

src/main/webapp/WEB-INF/templates/admin/fragment/post.html

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,20 @@
2727
</div>
2828
<div class="row control-group">
2929
<div class="form-group col-xs-12">
30-
<label th:text="#{form.post.status}">Post status</label>
31-
<div class="radio col-xs-12">
32-
<label for="radio_published" th:text="#{form.post.published}">Published</label>
33-
<input th:field="*{published}" type="radio" id="radio_published" name="post_status" value="true" />
34-
</div>
35-
<div class="radio col-xs-12">
36-
<label for="radio_not_published" th:text="#{form.post.notpublished}">Not Published</label>
37-
<input th:field="*{published}" type="radio" id="radio_not_published" name="post_status" value="false" />
30+
<label class="col-md-2 control-label" th:text="#{form.post.status}">Post status</label>
31+
<div class="col-md-10">
32+
<div class="radio">
33+
<label for="radio_published" th:text="#{form.post.published}">Published</label>
34+
<input th:field="*{published}" type="radio" id="radio_published" name="post_status" value="true" />
35+
</div>
36+
<div class="radio">
37+
<label for="radio_not_published" th:text="#{form.post.notpublished}">Not Published</label>
38+
<input th:field="*{published}" type="radio" id="radio_not_published" name="post_status" value="false" />
39+
</div>
3840
</div>
3941
</div>
4042
</div>
4143
<div class="row control-group">
42-
<legend th:text="#{form.post.image}">Post Image</legend>
4344
<div class="form-group">
4445
<label class="col-md-2 control-label" th:text="#{form.post.image.label}">Image</label>
4546
<div class="col-md-10">

0 commit comments

Comments
 (0)