struts2之从数据库取出文件写进file属性框中

来源:互联网 发布:小清新淘宝素材psd 编辑:程序博客网 时间:2024/04/29 20:37

今天项目需要从数据库中读出文件路径然后显示在前台type=“file”属性框中,但是file是只读的也就是不可能写进去,所以只能采取折中的办法即用一个文本框显示路径内容覆盖文件上传框。代码如下

<td>
       <input id="file1_input" type="text" value="<s:property value='child.phtotoPath'/>">
       <input id="file1" type="file" style="width:20" onchange="document.getElementById('file1_input').value=this.value;" name="document">
  </td>

当然这个只是看着像写进了file属性框,但是当你提交时不可能提交给服务器,所以这个就要在后台进行处理,代码如下

if(document == null) {child.setPhtotoPath(childProxy.findByChildId(childId).getPhtotoPath());child.setChildVcrPath(childProxy.findByChildId(childId).getChildVcrPath());child.setMessagePath(childProxy.findByChildId(childId).getMessagePath());}else {for(int i=0; i<document.length; i++) {if(documentContentType[i].equals("application/msword")) {message = 1;child.setMessagePath(new FileUploadUtil().fileUpload(document[i], documentFileName[i],begainPath[2]));}if(documentContentType[i].equals("")) {vcr = 1;child.setChildVcrPath((new FileUploadUtil().fileUpload(document[i], documentFileName[i],begainPath[1])));}if(documentContentType[i].equals("image/pjpeg")) {photo = 1;child.setPhtotoPath((new FileUploadUtil().fileUpload(document[i], documentFileName[i],begainPath[0])));}}if(message!=1) {child.setMessagePath(childProxy.findByChildId(childId).getMessagePath());}if(vcr!=1) {child.setChildVcrPath(childProxy.findByChildId(childId).getChildVcrPath());}if(photo!=1) {child.setPhtotoPath(childProxy.findByChildId(childId).getPhtotoPath());}}

通过判断是否更改了文件来确定是否要从服务器读出文件路径读重新封装进类里。上面代码涉及到判断是更改了第几个文件,所以用类型来判断(当然我这里个文件的类型是不一样的哦,如果类型一样就希望读者自己想办法了),最后更新成功


 

原创粉丝点击