这个应该可以[8D]
DownLoad.java
==============================================================
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import tools.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public classdo
wnLoad extends HttpServlet {
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public voiddo
Get(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String filename = request.getParameter("filename"
;
String filePath = request.getParameter("filePath"
;
try{
if(filename != null){
String path = request.getRealPath("/"
+ filePath + filename;
byte[] b = new byte[500];
File fileLoad = new File(path);
FileInputStream in = new FileInputStream(fileLoad);
response.setContentType("application/x-macbinary"
;
filename = new String(OtherTools.deleteTail(filename).getBytes("gb2312"
,"ISO8859-1"
;
response.setHeader("Content-disposition","attachment;filename="+ filename);
response.setContentLength((int)fileLoad.length());
OutputStream o = response.getOutputStream();
int n=0;
while((n=in.read(b)) != -1){
o.write(b,0,n);
}
in.close();
}
}
catch(FileNotFoundException ex){
ex.printStackTrace();
}
}
//Process the HTTP Post request
public voiddo
Post(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
do
Get(request, response);
}
//Clean up resources
public void destroy() {
}
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();
}
}