我想将图像保存为数据存储区中的blob文件。我使用JPA为对象编写了一个模块类:
@XmlRootElement
@Entity
public class PotholeMedia {
@Id
@GeneratedValue (strategy = GenerationType.IDENTITY)
private Long pothole_media_id;
private int pothole_Id;
private Blob image;
//Default constructor
public PotholeMedia (){}
//Overloaded constructor
public PotholeMedia(Long pothole_media_id, int pothole_Id, Blob image)
{
super();
this.pothole_media_id = pothole_media_id;
this.pothole_Id = pothole_Id;
this.image = image;
}//end constructor
/**
* Gets pothole_media_id attribute
* @return Long
*/
public Long getPotholeMediaId()
{
return pothole_media_id;
}//end getPotholeId method
/**
* Sets pothole_media_id attribute
* @param pothole_media_id
*/
public void setPotholeMediaId(Long pothole_media_id)
{
this.pothole_media_id = pothole_media_id;
}//end getPotholeMedia method
/**
* Get pothole_id attribute
* @return
*/
public int getPotholeId()
{
return pothole_Id;
}//end getPotholeId method
/**
* Set pothole_id attribute
* @param pothole_Id
*/
public void setPotholeId(int pothole_Id)
{
this.pothole_Id = pothole_Id;
}//end setPotholeId method
/**
* Get image attribute
* @return Blob
*/
public Blob getImage()
{
return image;
}//end getImage method
/**
* Set image attribute
* @param image
*/
public void setImage(Blob image)
{
this.image = image;
}//end setImage method
}//end PotholeMedia class
我的问题是我不知道如何使用泽西保存这个,这是我到目前为止所尝试过的。
@POST
@Path ("/save/image")
@Consumes (MediaType.MULTIPART_FORM_DATA)
public Response saveImg (@FormParam ("file") PotholeMedia media)
{
try
{
EntityManager manager = EMF.getInstance().createEntityManager();
manager.getTransaction().begin();
manager.persist(media);
manager.getTransaction().commit();
return Response.status(Status.OK).build ();
}
catch (Exception e)
{
return Response.status(Status.INTERNAL_SERVER_ERROR).build ();
}
}//end saveImg method
这是保存对象的正确方法,如果不是,我应该怎么做。