Servlet 怎么支持迅雷的下载

写了一段servelt下载的代码,直接浏览器保存没有问题,如果是下载工具下载的话就会出错,而且下载的时候在迅雷里面显示的不是我代码里面指定的文件名,直接是servelt的名称;

servelt下载代码:
[code="java"]
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {

    String path = request.getParameter("path");
    try {
        String fileName = request.getParameter("fileName").trim();
        int c = fileName.lastIndexOf(".");
        String name = fileName.substring(0, c > 0 ? c : fileName.length())
                + "."
                + path.substring(path.lastIndexOf(".") + 1, path.length());
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachement;filename=\""
                + new String(name.getBytes("GBK"), "ISO8859_1") + "\"");
        File file = new File(Const.getCurrentUtterlyPath() + path);
        if (!file.exists()) {
            throw new IOException(fileName + ",所下载的文件不存在!");
        }
        response.setContentLength(Integer.valueOf(String.valueOf(file.length())));
        InputStream fs = new FileInputStream(file);
        OutputStream os = response.getOutputStream();
        byte[] buff = new byte[1024];
        int readCount = 0;
        readCount = fs.read(buff);
        while (readCount != -1) {
            os.write(buff, 0, readCount);
            readCount = fs.read(buff);
        }
        if (fs != null) {
            fs.close();
        }
        if (os != null) {
            os.flush();
            os.close();
        }
        response.setStatus(response.SC_OK);
        response.flushBuffer();
    } catch (IOException e) {
        LOG.error("error: " + e.getMessage() + ",path: " + path);
        throw e;
    }
}

[/code]

呼我,我把我写的源码传你!23568956

如果你的文件是放在webroot工程根目录下面的话,不如直接链接到你的那个文件,不过那就不是servlet下载了,呵呵,我试过,使用struts1写文件下载,到后来就是下载文件名.do的文件了,哥们儿,如果有解决办法希望能指点一二,我也正迷茫

[code="java"]
public static String getWebContent(String URL) {
String s = "";
System.err.println(URL);
try {
//从url打开stream
InputStream in = null;
HttpURLConnection conn = (HttpURLConnection) new URL(URL).openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(10000);
in = conn.getInputStream();
//尝试从http头中获取字符
String contentType = conn.getHeaderField("Content-Type");
String encoding = "utf-8";
boolean charsetFound = false;
if(contentType!=null){
contentType = contentType.toLowerCase();
if (contentType.contains("charset")) {
encoding = contentType.split("charset=")[1];
charsetFound = true;
}
}
byte[] buf = new byte[1024];
if (!charsetFound) {
int len = in.read(buf);
while (len <= 32) {
len = in.read(buf);
}
String header = new String(buf, 0, len);
Pattern p = Pattern.compile(".* Matcher m = p.matcher(header);
if (m.matches()) {
encoding = header.toLowerCase().split("charset=")[1].replaceAll("[^a-z|1-9|\\-]", " ").split("\\s+")[0];
} else {
//如果没有的话,直接用gb2312解码
encoding = "gb2312";
}
}
BufferedReader br = new BufferedReader(new InputStreamReader(in, encoding));
String header = new String(buf, encoding);
//add the header to our content
StringBuffer sb = new StringBuffer(header);
char[] charBuf = new char[2048];
int len = br.read(charBuf);
while (len != -1) {
sb.append(charBuf, 0, len);
len = br.read(charBuf);
}
br.close();
s = sb.toString();
}catch(ConnectException ex){
System.out.println(ex.getMessage()+" => "+URL);
}catch (IOException ex) {
Logger.getLogger(Utility.class.getName()).log(Level.SEVERE, null, ex);
System.out.println(URL);
}
return s;
}
[/code]
希望对你有帮助!