【发布时间】:2015-01-23 06:13:15
【问题描述】:
我在从 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 ......请提供任何解决方案来解决我的问题
【问题讨论】:
标签: java mysql hibernate spring-mvc