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<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;
}
}
效果:
转载于:https://my.oschina.net/lmoon/blog/1518256