博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
javaweb 文件blob上传到数据库以及反显
阅读量:6892 次
发布时间:2019-06-27

本文共 4333 字,大约阅读时间需要 14 分钟。

  hot3.png

简单的例子:

Service:

    @Service("bookService")

public class BookService implements BookServiceInterface {
    @Resource(name = "bookDAO")
    private BookDAOInterface bookDAO;
    @Resource(name = "picDAO")
    private PicDAOInterface picDAO;

    public boolean addBook(Book book, List<File> listPic) {

        try {
            File f;
            f = book.getFile();
            book.setBlobFile(GetBlob.getBlob(f));
            bookDAO.insert(book);
            int bookId = book.getId();
            int count = listPic.size();
            for (int i = 0; i < count; i++) {
                f = listPic.get(i);
                Pic p = new Pic();
                p.setBookId(bookId);
                p.setPic(GetBlob.getBlob(f));
                picDAO.insert(p);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }

    public Book findBook(int id) {

        return bookDAO.getBook(id);
    }

}

@Service("picService")

public class PicService implements PicServiceInterface {
    @Resource(name = "picDAO")
    private PicDAOInterface picDAO;

    public List<Pic> findPicByBookId(int bookId) {

        return picDAO.queryPicByBookId(bookId);
    }

}

Action:

@Controller("bookAction")

@ParentPackage("struts-default")
@Namespace("/xxx")
public class BookAction extends ActionSupport {
    @Resource(name = "bookService")
    private BookService bookService;
    private Book book;
    private File f1;
    private String f1FileName;
    private File f2;
    private String f2FileName;
    private File f3;
    private String f3FileName;

    public Book getBook() {

        return book;
    }

    public void setBook(Book book) {

        this.book = book;
    }

    public File getF1() {

        return f1;
    }

    public void setF1(File f1) {

        this.f1 = f1;
    }

    public String getF1FileName() {

        return f1FileName;
    }

    public void setF1FileName(String f1FileName) {

        this.f1FileName = f1FileName;
    }

    public File getF2() {

        return f2;
    }

    public void setF2(File f2) {

        this.f2 = f2;
    }

    public String getF2FileName() {

        return f2FileName;
    }

    public void setF2FileName(String f2FileName) {

        this.f2FileName = f2FileName;
    }

    public File getF3() {

        return f3;
    }

    public void setF3(File f3) {

        this.f3 = f3;
    }

    public String getF3FileName() {

        return f3FileName;
    }

    public void setF3FileName(String f3FileName) {

        this.f3FileName = f3FileName;
    }

    @Action(value = "inputAction", results = { @Result(name = "success", location = "xxx.jsp", type = "redirect") })

    public String inputBook() {
        ActionContext ac = ActionContext.getContext();
        Map session = ac.getSession();
        List<File> listPic = new ArrayList<File>();
        System.out.println(f1);
        listPic.add(f1);
        listPic.add(f2);
        listPic.add(f3);
        boolean flag = bookService.addBook(book, listPic);
        if (flag) {
            session.put("book", book);
            return "success";
        } else {
            return "failure";
        }
    }

}

@Controller("showPicAction")

@ParentPackage("struts-default")
@Namespace("/xxx")
public class ShowBookAction {
    @Resource(name = "bookService")
    private BookServiceInterface bookService;
    @Resource(name = "picService")
    private PicServiceInterface picService;
    private Integer id;
    private Integer picId;

    public Integer getPicId() {

        return picId;
    }

    public void setPicId(Integer picId) {

        this.picId = picId;
    }

    public Integer getId() {

        return id;
    }

    public void setId(Integer id) {

        this.id = id;
    }

    @Action(value = "showPicsAction")

    public String showPics() {
        List<Pic> pics = picService.findPicByBookId(id);
        Pic p = pics.get(picId);
        Blob blob = p.getPic();
        byte[] b = null;
        if (blob != null) {
            long index = 1;
            try {
                b = blob.getBytes(index, (int) (blob.length()));
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                OutputStream outputStream = ServletActionContext.getResponse()
                        .getOutputStream();
                InputStream in = new ByteArrayInputStream(b);
                int len = 0;
                byte[] buf = new byte[1024];
                while ((len = in.read(buf, 0, 1024)) != -1) {
                    outputStream.write(buf, 0, len);
                }
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Action(value = "showBookPicAction")

    public String showBookPic() {
        Book book = bookService.findBook(id);
        Blob blob = book.getBlobFile();
        byte[] b = null;
        if (blob != null) {
            long lo = 1;
            try {
                b = blob.getBytes(lo, (int) (blob.length()));
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                OutputStream outputStream = ServletActionContext.getResponse()
                        .getOutputStream();
                InputStream in = new ByteArrayInputStream(b);
                int len = 0;
                byte[] buf = new byte[1024];
                while ((len = in.read(buf, 0, 1024)) != -1) {
                    outputStream.write(buf, 0, len);
                }
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}

效果:

200800_qChF_2841767.png

200826_LpVW_2841767.png

 

转载于:https://my.oschina.net/lmoon/blog/1518256

你可能感兴趣的文章
集合的概念及应用和HashSet保证数据不重复的原理
查看>>
机器学习中的数据清洗与特征处理综述
查看>>
第一单元作业
查看>>
My Favorite Ten NBA Players and Their Sneakers
查看>>
oldboy学习总结1
查看>>
老虞要学GoLang-代码规范
查看>>
Web前端面试指导(七):入职后的建议
查看>>
代理IP对爬虫的重要性以及Python3如何设置代理
查看>>
搭建之星中文编程汉语编程计算机快速入门教学教程
查看>>
Excel用数据条展示数据大小
查看>>
什么叫技术好?
查看>>
典型的开发国内小项目没失败的经验分享
查看>>
我的友情链接
查看>>
ttf-mscorefonts-installer 无法安装,解决办法
查看>>
我的友情链接
查看>>
Linux下查看Nginx Apache MySQL的并发连接数和连接状态
查看>>
shell 学习笔记
查看>>
我的友情链接
查看>>
常用系统优化参数
查看>>
系统进程内存模型
查看>>