j2EE上传图片

本文介绍了一个图书管理系统的实现细节,包括图书信息的添加、查询及删除功能。系统使用Spring框架进行事务管理,Hibernate作为持久层框架,实现图书数据的增删查操作。同时,文章详细描述了如何处理图书图片上传,包括图片重命名、保存路径设置及图片数据的读写。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值