列出要下载的文件夹中的文件

时间:2016-08-04 09:02:15

标签: java jsp servlets

我正在尝试从文件夹列出要在jsp页面上下载的文件。我正在使用jsp-servlet组合。我非常需要这方面的帮助。请注意 - 我想坚持使用jsp-servlet组合

Servlet代码

package com.candidjava.servlet.download;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DownloadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    //String filename = "documents.txt";
    String filepath = "d:\\uploads\\";
            for(File f:new File(filepath).listFiles()){


    response.setContentType("APPLICATION/OCTET-STREAM");
    response.setHeader("Content-Disposition", "attachment; filename=\""
            + f + "\"");

    // use inline if you want to view the content in browser, helpful for
    // pdf file
    // response.setHeader("Content-Disposition","inline; filename=\"" +
    // filename + "\"");
    FileInputStream fileInputStream = new FileInputStream(filepath
            + f);

    int i;
    while ((i = fileInputStream.read()) != -1) {
        out.write(i);
    }
    fileInputStream.close();
    out.close();
}
    }
}

JSP页面

<%-- 
Document   : index
Created on : 28 Jul, 2016, 3:50:33 PM
Author     : Admin
--%>

  <%@page contentType="text/html" pageEncoding="UTF-8"%>
 <!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
 Click on the link to download:
  <a href="DownloadServlet">Download a File</a>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

我能够自己想出一个解决方案 - 粘贴下面的代码,以防任何人想要实现同样的目的。非常感谢你!!!

JSP代码

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.cts.dao.CaseDocumentDAO;
import com.cts.model.CaseDocument;
import com.cts.model.CaseDetail;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpSession;

public class CaseDocumentController extends HttpServlet {

private static final long serialVersionUID = 1L;
private static String INSERT_OR_EDIT = "/ctsCaseDocuments.jsp";
private static String LIST_CASEDETAIL = "/ctsCaseDocuments.jsp";
private static String LIST_CASEDDOCUMENTS = "/ctsDownloadDocuments.jsp";
private CaseDocumentDAO dao;

private final String UPLOAD_DIRECTORY = "d:/uploads";

public CaseDocumentController() {
    super();
    dao = new CaseDocumentDAO();

}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String forward = "";
    String action = request.getParameter("action");

    if (action.equalsIgnoreCase("download")) {

        response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    String filename = request.getParameter("FileName");;
    String filepath = "d:\\uploads\\";



    response.setContentType("APPLICATION/OCTET-STREAM");
    response.setHeader("Content-Disposition", "attachment; filename=\""
            + filename + "\"");

    // use inline if you want to view the content in browser, helpful for
    // pdf file
    // response.setHeader("Content-Disposition","inline; filename=\"" +
    // filename + "\"");
    FileInputStream fileInputStream = new FileInputStream(filepath
            + filename);

    int i;
    while ((i = fileInputStream.read()) != -1) {
        out.write(i);
    }
    fileInputStream.close();
    out.close();
    }

    else if (action.equalsIgnoreCase("listDocument")) {
                forward = LIST_CASEDETAIL;
                request.setAttribute("casemasters", dao.getAllCaseMaster());
     }

     else
     {
                forward = LIST_CASEDDOCUMENTS;
                request.setAttribute("casemasters", dao.getAllCaseMaster());
                request.setAttribute("casedocuments", dao.getAllCaseDocuments());
     }

    RequestDispatcher view = request.getRequestDispatcher(forward);
    view.forward(request, response);
}


}

Servlet代码

{{1}}