关闭

文件下载jspServlet

标签: jsp 下载文件下载乱码
360人阅读 评论(0) 收藏 举报
分类:

下载
承接以前的上傳
列出文件:

public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  String real=getServletContext().getRealPath("/WEB-INF/Files");
  Map<String,String> map=new HashMap<String, String>();
  File file=new File(real);
  listfiles(file,map);
  request.setAttribute("map", map);
  request.getRequestDispatcher("/ListFiles.jsp").forward(request, response);
 }

 private void listfiles(File file, Map<String, String> map) {
  // TODO Auto-generated method stub
  if(file.isFile()){
   String uuidn=file.getName();
   String oldname=uuidn.substring(uuidn.indexOf("_")+1);
   map.put(uuidn, oldname);
  }
  else{
   File[] fis=file.listFiles();
   for(File f:fis){
    listfiles(f, map);
   }
  }
 }


 

jsp:

    <h1>本站资源</h1>
    <c:forEach items="${map}" var="me">
    <c:url value="/servlet/Download" var="url">
     <c:param name="filename" value="${me.key}"></c:param>
    </c:url>
    <a href="${url }"> ${me.value }<br /></a>
    </c:forEach>


 

处理下载:

 

public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  String real=getServletContext().getRealPath("/WEB-INF/Files");
  Map<String,String> map=new HashMap<String, String>();
  File file=new File(real);
  listfiles(file,map);
  request.setAttribute("map", map);
  request.getRequestDispatcher("/ListFiles.jsp").forward(request, response);
 }

 private void listfiles(File file, Map<String, String> map) {
  // TODO Auto-generated method stub
  if(file.isFile()){
   String uuidn=file.getName();
   String oldname=uuidn.substring(uuidn.indexOf("_")+1);
   map.put(uuidn, oldname);
  }
  else{
   File[] fis=file.listFiles();
   for(File f:fis){
    listfiles(f, map);
   }
  }
 }


 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  response.setContentType("text/html;charset=UTF-8");
  OutputStream out = response.getOutputStream();
  
  String filename = request.getParameter("filename");// get请求方式
  filename = new String(filename.getBytes("ISO-8859-1"),"UTF-8");// 中文编码
  
  // 截取老文件名
  String oldFileName = filename.substring(filename.indexOf("_")+1);
  // 得到存储路径
  String storePath = getServletContext().getRealPath("/WEB-INF/files");
  // 得到文件的全部路径
  String filePath = makeStorePath(storePath, filename)+"\\"+filename;
  
  // 判断文件是否存在
  File file = new File(filePath);
  if(!file.exists()){
   out.write("对比起!你要下载的文件可能已经不存在了".getBytes("UTF-8"));
   return;
  }
  
  InputStream in = new FileInputStream(file);
  // 通知客户端以下载的方式打开
  response.setHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode(oldFileName, "UTF-8"));
  
  byte[] b = new byte[1024];
  int len = -1;
  while((len=in.read(b))!=-1){
   out.write(b, 0, len);
  }
  in.close();
  out.write("下载成功".getBytes("UTF-8"));
 }
 private String makeStorePath(String storePath, String fileName) {
  int hashCode = fileName.hashCode();
  int dir1 = hashCode & 0xf;// 0000~1111:整数0~15共16个
  int dir2 = (hashCode & 0xf0) >> 4;// 0000~1111:整数0~15共16个

  String path = storePath + "\\" + dir1 + "\\" + dir2; // WEB-INF/files/1/12
  File file = new File(path);
  if (!file.exists())
   file.mkdirs();

  return path;
 }


处理Get方式的异常:

filename = new String(filename.getBytes("ISO-8859-1"),"UTF-8");// 中文编码
处理Post方式的乱码:

 request.setCharacterEncoding("utf-8")

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:25337次
    • 积分:676
    • 等级:
    • 排名:千里之外
    • 原创:22篇
    • 转载:11篇
    • 译文:20篇
    • 评论:0条
    文章分类