利用控件上传文件
起初方法:直接把文件写入数据库(Oracle)。由于Oracle字段Blob的特殊性,查阅了很多资料。Dao层的代码如下:
public void saveByBlob (Banyan banyan,FormFile fileName) throws SQLException, IOException{ banyan.setImage(Hibernate.createBlob(new byte [1])); getHibernateTemplate().save(banyan); getHibernateTemplate().flush(); getHibernateTemplate().update(banyan); getHibernateTemplate().refresh(banyan, LockMode.UPGRADE); SerializableBlob serializableBlob = (SerializableBlob) banyan.getImage(); java.sql.Blob javablob = serializableBlob.getWrappedBlob() ; oracle.sql.BLOB blob = (oracle.sql.BLOB) javablob ; BufferedInputStream in = new BufferedInputStream( fileName.getInputStream()); BufferedOutputStream out = new BufferedOutputStream(blob .getBinaryOutputStream()); byte [] buffer =new byte[102400]; int len; buffer =new byte[102400]; while((len=in.read(buffer))!=-1){ out.write(buffer,0,len); } in.close(); out.flush(); out.close(); }
这样的做法是,经常会碰到死锁现象,卡在那。。。不懂是什么情况。。。
成功使用方法:把文件传到服务器,然后把该文件在服务器上的地址存到数据库。又由于利用控件来操作。SmartUpload.initialize(config, request, response);涉及到config的,试了很多办法还是不懂得怎么在action或dao层进行,所以改用在servlet。其中jsp页面里要注意的是:form标签里要增加enctype="multipart/form-data" 。
public class ServletBanyanAdd extends HttpServlet { private ServletConfig config; final public void init(ServletConfig config) throws ServletException { this.config = config; } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub //设置图片存储命名为:urlPattern+文件名,避免重复的名字。 String urlPattern = banyan.getBanyanId().toString()+"-"; SmartUpload mySmartUpload = new SmartUpload(); String fileName=null; try { mySmartUpload.initialize(config, request, response); mySmartUpload.upload(); for (int i = 0; i < mySmartUpload.getFiles().getCount(); i++) { com.jspsmart.upload.File myfile = mySmartUpload.getFiles() .getFile(i); if (StringUtil.isNotNull(myfile.getFileName())) { fileName = urlPattern + myfile.getFileName().toLowerCase(); myfile.saveAs("/upload/" + fileName); banyan.setPicture("/bms/upload/" + fileName); } } } catch (Exception e) { } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
判断图片格式和大小的js,页面使用方法:<html:file property="picture" onchange="return checkType('picture');"></html:file>
function checkType(fieldName)//检查图片格式以及大小的函数 { var string = document.all[fieldName].value; if(string==null||string=="") return true; else{ string=string.toLowerCase(); var len=string.length; var suffix=string.substring(string.lastIndexOf('.')); //var suffix=string.substring(len-4,len); if(!(suffix==".jpg"||suffix==".gif"||suffix==".png"||suffix==".bmp")) { alert("上传图片格式不对"); //obj.focus(); document.all[fieldName].select(); document.execCommand('Delete');//清空上传文件空间内容 document.all[fieldName].focus(); return false; } var img=new Image(); img.src = document.all[fieldName].value; if(img.fileSize/1024>1024) { alert("上传的图片超过1M"); document.all[fieldName].select(); document.execCommand('Delete');//清空上传文件空间内容 document.all[fieldName].focus(); return false; } return true; } }