怎么没有人理我啊!麻烦大家帮帮忙啊!很急!

解决方案 »

  1.   

    path应该是E:\\ebook,另外我觉得在jsp中写代码是个很不好的行为,会使你的代码难于维护,并且可能会引起并发访问方面的问题,写到servlet中是比较好的方法,只需要你在jsp中向servlet传递一个下载文件的id就可以了,这里有一些代码你看看吧
    private static final String CONTENT_TYPE = "text/html; charset=GBK";   //Initialize global variables
       public void init() throws ServletException {
       }
       //Process the HTTP Get request
       public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //HttpServletRequestWrapper httpReqWrapper = (HttpServletRequestWrapper) request;
    //HttpServletRequest httpRequest =(HttpServletRequest)httpReqWrapper.getRequest();
    try {
           downloadArtifact(request, response);
         }catch (Exception x) {
           x.printStackTrace();
           RequestDispatcher dispatcher = this.getServletContext().
               getRequestDispatcher("/common/ShowError.jsp");
          
           dispatcher.forward(request, response);
           }
        }
        
       public void downloadArtifact(HttpServletRequest request, HttpServletResponse response) throws Exception {
         Exception ex=null;
         String fileId = (String)request.getParameter("ID");
         String realFile = null;
         File uploadDir = new File("D:\\upload");
         if (uploadDir.isDirectory()) {
         File[] files = uploadDir.listFiles();
         for (int j = 0; j < files.length; j++) {
         File tempFile = files[j];
         if ((tempFile.getName().indexOf(fileId)) >= 0){
         System.err.println("get The file" + tempFile.getName());
         realFile = tempFile.getName();
         break;
         }
         }
         }
        
    File file = new File("D:\\upload", realFile);
    if(!file.exists()) throw new Exception("Sorry, File Not Found");
         int length=(new Long(file.length())).intValue();
         response.setContentType("application/octet-stream; charset=UTF-8");
         response.setHeader("Content-disposition", "attachment; filename=\""+file.getName()+"\"");
        
         int bufferSize=1024;
    BufferedOutputStream output = null;
         BufferedInputStream input = null;
         output = new BufferedOutputStream(response.getOutputStream());
         input = new BufferedInputStream(new FileInputStream(file));
         try {
           int once = 0;
           int total = 0;
          byte[] buffer = new byte[bufferSize];
           do {
             once = input.read(buffer);
             total += once;
             if (once >= 0)
               output.write(buffer, 0, bufferSize);
           }
           while ( (total < length) && (once >= 0));
           response.flushBuffer();
         }
         catch (Exception e) {
           ex = e;
         } // maybe user cancelled download
         finally {
           if (input != null) input.close();
           if(output!=null) output.close();       if(null!=ex) throw ex;
         }
        }   //Process the HTTP Post request
       public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         doGet(request, response);
       }