从JSP下载文件 [英] Download file from JSP

查看:60
本文介绍了从JSP下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JSP上编写了代码,该代码将显示远程托管服务器上的所有文件,当我单击该文件时,我应该能够下载它.什么是实现下载的最佳方法?

I wrote the code on JSP that will display all the files on Remote hosting server, when I click on the file, I should be able to download it. what the best way to implement download?

现在,该JSP可以向我显示该目录中的所有文件,例如->'C:/'

Right now, that JSP will able to show me all the files in that directory for example --> 'C:/'

感谢您的帮助

这是示例jsp页面

<%@ page import="java.io.*" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Report</title>
</head>
<body>
<h1>Files</h1>
<ul>

    <%
        String root = "c:/";
        java.io.File file;
        java.io.File dir = new java.io.File(root);

        String[] list = dir.list();



        if (list.length > 0) {

            for (int i = 0; i < list.length; i++) {
                file = new java.io.File(root + list[i]);

        if (file.isFile()) {
    %>
    <li><a href="/<%=file.getAbsolutePath()+file.getName()%>" target="_top"><%=list[i]%>
    </a><br>
            <%
    }
  }
}
%>
</ul>
</body>
</html>

推荐答案

我在这里发布给任何在此寻找答案的人.

I am posting here for anybody here who looking for answer on it.

在JSP文件上,使用此链接

On JSP file, use this for link

<<li><a href="/reportFetch?filePath=<%=file.getAbsolutePath()%>&fileName=<%=file.getName()%>" target="_top"><%=list[i]%></a><br>

并创建servlet

and create the servlet

...

import javax.activation.MimetypesFileTypeMap;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;

import java.io.*;

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

@WebServlet("/reportFetch")
public class Report extends HttpServlet {

    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String filePath = request.getParameter("filePath");
        String fileName = request.getParameter("fileName");

        MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
        String mimeType = mimeTypesMap.getContentType(request.getParameter("fileName"));

        response.setContentType(mimeType);
        response.setHeader("Content-disposition", "attachment; filename=" + fileName);

        OutputStream out = response.getOutputStream();
        FileInputStream in = new FileInputStream(filePath);
        byte[] buffer = new byte[4096];
        int length;
        while ((length = in.read(buffer)) > 0) {
            out.write(buffer, 0, length);
        }
        in.close();
        out.flush();

    }


    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }
}

这篇关于从JSP下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆