servlet實現圖片下載

需要的都在註釋裏,修改一下就可以用了:

首先是jsp頁面,這裏下載通過判斷id得到要下載的圖片id值,訪問servlet,Jsp頁面:


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c"  uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
    <title>Title</title>
</head>
<body>
<!--輸出所有的學生信息 -->
<a  href="/updownload2/add.jsp">添加學生信息</a>

<table align="center" border="1" width="70%">
    <tr>
        <th>編號</th>
        <th>姓名</th>
        <th>年齡</th>
        <th>分數</th>
    </tr>
    <c:forEach items="${requestScope.stuList2}" var="stu" >
        <tr>
            <td>${stu.id}</td>
            <td>${stu.name }</td>
            <td>${stu.age }</td>
            <td>${stu.score}</td>
            <td><img src="/updownload2/servlet/DownloadServlet?id=${stu.id}" width="100px"> </td>
            <td><a href="/updownload2/servlet/DownloadServlet?id=${stu.id}">下載</a></td>
        </tr>
    </c:forEach>
</table>


</body>
</html>

servlet頁面:

package com.bjsxt.servlet;

import com.bjsxt.entity.Student;
import com.bjsxt.service.StudentService;
import com.bjsxt.service.impl.StudentServiceImpl;
import org.apache.commons.io.IOUtils;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;

public class DownloadServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id=Integer.parseInt(request.getParameter("id"));
        StudentService studentService=new StudentServiceImpl();
        //查詢數據庫得到存儲圖片的格式和內容
        Student student=studentService.findById(id);
        //圖片真實名字
        String realName=student.getRealName();
        //圖片的唯一名 uuid生成的名字
        String uuidName=student.getUuidName();
        //什麼類型 image/jpeg
        String mimeType=student.getPhotoType();
        //圖片的路徑   圖片的名稱
        File file=new File("D:/upload",uuidName);
        //設置響應的格式
        //以圖片的形式開始響應
        response.setContentType(mimeType);
        //設置響應長度
        response.setContentLength((int)file.length());
        //設置下載的時候 對中文圖片的名字進行處理,保證下載的時候圖片的名字是原來的中文
        String userAgent = request.getHeader("User-Agent").toLowerCase();
        if(userAgent.indexOf("msie")>=0){ //msie
            realName = URLEncoder.encode(realName, "utf-8");
        }else{
            byte [] bytes = realName.getBytes("utf-8");
            realName = new String(bytes,"iso-8859-1");
        }
        //讓瀏覽器知道是下載的是什麼類型(內容處理-附件)
        response.setHeader("Content-disposition","attachment;filename="+realName);
        //創建輸入輸出流
        //先找到本地的文件
        InputStream is=new FileInputStream(file);
        //創建輸出流--往瀏覽器進行輸出
        OutputStream os=response.getOutputStream();
        //使用輸入流輸出流完成文件的下載 這裏使用了 IOUtils工具類
        IOUtils.copy(is,os);
        //關閉流
        is.close();
        os.close();

    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章