2023年11月28日发(作者:)jsp下载全攻略



1、下载链接页面



页面源码如下:











下载







点击下载

>







2、下载处理页面do_





源码如下:



<%@ page contentType="text/html;charset=gb2312"

import=".*" %><%

// 新建一个SmartUpload对象

SmartUpload su = new SmartUpload();

// 初始化

lize(pageContext);

// 设定contentDisposition为null以禁止浏览器自动打开文件,

//保证点击链接后是下载文件。若不设定,则下载的文件扩展名为

//doc时,浏览器将自动用word打开它。扩展名为pdf时,

//浏览器将用acrobat打开。

tentDisposition(null);

// 下载文件

adFile("/upload/如何赚取我的第一桶金.doc");

%>





3、如何下载中文文件





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((""))

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_lPath(s);

file = new (s);

FileInputStream fileinputstream = new FileInputStream(file);

long l = ();

boolean flag = false;

int k = 0;

byte abyte0[] = new byte[i];

if(s1 == null)

m_tentType("application/x-msdownload");

else

if(() == 0)

m_tentType("application/x-msdownload");

else

m_tentType(s1);

m_tentLength((int)l);

m_contentDisposition = m_contentDisposition != null ?

m_contentDisposition : "attachment;";

if(s2 == null)

m_der("Content-Disposition",

m_contentDisposition + " filename=" +

toUtf8String(getFileName(s)));

else

if(() == 0)

m_der("Content-Disposition",

m_contentDisposition);

else

m_der("Content-Disposition",

m_contentDisposition + " filename=" + toUtf8String(s2));

while((long)k < l)

{

int j = (abyte0, 0, i);

k += j;

m_putStream().write(abyte0, 0, j);

}

();

}





public static String toUtf8String(String s) {

StringBuffer sb = new StringBuffer();

for (int i=0;i<();i++) {

char c = (i);

if (c >= 0 && c <= 255) {

(c);

} else {

byte[] b;

try {

b = ng(c).getBytes("utf-8");

} catch (Exception ex) {

n(ex);

b = new byte[0];

}

for

(int j = 0; j < ; j++) {

int k = b[j];

if (k < 0) k += 256;

("%" + tring(k).

toUpperCase());

}

}

}

return ng();

}