我在从MySQL数据库中读取blob时遇到了一些麻烦。我已经把它成功插入到数据库中,但似乎无法让它回读。我知道有些人可能在想“为什么他使用数据库来存储图像的blob,而不仅仅是文件路径/文件名”,但我想要有灵活性,因为很多这些图像都会存储在服务器而不是本地服务器,这可以优化效率,并允许我根据需要将图像移动到本地。我已经按照(简短)教程编写了以下方法来接收blob。
以下是我的域类:
@Entity
@Table(name="PROFILE")
public class Profile implements Serializable{
private static final long serialVersionUID = 3641L;
@Id
@Column(name="PROFILE_ID")
@GeneratedValue(strategy=GenerationType.AUTO)
private int profileId;
@OneToOne
@JoinColumn(name="CUSTOMER_ID")
private Customer customer;
@Column(name = "FIRST_NAME")
private String firstName;
@Column(name = "LAST_NAME")
private String lastName;
@Column(name = "ADDRESS")
private String address;
@Column(name = "CONTACT_NUMBER")
private String contactNumber;
@Column(name = "PROJECT_NAME")
private String projectName;
@Column(name="CONTENT")
@Lob
private Blob content;
@Column(name="filename")
private String filename;
@Column(name="content_type")
private String contentType;
//getters and setter
}
这是我的控制者:
@RequestMapping(value="/myProfile.htm", method=RequestMethod.GET)
public ModelAndView Profilelist(HttpServletRequest request,ModelMap model,Customer
customer,Profile profile, HttpServletResponse response) throws SQLException ,
Exception{
//Profile profile = new Profile();
String customerName = request.getUserPrincipal().getName();
customer = customerService.getCustomerId(customerName);
profile = profileService.getBycustomerId(customer);
System.out.println("cust: "+ customer);
System.out.println("profile: "+ profile);
logger.error("Viewing Profile" +customerName);
//Customer customer = new Customer();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
byte[] encodeBase64 = Base64.encodeBase64(buf);
String base64Encoded = new String(encodeBase64, "UTF-8");
ModelAndView mav = new ModelAndView();
mav.addObject("profile", base64Encoded);
//Blob blob = profile.getContent();
customer.setEmailId(customerName);
profile.setCustomer(customer);
//profile.setContent(blob);
System.out.println();
profile = profileService.findProfileById(customer);
model.addAttribute("content",base64Encoded);
model.addAttribute("profile", profile);
mav = new ModelAndView("myProfile");
return mav;
}
这里无法将字节设置为我的profile.setContent ......请给出任何解决方案以摆脱我的问题
答案 0 :(得分:2)
您不应该在控制器中,从文档
中执行此操作默认情况下,驱动程序使用SQL定位器(BLOB)实现Blob 表示Blob对象包含指向SQL BLOB的逻辑指针 数据而不是数据本身。 Blob对象对于有效 已创建的交易的持续时间。
如果您需要访问字节数组,则应该执行的操作是在Profile类中创建JPA瞬态属性,例如:
之类的东西 @Transient byte[] image; // do add getters and setters
并将字节数组设置为该属性。持久性机制会忽略瞬态属性。
关于评论的更新
由于目的是在jsp中显示图片,您可以采用两种方式,第一种方式,将文件存储在Web可访问的位置,只需从 src 元素指向它,但我从你的问题中理解,这不是你需要的。
第二个选项是将图像渲染为字节序列,但为了做到这一点,首先必须将字节数组编码为Base64编码的字符串。这是一个例子
要在JSP上显示图像而不存储到文件系统并链接到它,您必须对字节数组进行Base64编码。将控制器方法更改为
@RequestMapping(value="/myProfile.htm", method=RequestMethod.GET)
public String Profilelist(HttpServletRequest request,ModelMap model,Customer
customer,Profile profile, HttpServletResponse response) throws SQLException ,
Exception{
//Profile profile = new Profile();
String customerName = request.getUserPrincipal().getName();
customer = customerService.getCustomerId(customerName);
profile = profileService.getBycustomerId(customer);
System.out.println("cust: "+ customer);
System.out.println("profile: "+ profile);
logger.error("Viewing Profile" +customerName);
//Customer customer = new Customer();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
Blob blob = profile.getContent();
InputStream in = blob.getBinaryStream();
System.out.println("id content" +in);
int n = 0;
while ((n=in.read(buf))>=0)
{
baos.write(buf, 0, n);
}
in.close();
byte[] bytes = baos.toByteArray();
System.out.println("bytes" +bytes);
byte[] encodeBase64 = Base64.encodeBase64(buf);
String base64Encoded = new String(encodeBase64, "UTF-8");
customer.setEmailId(customerName);
profile.setCustomer(customer);
//profile.setContent(blob);
System.out.println();
profile = profileService.findProfileById(customer);
model.addAttribute("content",base64Encoded);
model.addAttribute("profile", profile);
return "myProfile";
}