文件上传
2016-03-26 15:48
281 查看
jsp页面
<tr>
<td colspan="3" class=" xyz-field-collapse">
<div>
<div class="uploadPanel">
<a href="#" id="attachAddLink" class="underline-link attach-add">添加附件</a>
<div id="attachFile">
<input id="attachFileInfo" name="attachFileInfo" type="file"/>
</div>
<!-- 上传文件游览列表 -->
<ul id="attach_upload_list" style="max-height:150px; overflow-y:auto;"></ul>
</div>
</div>
</td>
<td style="text-align: right;">
<!-- 上传按钮 -->
<div id="attachContractDlgForm_btnUpload" />
</td>
</tr>
js(基于Extjs的文件上传)
<tr>
<td colspan="3" class=" xyz-field-collapse">
<div>
<div class="uploadPanel">
<a href="#" id="attachAddLink" class="underline-link attach-add">添加附件</a>
<div id="attachFile">
<input id="attachFileInfo" name="attachFileInfo" type="file"/>
</div>
<!-- 上传文件游览列表 -->
<ul id="attach_upload_list" style="max-height:150px; overflow-y:auto;"></ul>
</div>
</div>
</td>
<td style="text-align: right;">
<!-- 上传按钮 -->
<div id="attachContractDlgForm_btnUpload" />
</td>
</tr>
js(基于Extjs的文件上传)
//获取文件名 var getFileName = function(str){ var reg = /[^\\\/]*[\\\/]+/g; var str = str.replace(reg, ''); return str; };
//文件上传 var uploadFile = function(fileName){ Ext.Ajax.service({ url : SERVICE.UPLOAD_FILE_URL, jsonData : {}, success : function(jsonSrc){ var data = jsonSrc.getData(); if(data.retCode == '-1') { Ext.MessageBox.alert("提示", "添加附件失败,原因:" + data.msg); } else { var attachDTO = data.dto; addAttachMap(attachDTO); appendAttachLink(attachDTO); } fileField.reset(); }, failure : function(jsonSrc) { Ext.MessageBox.alert("提示", "添加附件失败,原因:" + jsonSrc.getMessage()); }, params :{ fileName : fileName, attachType : attachTypeCombox.getValue() }, form : "attachContractDlgForm", isUpload : true }); };
//添加map var addAttachMap = function(attachDTO){ var key = attachDTO.fileId; attachMap.add(key, attachDTO); }; //添加附件列表节点 var appendAttachLink = function (attachDTO) { var fileSize = attachDTO.fileSize; var tpl = new Ext.Template( '<li fileId="{fileId}">', '<span >' + attachDTO.fileName + ' (' + fileSize + ')' + '</span>', '<a href="#" class="del-link">删除 </a>', '</li>'); tpl.append('attach_upload_list', attachDTO); Ext.select("#attach_upload_list li[fileId=" + attachDTO.fileId + "] > a.del-link").on("click", delAttachEvent); Ext.get("attach_upload_list").addClass(uploadBorderCls); };
struts2中的Action代码: byte[] fileByteData = null; InputStream in = null; ByteArrayOutputStream out = new ByteArrayOutputStream(); try { in = attachFileInfo.getInputStream(); byte[] buff = new byte[1024]; int len = 0; while((len = in.read(buff, 0 ,100)) != -1){ out.write(buff, 0, len); } fileByteData = out.toByteArray(); //获取内存缓冲中的数据 } catch (Exception e) { map.put("retCode", "-1"); map.put("msg", e.getMessage()); e.printStackTrace(); return map; }finally{ if(out != null){ try { out.close(); } catch (IOException e) { e.printStackTrace(); } } if(in != null){ try { in.close(); } catch (IOException e) { e.printStackTrace(); } } }
FileOutputStream fos = null; BufferedOutputStream bos = null; try { //设置文件的大小 Double fileSizeKB = fileSize / 1024.0; DecimalFormat df = new DecimalFormat("########0.00"); //格式化数据 String fileSizeStr = df.format(fileSizeKB); dto.setFileSize(fileSizeStr + "KB"); DmStdTrInstrAttachDTO tempDTO = dmStdTrInstrAttachCtrl.addContractDmAttachMgr(sessionId, dto); //获取文件保存路径 String fileFullPath = path; File file = new File(fileFullPath); //文件不存在就创建文件 if(file!= null && !file.exists()){ new File(file.getParent()).mkdirs(); file.createNewFile(); } fos = new FileOutputStream(file); bos = new BufferedOutputStream(fos); //向文件写入数据 bos.write(fileByteData); bos.close(); fos.close(); return tempDTO; } catch (Exception e) { e.printStackTrace(); throw new ShineException(e.getMessage()); }finally{ if(fos != null) try { fos.close(); } catch (IOException e) { e.printStackTrace(); } if(bos != null) try { bos.close(); } catch (IOException e) { e.printStackTrace(); } }
相关文章推荐
- Win8.1系统中的任务管理器怎么打开?win8.1快速启动任务管理器的三种方法
- 获取当前系统的信息(CPU、物理内存、虚拟内存等
- 读取BMP图片并显示到窗口.
- 用pyenv解决在centos7下多版本python共存问题
- 解决GitHub安装失败
- 最大子数组和02
- 换行符在ajax中返回json,eval时发生的 Unexpected token ILLEGAL
- 链表常用技巧:快行指针
- 静态链表
- smplayer 不支持WMV,H264等格式的视频
- Windows7 64bit VS2013 Caffe test MNIST操作步骤
- JSONObject json = JSONObject.fromObject(josnStr);报错
- Mason 入门例子5 --- 增加学生之间的联系
- Windows7 64bit VS2013 Caffe test MNIST操作步骤
- 你应该知道的最好Webmail邮件客户端,
- javascript实现方法调用与方法触发小结
- 管理Java垃圾回收的五个建议
- C++中static用法总结
- 8款世界级Webmail工具推荐
- Android-开关按钮ToggleButton