将图像添加到mysql数据库不起作用

时间:2017-05-23 12:25:08

标签: java mysql spring hibernate jsp

您好我想将我的照片添加到mysql数据库但是它不起作用,我在工作台中有BLOB但是当我在编辑器中打开值时我只看到图像和二进制值的名称,我没有看看BLOB的图片和大小与图片大小不一致。文本正确地保存到行。

MODEL:

@Entity
@Table(name = "skis")
public class Skis extends BaseEntity  {


    @Column(name = "company", length = 20)
   private String company;

    @Column(name = "model", length = 20)
    private String model;

    @Column(name = "description", length = 200)
    private String description;

    @Column(name = "photo", columnDefinition = "MEDIUMBLOB")
    private byte[] photo;

    public Skis(String company, String model, String description, byte[] photo) {
        this.company = company;
        this.model = model;
        this.description = description;
        this.photo = photo;

    }

    public Skis() {

    }

   public String getCompany() {
       return company;
   }
 public void setCompany(String company) {
        this.company = company;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public byte[] getPhoto() {
        return photo;
    }

    public void setPhoto(byte[] photo) {
        this.photo = photo;
    }
}

控制器:

@Controller
public class SkisController {

    @Autowired
    private SkisDAO skisDAO;

    @RequestMapping(value = "/ski/save", method = RequestMethod.POST)
    public String postCreateSki(@ModelAttribute Skis skis){

        skisDAO.save(skis);

        return "redirect:/skis";

    }

    @RequestMapping(value = "/ski-create", method = RequestMethod.GET)
    public String getCreateSkisForm(Model model) {

        model.addAttribute("skis", new Skis());

        return "add-skis";
    }


}

JSP:

   <div class="container">

<div class="row">
    <form:form commandName="skis" action="${createSkiURL}" method="post" role="form" class="form-horizontal">
        <form:hidden path="id" />
    <div class="form-group">
        <label class="control-label col-sm-2" for="company">company:</label>
        <div class="col-sm-6">
            <form:input path="company" type="text" id="company" class="form-control" placeholder="Enter company" autofocus="autofocus" />
            </div>
    </div>

        <div class="form-group">
            <label class="control-label col-sm-2" for="model">model:</label>
            <div class="col-sm-6">
                <form:input path="model" type="text" id="model" class="form-control" placeholder="Enter model" autofocus="autofocus" />
            </div>
        </div>

        <div class="form-group">
            <label class="control-label col-sm-2" for="description">description:</label>
            <div class="col-sm-6">
                <form:input path="description" type="text" id="description" class="form-control" placeholder="Enter description" autofocus="autofocus" />
            </div>
        </div>

        <div class="form-group">
            <label class="control-label col-sm-2" for="photo">photo:</label>
            <form:input path="photo" type="file" id="photo" />
        </div>

        <div class="form-group">
            <div class="col-sm-offset-2 col-sm-6">
                <button type="submit" class="btn btn-primary">Zapisz</button>
            </div>
        </div>
    </form:form>
</div>
</div>

DAO:

@Repository
public interface SkisDAO extends JpaRepository<Skis, Long> {
}

另一个问题:

如何在另一个jsp上显示图像?我知道如何显示文字,但不知道如何从数据库显示照片

0 个答案:

没有答案