0
点赞
收藏
分享

微信扫一扫

【汇智学堂】-JSP无组件下载功能的实现


list.jsp

<%@ page import="java.util.List" %><%--
  Created by IntelliJ IDEA.
  User: soft
  Date: 2019/5/18
  Time: 16:06
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

<%  List<String> list=(List)request.getAttribute("list");%>
<table border="0" cellspacing="1" cellpadding="4" bgcolor="#f0ffff" width="100%">
    <tr align="center" bgcolor="#f0f8ff">
        <td>文件名称</td>
        <td>上传时间</td>
        <td>文件大小</td>
        <td>操    作</td>
    </tr>

    <%for(String str:list){%>

    <tr align="center">
        <%
        String param[]=str.split("#");
        %>

        <td align="center"><%=param[1]%></td>
        <td align="center"><%=param[2]%></td>
        <td align="center"><%=param[3]%></td>
        <td align="center"><a href="<%=param[0]%>" >下载</a></td>
        <%--<td align="center"><a href="download.jsp?path=<%=param[0]%>" >下载</a></td>--%>
    </tr>

    <%}%>

</table>

</body>
</html>

download.jsp

<%--
  Created by IntelliJ IDEA.
  User: soft
  Date: 2019/5/18
  Time: 16:06
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%@ page import="java.net.URLDecoder" %>
<%@ page import="java.io.*" %>

<html>
<head>
    <title>Title</title>
</head>
<body>

<%
request.setCharacterEncoding("gb2312");
response.setCharacterEncoding("gb2312");
response.setContentType("text/html;charset=gb2312");

String pathname=request.getParameter("path");
pathname=new String(pathname.getBytes("iso8859-1"),"gb2312");
File file=new File(pathname);

out.println(java.net.URLEncoder.encode(file.getName(),"iso8859-1"));
out.println(java.net.URLEncoder.encode(file.getName(),"gb2312"));
out.println(URLDecoder.decode(file.getName(),"utf-8"));

if(file.exists()==false){
    out.println("<p align='center'>文件不存在或已被删除,下载失败!!<p>");
}
else{
    InputStream ins=new FileInputStream(file);
    OutputStream os=response.getOutputStream();
    response.addHeader(
            "Content-Disposition",
            "attachment;filename="
              +java.net.URLEncoder.encode(file.getName(),
                    "gb2312"));

    response.addHeader("Content-Length",file.length()+"");
    response.setContentType("application/octet-stream");
    int data=0;
    while ((data=ins.read())!=-1){
        os.write(data);
    }
    out.clear();
    out=pageContext.pushBody();
    os.close();
    ins.close();
}

%>

</body>
</html>

GetFile类

package com.updownload.servlet;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.io.File;

public class GetFile {
    private static ArrayList<String> filelist=new ArrayList<String>();
    public static List<String> getPath(String filePath){
        List<String> list=new ArrayList<String>();
        File root=new File(filePath);
        File[] files=root.listFiles();
        for(File file:files){
            if(file.isDirectory()){
                getPath(file.getAbsolutePath());
            }
            else{
                SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
                String time=sdf.format(new Date(file.lastModified()));
                /*list.add(file.getAbsolutePath()+"#"+new File(file.getAbsolutePath()).getName()+"#"+time+"#"+(new
                        File(file.getAbsolutePath())).length()+"字节");*/
                list.add("http://localhost:8080/upload/"+new File(file.getAbsolutePath()).getName()+"#"+new File(file.getAbsolutePath()).getName()+"#"+time+"#"+(new
                        File(file.getAbsolutePath())).length()+"字节");
               }
        }
        return  list;
    }
}

Servlet类:ServletUpdownload

package com.updownload.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "ServletUpdownload")
public class ServletUpdownload extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setCharacterEncoding("gb2312");
        response.setContentType("text/html;charset=gb2312");
        request.setCharacterEncoding("gb2312");

       List<String> list=GetFile.getPath(request.getSession().getServletContext().getRealPath("/")+"upload/");
        //List<String> list=GetFile.getPath("http://localhost:8080/upload/");
        request.setAttribute("list",list);
        request.getRequestDispatcher("page/download/list.jsp").forward(request,response);
    }
}


举报

相关推荐

0 条评论