java blob上传,javaweb 文件blob上传到数据库以及反显

简单的例子:

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 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 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 listPic = new ArrayList();

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 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;

}

}

效果:

3b5b6edc0abb1663923ec25aad00cdf6.png

1004f8f2ddb6cbab4e00a643ed61efa6.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值