我正在尝试使用hibernate和spring在mysql数据库中上传图像 ajax调用在createImge.jsp页面中完成,并提供控制器来处理请求,其中图像绝对路径存储在变量中,并将整个实体对象传递给daoImpl类,其中hibernate程序用于存储在数据库中。
我没有收到任何错误,但问题是它没有插入到数据库中并且收到的警报未从ajax插入,即使在控制台上也没有错误。
请为此提供任何解决方案。
CreateImge.jsp
<html>
<body>
<form class="form-horizontal bucket-form" id="myform" method="post" >
<div class="control-label text-center">
<p class="required"><em>required fields</em></p></div>
<div class="form-group">
<label class="col-sm-3 control-label">ID</label>
<div class="col-sm-6">
<input type="number" class="form-control" id="id" >
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Upload
image</label>
<div class="col-sm-6">
<input type="file" class="form-control" id="path"
accept="image/*">
</div>
</div>
<div class="form-group">
<div class="col-lg-offset-3 col-lg-6">
<button class="btn btn-primary" id="speakerSubmit"
type="submit">Save</button>
<button type="reset" class="btn btn"
onClick="myFunction()">Reset</button>
<script type="text/javascript">
function myFunction() {
document.getElementById("myform").reset();
}
</script>
</div>
</div>
</form>
<script src="js/jquery2.0.3.min.js"></script>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">
</script>
<script src="js/bootstrap.js"></script><!--bootstrap script-->
<script src="js/jquery.dcjqaccordion.2.7.js"></script><!--accordion
script-->
<script src="js/scripts.js"></script><!--accordion script-->
<script src="js/jquery.nicescroll.js"></script><!--toggle_effect script-->
<script>
$( document ).ready(function() {
$("#speakerSubmit").click(function(event){
event.preventDefault();
alert("create ready");
var id=document.getElementById("id").value;
var path=document.getElementById("path").value;
alert(path);
var details={id:id,path:path};
alert(details.id+" "+details.path);
var res=JSON.stringify(details);
alert("mytxt "+res);
$.ajax({
url: "http://localhost:8080/EMS_APP/imgadd",
type: "POST",
contentType:"application/json",
data : res,
success: function(data, status) {
if(data){
alert("inserted");
}else{
//window.open("index.jsp","_self");
alert("not inserted");
}
},
error: function(e) {
console.log("error");
}
});
});
});
</script>
</body>
</html>
Pojo课程
package com.ems.DO;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="forimgs")
public class ImgeClass {
@Id
@Column(name="id")
private int id;
@Column(name="path")
private String path;
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="imgstore")
private byte[] image;
public byte[] getImage() {
return image;
}
public void setImage(byte[] image) {
this.image = image;
}
}
控制器类方法
@RequestMapping(value="/imgadd",method=RequestMethod.POST)
public @ResponseBody void addimage (@RequestBody ImgeClass e){
System.out.println(e.getPath());
speakerService.addImg(e);
}
ServiceImpl类方法
@Override
public void addImg(ImgeClass e) {
System.out.println("in service");
speakerDao.addImage(e);
}
DaoImpl类方法
public void addImage(ImgeClass e)
{
System.out.println("in dao");
System.out.println(e.getId());
System.out.println(e.getPath());
String path=e.getPath();
File file=new File(path);
System.out.println("Your file length is "+file.length());
byte[] bfile=new byte[(int) file.length()];
try {
FileInputStream fileInputStream =new FileInputStream(file);
fileInputStream.read(bfile);
fileInputStream.close();
for(byte b : bfile) { //Just to check whether bfile has any content
System.out.print(b +" ");
}
}
catch(Exception ex) {
ex.printStackTrace();
}
e.setImage(bfile);
Session session = sessionFactory.getSessionFactory().openSession();
System.out.println(e.getId()+" "+e.getPath()+" "+e.getImage());
session.persist(e);
//System.out.println("value");
System.out.println("One image uploaded into database");
}