成语大全网 - 汉语词典 - java 代码实现.doc文件

java 代码实现.doc文件

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

<%@ page import="java.io.*"%>

<%!

public 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();

}

%>

<%

String filename=new String(request.getParameter("filename").getBytes("ISO8859-1"),"GBK");

String dirName="D:/我.doc";

java.io.File ff=null;

String dd=dirName+System.getProperties().getProperty("file.separator")+filename;

try{

ff=new java.io.File(dd);

}

catch(Exception e){

System.out.println(e.getMessage());

e.printStackTrace();

}

if (ff!=null&&ff.exists()&&ff.isFile())

{

long filelength = ff.length();

InputStream inStream=new FileInputStream(dd);

//设置输出的格式

response.reset();

response.setContentType("application/x-msdownload");

response.setContentLength((int)filelength);

response.addHeader("Content-Disposition","attachment; filename=\"" + toUtf8String(filename) + "\"");

//循环取出流中的数据

byte[] b = new byte[100];

int len;

while((len=inStream.read(b)) >0)

response.getOutputStream().write(b,0,len);

inStream.close();

out.clear();

out = pageContext.pushBody();

}

%>

:<a href="d.jsp">aa</a>

上面的那个是用流写的 但是也可以用超链接下载

你写上文件的路径就可以了