主题:jsp图片上传(救急,回复就加分)
shangxiaogang
[专家分:210] 发布于 2007-04-10 09:55:00
[size=6][size=5][size=3]小弟想求一个实现JSP文件上传后保存到数据库里(SQLServer),并不是保存文件各称,而是实际的文件,然后从数据库中读出来并显示到网页上,哪位有这样的例子,把代码贴出来看一下,急啊???
回复就加分啊[/size][/size][/size]
回复列表 (共2个回复)
板凳
mwsoft [专家分:760] 发布于 2007-04-18 11:57:00
Session s = null;
byte[] buffer = new byte[1];
buffer[0] = 1;
try {
Configuration ds = new
Configuration().configure("/hibernate.cfg.xml");
SessionFactory sf = ds.buildSessionFactory();
s = sf.openSession();
Transaction tx = s.beginTransaction();
ParentCatPO parentcat = new ParentCatPO();
parentcat.setName("老猫");
CatPO c = new CatPO();
c.setCatname("小猫1");
c.setImage(Hibernate.createBlob(buffer));
c.setParentcat(parentcat);
Set catlist = new HashSet();
catlist.add(c);
parentcat.setCats(catlist);
s.save(parentcat);
s.flush();
s.refresh(parentcat, LockMode.UPGRADE);
Iterator it = parentcat.getCats().iterator();
while (it.hasNext()) {
CatPO newcat = ( (CatPO) it.next());
BLOB blob = (BLOB) newcat.getImage();
OutputStream out = blob.getBinaryOutputStream();
String fileName = "oraclejdbc.jar";
File f = new File(fileName);
FileInputStream fin = new FileInputStream(f);
int count = -1, total = 0;
byte[] data = new byte[ (int) fin.available()];
fin.read(data);
out.write(data);
fin.close();
out.close();
}
s.flush();
tx.commit();
}
catch (ClassCastException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if (s != null) {
try {
s.close();
}
catch (Exception e) {}
}
}
import java.sql.Blob;
public class CatPO {
private String catid;
private String parentid;
private String catname;
private ParentCatPO parentcat;
private Blob image;
public CatPO() {
}
public String getCatid() {
return catid;
}
public void setCatid(String catid) {
this.catid = catid;
}
public String getCatname() {
return catname;
}
public void setCatname(String catname) {
this.catname = catname;
}
public Blob getImage() {
return image;
}
public void setImage(Blob image) {
this.image = image;
}
public String getParentid() {
return parentid;
}
public void setParentid(String parentid) {
this.parentid = parentid;
}
public ParentCatPO getParentcat() {
return parentcat;
}
public void setParentcat(ParentCatPO parentcat) {
this.parentcat = parentcat;
}
我来回复