使用uploadify实现功能上传

liloaches 贡献于2012-09-03

作者 Administrator  创建于2012-08-31 07:14:00   修改者SkyUN.Org  修改于2012-08-31 07:25:00字数5367

文档摘要:附件上传到服务器upload文件夹中,并保存到ucap_attr表中
关键词:

 附件上传到服务器upload文件夹中,并保存到ucap_attr表中 Ucap_attr.jsp上传页的代码: <%@ page language="java" pageEncoding="UTF-8"%> <%@ page import="com.linewell.core.system.GlobalParameter"%>
"/>
下载页面的代码: <%@ page contentType="text/html;charset=GBK"%> <%@ page import="com.linewell.was.ucapAttr.*"%> <%@ page import="java.io.File"%> <%@ page import="java.io.InputStream"%> <%@ page import="java.io.OutputStream"%> <%@ page import="java.io.FileInputStream"%> <%@ page import="java.io.BufferedOutputStream"%> <%@ page import="java.net.URLEncoder"%> <%@ page import="java.sql.Blob"%> <% try{ String unid = request.getParameter("unid"); UcapAttr ucapAttr = new UcapAttrManager().doFindBeanByKey(unid); ucapAttr = null == ucapAttr ? new UcapAttr() : ucapAttr; String filename = ucapAttr.getAttr_file_name(); InputStream is = null; Blob file_data = ucapAttr.getAttr_contents(); if(null != file_data && file_data.length() > 0){ is = ucapAttr.getAttr_contents().getBinaryStream(); }else{ String filepath = request.getRealPath("") + ucapAttr.getFile_path(); filepath = filepath.replaceAll("\\\\", "/"); // 反斜杠("\")属于转义符号,在传参之前必须先转化成正斜杠("/") File file = new File(filepath); if(null == file || !file.exists()){ StringBuffer script=new StringBuffer(); script.append(""); out.println(script.toString()); return; }else{ is = new FileInputStream(file); } } //目前文件实体没有保存到数据库中,而是直接保存到服务器上,所以这边可通过文件路径直接读取 response.addHeader("Content-Disposition","attachment; filename="+URLEncoder.encode(filename,"UTF8")); response.setContentType("application/msword"); response.setHeader("Pragma", "public"); response.setHeader("Cache-Control", "max-age=0"); int bytesRead; byte[] buffer = new byte[4096]; OutputStream bos = new BufferedOutputStream(response.getOutputStream()); while((bytesRead = is.read(buffer, 0, buffer.length))!=-1) { bos.write(buffer, 0, bytesRead); } response.flushBuffer(); bos.close(); is.close(); }catch(Exception e){ e.printStackTrace(); }finally{ // 后面两句必须加上去,否则tomcat在把jsp编译成servlet之后,会加上释放jsp引用对象的代码,会调用response.getWriter(), // 因为这个方法是和response.getOutputStream()相冲突的!所以会出现以上这个异常。 // add by lpeitu@linewell.com 2009-10-26 out.clear(); out = pageContext.pushBody(); } %>

下载文档到电脑,查找使用更方便

文档的实际排版效果,会与网站的显示效果略有不同!!

需要 5 金币 [ 分享文档获得金币 ] 0 人已下载

下载文档