CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
英特尔®游戏设计大赛100美元现金周周送 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Java >  Web 开发

jsp+servlet 客户端文件保存/下载问题!

楼主zxp_net(好心情)2003-09-03 22:46:59 在 Java / Web 开发 提问

一般情况下客户端文件下载大致形式是:  
  <a   href="a.gif">图片</a>  
  <a   href="b.wav">媒体文件</a>  
  <a   href="c.mp3">声音文件</a>  
  这种连接有的单击后出现下载对话框,但有的则直接播放。  
  现在要求无论那种格式文件,都出现下载对话框,请问如何解决?如果多种文件格式不行,则现在至少要实现tif和wav格式文件。向各位讨教!!  
  我现在有个办法,但不知道是否可行:  
  单及连接后,将文件路径名称传人servlet(extend   HttpServlet),在doGet方法中用流写出来,不知道如何实现!! 问题点数:100、回复次数:6Top

1 楼zxp_net(好心情)回复于 2003-09-03 22:49:11 得分 0

因为考虑可操作性,不能使用右键单击另存为的方式下载。最好通过servlet或action方式实现。Top

2 楼leshui(大象无形)(有物混成,先天地生)回复于 2003-09-04 11:43:13 得分 0

写一个下载的程序就可以了  
  http://www.csdn.net/develop/read_article.asp?id=19064Top

3 楼ticlej(ticlej)回复于 2003-09-04 12:22:41 得分 0

用servlet实现最好,除了对资源可控之外,也能更好处理客户信息Top

4 楼zxp_net(好心情)回复于 2003-09-04 15:13:32 得分 0

用servlet可以实现,但现在不能下载中文文件,当下载中文文件时,出现  
  java.io.FileNotFoundException:   ..\file_share\%B9%D8%B1%D5%CA%B1%D7%E0%BB%C3%CF%EB%BF%............错误信息。请帮忙看看!!谢谢  
  得到文件路径和文件名称后,通过system.out.println在servlet中打印中文字符正常,  
  之后  
  response.setContentType("application/binary;   charset=ISO8859_1");  
  String   fileName   =   request.getAttribute("fileName").toString();  
  fileName   =   new   String(fileName.getBytes("ISO8859_1"),"GB2312");    
  System.out.println("new   file   =   "   +   fileName);//正常  
  fileName   =   URLEncoder.encode(fileName);  
  但  
  File   file   =   new   File("..//file_share//"   +   fileName);  
  new   BufferedInputStream(new   FileInputStream(file));   //但在这里就报上面的异常Top

5 楼swallowsky(子痕)回复于 2003-09-04 15:31:39 得分 0

File   file   =   new   File("..//file_share//"   +   fileName);  
  new   BufferedInputStream(new   FileInputStream(file));   //但在这里就报上面的异常  
   
  路径错了,斜杠应该反过来写。如果在windows系统中Top

6 楼shipp(嘎子)回复于 2003-09-15 15:39:43 得分 100

用servlet:  
   
   
  import   javax.servlet.*;  
  import   javax.servlet.http.*;  
  import   java.io.*;  
  import   java.util.*;  
  import   java.net.URLEncoder;  
  import   java.net.MalformedURLException;  
   
  public   class   DownloadServlet   extends   HttpServlet  
  {  
          private   static   final   String   CONTENT_TYPE   =   "text/html;   charset=GBK";  
          //这里填入服务器的url  
          private   String   serverUrl   =   "";          
          //Initialize   global   variables  
          public   void   init()   throws   ServletException  
          {  
          }  
   
          //Process   the   HTTP   Get   request  
          public   void   doGet(HttpServletRequest   request,  
                                              HttpServletResponse   response)   throws   ServletException,   IOException  
          {  
                  /////////////////////////////////////////////  
                  if(request.getAttribute("checkMimeType")   ==   null   ||   !"true".equals(request.getAttribute("checkMimeType").toString()))  
                  {  
                          java.util.Enumeration   enum   =   request.getHeaderNames();  
                          //如果不是从网站过以的连接,不允许下载  
                          if(request.getHeader("referer")   ==   null   ||  
                                (   (String)   request.getHeader("referer")).trim().indexOf(serverUrl)   <   0)  
                          {  
                                  response.setContentType("text/html;charset=GB2312");  
                                  PrintWriter   out   =   response.getWriter();  
                                  out.println("<html>");  
                                  out.println("<head>");  
                                  out.println("<title></title>");  
                                  out.println("<meta   http-equiv=\"content-type\"   content=\"text/html;   charset=GB2312\">");  
                                  out.println("</head>");  
                                  out.println("<body>");  
                                  out.println("您无权下载该文件!");  
                                  out.println("</body>");  
                                  out.println("</html>");  
                                  return;  
                          }  
                  }  
   
                  String   fileName   =   request.getAttribute("fileName").toString();  
  //                 fileName   =   URLEncoder.encode(fileName);  
                  ServletOutputStream   out   =   response.getOutputStream();  
                  //如果需要根据文件指定MimeType  
                  if(request.getAttribute("checkMimeType")   !=   null   &&   "true".equals(request.getAttribute("checkMimeType").toString()))  
                  {  
                          if(fileName.toLowerCase().indexOf(".jpg")   >   0   ||   fileName.toLowerCase().indexOf(".jpeg")   >   0)  
                          {  
                                  response.setContentType("image/jpeg;   charset=ISO8859_1");  
                          }  
                          else   if(fileName.toLowerCase().indexOf(".gif")   >   0)  
                          {  
                                  response.setContentType("image/gif;   charset=ISO8859_1");  
                          }  
                          else   if(fileName.toLowerCase().indexOf(".bmp")   >   0)  
                          {  
                                  response.setContentType("image/bmp;   charset=ISO8859_1");  
                          }  
                          else   if(fileName.toLowerCase().indexOf(".ico")   >   0)  
                          {  
                                  response.setContentType("image/x-icon;   charset=ISO8859_1");  
                          }  
                          else   if(fileName.toLowerCase().indexOf(".png")   >   0)  
                          {  
                                  response.setContentType("image/png;   charset=ISO8859_1");  
                          }  
                          else  
                          {  
                                  response.setContentType("application/binary;   charset=ISO8859_1");  
                          }  
                  }  
                  else  
                  {  
                          response.setContentType("application/binary;   charset=ISO8859_1");  
                  }  
                  fileName   =   new   String(fileName.getBytes("GB2312"),"ISO8859_1");   //add   by   dl  
                  System.out.println("IE   "   +   fileName);  
                  response.setHeader("Content-disposition",   "attachment;   filename="   +   fileName);  
   
                  BufferedInputStream   bis   =   null;  
                  BufferedOutputStream   bos   =   null;  
                  String   fileUrl   =   request.getAttribute("fileUrl").toString();  
                  System.out.println("--------debug:   "   +   fileUrl);  
                  try  
                  {  
                          File   file   =   new   File(fileUrl);  
                          bis   =   new   BufferedInputStream(new   FileInputStream(file));  
                          bos   =   new   BufferedOutputStream(out);  
   
                          byte[]   buff   =   new   byte[2048];  
                          int   bytesRead;  
                          while(   -1   !=   (bytesRead   =   bis.read(buff,   0,   buff.length)))  
                          {  
                                  bos.write(buff,   0,   bytesRead);  
                          }  
                          bos.flush();  
                          bos.close();  
                          bis.close();  
                          out.close();  
                          out   =   null;  
                          bis   =   null;  
                          bos   =   null;  
                          buff   =   null;  
                  }  
                  catch(MalformedURLException   e)  
                  {  
                          System.out.println("下载文件时发生了MalformedURLExceptin错误"   +   e.toString());  
                          throw   e;  
                  }  
                  catch(IOException   e)  
                  {  
                          System.out.println("下载文件时发生了IOException错误"   +   e.toString());  
                          throw   e;  
                  }  
                  catch(Exception   e)  
                  {  
                          System.out.println("下载文件时发生错误"   +   e.toString());  
                  }  
                  finally  
                  {  
                          if(bis   !=   null)  
                          {  
                                  bis.close();  
                          }  
                          if(bos   !=   null)  
                          {  
                                  bos.close();  
                          }  
                  }  
   
          }  
   
          //Process   the   HTTP   Post   request  
          public   void   doPost(HttpServletRequest   request,  
                                                HttpServletResponse   response)   throws   ServletException,   IOException  
          {  
                  doGet(request,   response);  
          }  
   
          //Clean   up   resources  
          public   void   destroy()  
          {  
          }  
  }  
   
   
  Top

相关问题

  • 请问如何找到TOMCAT保存的JSP翻译成SERVLET之后的源文件或CLASS文件?
  • 求助,jb8不能保存jsp文件
  • JBuilder 8 不能保存JSP文件!
  • Eclipse不能保存XML和JSP文件
  • 怎样把编好的文件保存成JSP文件呢?
  • 保存文件
  • 保存文件
  • jsp中怎么弹出文件保存对话框?
  • jsp页面,怎么出现保存文件对话框
  • 保存txt文件

关键词

  • .net
  • 文件
  • 下载
  • println

得分解答快速导航

  • 帖主:zxp_net
  • shipp

相关链接

  • CSDN Java频道
  • Java类图书
  • Java类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo