package com.gao.ruan.service.imp;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.UUID;
import javax.annotation.Resource;
import javax.faces.application.Application;
import org.apache.struts.upload.FormFile;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;
import server.form.bookForm;
import com.gao.ruan.pojos.Book;
import com.gao.ruan.pojos.Category;
import com.gao.ruan.service.inter.bookServiceInter;
import com.gao.ruan.service.inter.categoryServiceInter;
@Transactional
public class bookServiceImp implements bookServiceInter {
@Resource
private SessionFactory sessionFactory;
@Resource
private categoryServiceInter categoryServiceImp;
@Override
public void add(bookForm mm) {
Book kk=new Book();
kk.setAuthor(mm.getAuthor()); //书的作者
kk.setBookName(mm.getNamee()); //书的名字
kk.setPrice(Float.parseFloat(mm.getPrice())); //书的价格
String oldName=mm.getPhoto().getFileName(); //保存图片状态
String houZhui= oldName.substring(mm.getPhoto().getFileName().lastIndexOf(".")); //图片格式 //图片
String newNameString=makeName(houZhui); //图片的新名字
kk.setDescription(mm.getDescription()); //描述
kk.setPicNameOld(oldName); //保存资源的旧名字
kk.setPicNameNew(newNameString); //保存图片的新名字
Category cc=categoryServiceImp.findCategoryById(mm.getCategory());
kk.setCategory(cc); //所属分类
try {
String outPutPath=getAbsolutePathByClass()+"bookImages"+"/"+newNameString; //图片保存的绝对路径
savePic(outPutPath, mm.getPhoto());
System.out.println(outPutPath);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sessionFactory.getCurrentSession().save(kk); //保存一本书
}
private String getAbsolutePathByClass() throws Exception { //通过类路径获取工程绝对路径,也就是工程根目录在tomcat中的路径
String webPath = this.getClass().getResource("/").getPath().replaceAll("^\\/", "");
webPath = webPath.replaceAll("[\\\\\\/]WEB-INF[\\\\\\/]classes[\\\\\\/]?", "/");
webPath = webPath.replaceAll("[\\\\\\/]+", "/");
webPath = webPath.replaceAll("%20", " ");
if (webPath.matches("^[a-zA-Z]:.*?$")) {
} else {
webPath = "/" + webPath;
}
webPath += "/";
webPath = webPath.replaceAll("[\\\\\\/]+", "/");
return webPath;
}
public String makeName(String prefixString){ //产生图片的名字
return UUID.randomUUID()+prefixString;
}
public void savePic(String outPutPathString,FormFile ff){
InputStream is=null;
OutputStream os=null;
try {
is=ff.getInputStream();
os=new FileOutputStream(outPutPathString);
int len=0;
byte[] bytes=new byte[1024];
while((len=is.read(bytes))>0){
os.write(bytes,0,len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
is.close();
os.close();
ff.destroy();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public List<Book> findAllBooks() {
//返回所有图书
List<Book> jihe=sessionFactory.getCurrentSession().createQuery("from Book").list();
return jihe;
}
@Override
public void delBook(int id) {
//删除某个id的分类
org.hibernate.Query kk = sessionFactory.getCurrentSession()
.createQuery("delete from Book where id=?");
kk.setInteger(0, id);
kk.executeUpdate();
}
}
FR:徐海涛(Hunk Xu)
QQ技术交流群:386476712