我已经在servlet中使用此代码保存了图像。 productSubType和Product是我数据库中的表。 ProductSubType的foriegn键包含在Product表中。
try (PrintWriter out = response.getWriter()) {
Session s = connection.NewHibernateUtil.getSessionFactory().openSession();
String name=request.getParameter("fupload");
File f= new File(getServletContext().getRealPath("/")+""+name);
ProductSubType p_Stype = new ProductSubType();
p_Stype=(ProductSubType) s.load(ProductSubType.class, Integer.parseInt(request.getParameter("p_combo")));
Product p = new Product();
p.setPName(request.getParameter("p_name"));
p.setKeyWords(request.getParameter("keywords"));
p.setQuality(request.getParameter("width")+","+request.getParameter("height"));
p.setProductSubType(p_Stype);
p.setProduct(f.getPath());
p.setPrice(request.getParameter("price"));
s.save(p);
s.beginTransaction().commit();
response.sendRedirect("add_image.jsp");
}
这是我的产品类
public class Product implements java.io.Serializable {
private Integer idproduct;
private ProductSubType productSubType;
private String PName;
private String quality;
private String product;
private String price;
private String keyWords;
private Set<Upload> uploads = new HashSet<Upload>(0);
private Set<InvoiceHasProduct> invoiceHasProducts = new HashSet<InvoiceHasProduct>(0);
private Set<CartHasProduct> cartHasProducts = new HashSet<CartHasProduct>(0);
private Set<Download> downloads = new HashSet<Download>(0);
public Product() {
}
public Product(ProductSubType productSubType) {
this.productSubType = productSubType;
}
public Product(ProductSubType productSubType, String PName, String quality, String product, String price, String keyWords, Set<Upload> uploads, Set<InvoiceHasProduct> invoiceHasProducts, Set<CartHasProduct> cartHasProducts, Set<Download> downloads) {
this.productSubType = productSubType;
this.PName = PName;
this.quality = quality;
this.product = product;
this.price = price;
this.keyWords = keyWords;
this.uploads = uploads;
this.invoiceHasProducts = invoiceHasProducts;
this.cartHasProducts = cartHasProducts;
this.downloads = downloads;
}
public Integer getIdproduct() {
return this.idproduct;
}
public void setIdproduct(Integer idproduct) {
this.idproduct = idproduct;
}
public ProductSubType getProductSubType() {
return this.productSubType;
}
public void setProductSubType(ProductSubType productSubType) {
this.productSubType = productSubType;
}
public String getPName() {
return this.PName;
}
public void setPName(String PName) {
this.PName = PName;
}
public String getQuality() {
return this.quality;
}
public void setQuality(String quality) {
this.quality = quality;
}
public String getProduct() {
return this.product;
}
public void setProduct(String product) {
this.product = product;
}
public String getPrice() {
return this.price;
}
public void setPrice(String price) {
this.price = price;
}
public String getKeyWords() {
return this.keyWords;
}
public void setKeyWords(String keyWords) {
this.keyWords = keyWords;
}
public Set<Upload> getUploads() {
return this.uploads;
}
public void setUploads(Set<Upload> uploads) {
this.uploads = uploads;
}
public Set<InvoiceHasProduct> getInvoiceHasProducts() {
return this.invoiceHasProducts;
}
public void setInvoiceHasProducts(Set<InvoiceHasProduct> invoiceHasProducts) {
this.invoiceHasProducts = invoiceHasProducts;
}
public Set<CartHasProduct> getCartHasProducts() {
return this.cartHasProducts;
}
public void setCartHasProducts(Set<CartHasProduct> cartHasProducts) {
this.cartHasProducts = cartHasProducts;
}
public Set<Download> getDownloads() {
return this.downloads;
}
public void setDownloads(Set<Download> downloads) {
this.downloads = downloads;
}
}
保存图像。保存没有错误。但图像的路径将保存到数据库中 - &gt; E:... \ ProjectName \ build \ web \ 190847.png
这是对的吗?如果是这样,我如何检索图像并在页面中显示它们?
答案 0 :(得分:0)
每次在服务器上上传图像文件时。我们应该在数据库中存储图像的相对路径或虚拟路径,以便可以从html页面查看这些路径或虚拟路径。 就像你需要存储图像的路径一样,如下所示:
http://yourserverdomain/web/image_name.png
还要确保服务器上的Web文件夹是公用文件夹。