• 用java写一个servlet,可以将放在tomcat项目根目录下的文件进行下载


        用java写一个servlet,可以将放在tomcat项目根目录下的文件进行下载,将一个完整的项目进行展示,主要有以下几个部分:

      1、servlet部分   Export 

      2、工具类:TxtFileUtil

      3、web.xml

      

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.URLEncoder;

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

    import com.test.util.TxtFileUtil;

    /**
    * 导出文件
    * @author ouyangyu
    * 如何下载tomcat的项目工程下面的文件
    *下载地址:http://localhost:8080/ytxtest/ex.hts?filepath=ss.rar
    *http://localhost:8080/ytxtest/ex.hts?filepath=fj.docx
    */
    public class Export extends HttpServlet
    {
    private static final long serialVersionUID = 342590465839109906L;

    private String contentType = "application/x-msdownload";

    private String enc = "utf-8";

    private String fileRoot = "";
    /**
    * 初使化方法
    */
    public void init(ServletConfig config) throws ServletException
    {
    String tempStr = config.getInitParameter("contentType");
    if (tempStr != null && !tempStr.equals(""))
    {
    contentType = tempStr;
    }
    tempStr = config.getInitParameter("enc");
    if (tempStr != null && !tempStr.equals(""))
    {
    enc = tempStr;
    }
    tempStr = config.getInitParameter("fileRoot");
    if (tempStr != null && !tempStr.equals(""))
    {
    fileRoot = tempStr;
    }
    }
    /**
    * doget请求
    */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    {
    //realUrl = /D:/ruanjian/tomcat/apache-tomcat-6.0.37/webapps/ytxtest/
    String realUrl = new TxtFileUtil().getWebRoot();

    //String path = realUrl+"/";
    //path += StaticValue.FILE_UPLOAD_PATH;
    //filepath= ss.rar
    String filepath = request.getParameter("filepath");
    String fullFilePath = realUrl + filepath;
    //fullFilePath = /D:/ruanjian/tomcat/apache-tomcat-6.0.37/webapps/ytxtest/ss.rar
    //file = D: uanjian omcatapache-tomcat-6.0.37webappsytxtestss.rar
    File file = new File(fullFilePath);
    if (file.exists())
    {
    //filename = ss.rar
    String filename = URLEncoder.encode(file.getName(), enc);
    response.reset();
    response.setContentType(contentType);
    response.addHeader("Content-Disposition", "attachment; filename=""
    + filename + """);
    //fileLength = 54325070
    int fileLength = (int) file.length();
    response.setContentLength(fileLength);
    if (fileLength != 0)
    {
    InputStream inStream = new FileInputStream(file);
    byte[] buf = new byte[4096];
    ServletOutputStream servletOS = response.getOutputStream();
    int readLength;
    while (((readLength = inStream.read(buf)) != -1))
    {
    servletOS.write(buf, 0, readLength);
    }
    inStream.close();
    servletOS.flush();
    servletOS.close();
    }
    }

    }
    /**
    * dopost请求
    */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    {
    doGet(request, response);
    }
    }

    /**
    * 读写文件的工具类
    *
    * @author Administrator
    */
    public class TxtFileUtil
    {
    /**
    * 获取路径
    *
    * @return
    */
    public String getWebRoot()
    {
    String realUrl = getClass().getProtectionDomain().getCodeSource().getLocation().getPath();

    String newUrl = "";

    if(realUrl.contains("/WEB-INF/"))
    {
    newUrl = realUrl.substring(0, realUrl.lastIndexOf("WEB-INF/"));
    }

    realUrl = newUrl.replace("%20", " ");// 此路径不兼容jboss

    return realUrl;
    }
    }

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <servlet>
    <description>导出文件</description>
    <display-name>导出文件</display-name>
    <servlet-name>Export</servlet-name>
    <servlet-class>com.test.servlet.Export</servlet-class>
    </servlet>


    <servlet-mapping>
    <servlet-name>Export</servlet-name>
    <url-pattern>/ex.hts</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    </web-app>

  • 相关阅读:
    方法重载的小demo
    面向对象的小demo
    直接选择排序
    冒泡排序
    杨辉三角用java实现
    从键盘输入成绩,找出最高分,并输出学生成绩等级。成绩>=最高分-10,为A,成绩>=最高分-20,为B,成绩>=最高分-30,为C,其余等级为D
    井号的含义
    svg snap 笔记
    jQuery 插件格式 规范
    工作遇到问题
  • 原文地址:https://www.cnblogs.com/ouyy/p/6823892.html
Copyright © 2020-2023  润新知