package com.servlet; import java.io.FileInputStream; import java.io.IOException; import java.io.OutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.common.BeanHelper; @SuppressWarnings("serial") public class DownloadServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) { String fullPath; String localPath = request.getParameter("path"); String basePath = BeanHelper.getConfService().getScatter().getBasePath(); //private.xml中读取文件存放路径 fullPath = basePath + localPath; //图片的全路径 String fileName = localPath.substring(localPath.lastIndexOf("/") + 1); FileInputStream fis = null; OutputStream toClient = null; try { fis = new FileInputStream(fullPath); int i = fis.available(); // 得到文件大小 byte data[] = new byte[i]; fis.read(data); // 读数据 fis.close(); response.setContentType("multipart/form-data"); // 设置返回的文件类型 response.setHeader("Content-Disposition", "attachment;fileName=" + fileName); // 设置文件头 response.setContentLength(i); toClient = response.getOutputStream(); // 得到向客户端输出二进制数据的对象 toClient.write(data); // 输出数据 toClient.close(); }catch(Exception e) { e.printStackTrace(); }finally { //try中途发生异常后关闭所有IO流 closeAllIO(toClient, fis); } } //关闭所有IO流 private void closeAllIO(OutputStream toClient,FileInputStream fis) { try { if(toClient != null) toClient.close(); if(fis != null) fis.close(); }catch(IOException e) { //iLog.error("IO关闭失败:"+e); } } } 下载文件的servlet,用浏览器直接下载没有问题,用迅雷下载存在两个问题: |
|
40分 |
你用迅雷精简版、迅雷7都试过吗?
|
我用的迅雷极速版,版本也会有关系吗? |