文件下载示例

Posted 第九种格调的人生

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了文件下载示例相关的知识,希望对你有一定的参考价值。

1.获得文件路径,得到文件名

2.告诉浏览器要下载文件和下载文件的格式

3.response获得输出流,输出文件

 

response.getWriter()示例

package com.learning.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;

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

/**
 * Servlet implementation class DownLoadServlet
 */
@WebServlet("/servlet/downLoadServlet")
public class DownLoadServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //文件下载
        
        
        //要下载的文件名
        String filename="123.txt";
        //设置文件名的编码
        if(request.getHeader("user-agent").toLowerCase().contains("msie")){
            filename = URLEncoder.encode(filename, "UTF-8");//将不安全的文件名改为UTF-8格式
        }else{
            filename = new String(filename.getBytes("UTF-8"),"iso-8859-1");//火狐浏览器
        }
    
        //告诉浏览器要下载文件
        response.setHeader("content-disposition", "attachment;filename="+filename);
        //告诉浏览器要下载的文件格式
        response.setContentType(this.getServletContext().getMimeType(filename));
        
        //获得输出流
        PrintWriter out = response.getWriter();
        out.write("tom,123 \n");
        out.write("jerry,123 \n");
        out.write("john,123 \n");
    
    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

 

下载图片示例:

/**
 * 
 */
package com.learning.servlet;

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

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

/**
 * @author 杰
 *
 */
public class ServletDemo1 extends HttpServlet {

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

        //先获得文件的路径
        String realPath = this.getServletContext().getRealPath("/WEB-INF/classes/美女.jpg");
        //通过文件流来读取文件
        FileInputStream fileInputStream=new FileInputStream(new File(realPath));
        //获取文件名
        String filename=realPath.substring(realPath.lastIndexOf("/")+1);
        //设置文件名的编码
        if(request.getHeader("user-agent").toLowerCase().contains("msie")){
            filename = URLEncoder.encode(filename, "UTF-8");//将不安全的文件名改为UTF-8格式
        }else{
            filename = new String(filename.getBytes("UTF-8"),"iso-8859-1");//火狐浏览器
        }
        
        //告诉浏览器要下载文件
        response.setHeader("content-disposition","attachment;filename="+filename);
        //告诉浏览器文件的格式
        response.setContentType(this.getServletContext().getMimeType(filename));
        //输出文件
        ServletOutputStream outputStream = response.getOutputStream();
        
        int len=1;
        byte[] b=new byte[1024];
        while ((len=fileInputStream.read(b))!=-1) {    
            outputStream.write(b, 0, len);
        }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}

 

以上是关于文件下载示例的主要内容,如果未能解决你的问题,请参考以下文章

LockSupport.java 中的 FIFO 互斥代码片段

我在哪里更改此 Python 代码片段以将临时文件保存在 tmp 文件夹中?

html PHP代码片段: - AJAX基本示例:此代码演示了使用PHP和JavaScript实现的基本AJAX功能。

使用 Python 代码片段编写 LaTeX 文档

Python 向 Postman 请求代码片段

如何在片段中创建选项卡的示例?