<!--文件名:download.html作 者:纵横软件制作中心雨亦奇(zhsoft88@sohu.com)--><!doctype html public "-//w3c//dtd html 4.01 transitional//en"><html><head><title>下载</title><meta http-equiv="content-type" content="text/html; charset=gb2312"></head><body><a href="jsp(sun企业级应用的首选)/do_download.jsp(sun企业级应用的首选)">点击下载</a></body></html> |
<%@ page contenttype="text/html;charset=gb2312" import="com.jsp(sun企业级应用的首选)smart.upload.*" %><%// 新建一个smartupload对象smartupload su = new smartupload();// 初始化su.initialize(pagecontext);// 设定contentdisposition为null以禁止浏览器自动打开文件,//保证点击链接后是下载文件。若不设定,则下载的文件扩展名为//doc时,浏览器将自动用word打开它。扩展名为pdf时,//浏览器将用acrobat打开。su.setcontentdisposition(null);// 下载文件su.downloadfile("/upload/如何赚取我的第一桶金.doc");%> |
public void downloadfile(string s, string s1, string s2, int i)throws servletexception, ioexception, smartuploadexception {if(s == null) throw new illegalargumentexception("file " + s + " not found (1040).");if(s.equals("")) throw new illegalargumentexception("file " + s + " not found (1040).");if(!isvirtual(s) && m_denyphysicalpath) throw new securityexception("physical path is denied (1035).");if(isvirtual(s)) s = m_application.getrealpath(s);java.io.file file = new java.io.file(s);fileinputstream fileinputstream = new fileinputstream(file);long l = file.length();boolean flag = false;int k = 0;byte abyte0[] = new byte[i];if(s1 == null) m_response.setcontenttype("application/x-msdownload");elseif(s1.length() == 0) m_response.setcontenttype("application/x-msdownload");else m_response.setcontenttype(s1);m_response.setcontentlength((int)l);m_contentdisposition = m_contentdisposition != null ?m_contentdisposition : "attachment;";if(s2 == null) m_response.setheader("content-disposition", m_contentdisposition + " filename=" + toutf8string(getfilename(s)));elseif(s2.length() == 0) m_response.setheader("content-disposition", m_contentdisposition);else m_response.setheader("content-disposition", m_contentdisposition + " filename=" + toutf8string(s2));while((long)k < l){ int j = fileinputstream.read(abyte0, 0, i); k += j; m_response.getoutputstream().write(abyte0, 0, j);}fileinputstream.close(); } /** * 将文件名中的汉字转为utf8编码的串,以便下载时能正确显示另存的文件名. * 纵横软件制作中心雨亦奇2003.08.01 * @param s 原文件名 * @return 重新编码后的文件名 */ public static string toutf8string(string s) {stringbuffer sb = new stringbuffer();for (int i=0;i<s.length();i++) { char c = s.charat(i); if (c >= 0 && c <= 255) {sb.append(c); } else {byte[] b;try { b = character.tostring(c).getbytes("utf-8");} catch (exception ex) { system.out.println(ex); b = new byte[0];}for (int j = 0; j < b.length; j++) { int k = b[j]; if (k < 0) k += 256; sb.append("%" + integer.tohexstring(k). touppercase());} }}return sb.tostring(); } |
如对本文有疑问, 点击进行留言回复!!
jspSmartUpload上传下载全攻略 (四、文件下载篇 )
网友评论