使用Servlet实现图片下载

package chensi.com;

import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.file.FileAlreadyExistsException;

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

/**
 * Servlet HttpServletResponse 下载文件  ,浏览器提示我们将图片下载下来。
 */
@WebServlet("/ResponseDemo7")
public class ResponseDemo7 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 path = this.getServletContext().getRealPath("/WEB-INF/classes/爪哇.jpg");
        //有了路径可以得到输入流(图片,要用字节输出流)
        FileInputStream fis = new FileInputStream(path); //使用绝对路径将图片用输入流变成一个流  fis(图片)
        //用response创建一个输出流
        ServletOutputStream outputStream = response.getOutputStream();    
        
        /*我们要将图片下载下来 */
        //得到要下载的文件名
        String fileName = path.substring(path.lastIndexOf("\\")+1);
        System.out.println(fileName);
        //设置文件的编码
        fileName = URLEncoder.encode(fileName,"UTF-8");
        //告知客户端要下载文件
        response.setHeader("content-disposition", "attachment;filename="+fileName);
        response.setHeader("content-type", "image/jpeg");
        
        //执行fileOutputStream的输出操作
        int len = 1;
        byte[] b = new byte[1024];
        while((len=fis.read(b))!=-1){
            outputStream.write(b, 0, len);
        }
        outputStream.close();
        fis.close();
    }

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

}

 

posted @ 2016-06-27 10:40  张昊亮  阅读(2726)  评论(0编辑  收藏  举报