Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.controller;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.URLEncoder;
- import java.sql.SQLException;
- import javax.servlet.RequestDispatcher;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import com.dao.IPictureTableDao;
- import com.dao.impl.PictureTableMSSQLDao;
- import com.model.PictureTable;
- @WebServlet("/GetPictureFile")
- public class GetPictureFile extends HttpServlet {
- private static final long serialVersionUID = 1L;
- public GetPictureFile() {
- super();
- }
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- // response.getWriter().append("Served at: ").append(request.getContextPath());
- String indexStr = "";
- int index = 0;
- indexStr = request.getParameter("id");
- // System.out.println("indexStr = " + indexStr);
- if (indexStr == null) {
- // 如果沒有 query string 就結束這段程式。
- System.out.println("無 query string ,抓取圖片id失敗。");
- RequestDispatcher requestDispatcher = request.getRequestDispatcher("viewAllPicture.jsp");
- requestDispatcher.forward(request, response);
- return;
- }
- try {
- index = Integer.parseInt(indexStr);
- } catch (NumberFormatException e) {
- e.printStackTrace();
- // 如果不是合法數字就結束這段程式。
- System.out.println("圖片id非合法數字。");
- RequestDispatcher requestDispatcher = request.getRequestDispatcher("viewAllPicture.jsp");
- requestDispatcher.forward(request, response);
- return;
- }
- IPictureTableDao pictureDao = new PictureTableMSSQLDao();
- PictureTable pictureTable = pictureDao.getPictureWithBlobById(index);
- if (pictureTable == null) {
- // 查無資料,結束程式。
- System.out.println("查無資料,結束程式。");
- RequestDispatcher requestDispatcher = request.getRequestDispatcher("viewAllPicture.jsp");
- requestDispatcher.forward(request, response);
- return;
- }
- String pictureName = pictureTable.getPictureName();
- InputStream inputStream = null;
- long fileLength = 0L;
- try {
- inputStream = pictureTable.getFile2().getBinaryStream();
- fileLength = pictureTable.getFile2().length();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- OutputStream outputStream = null;
- // String mimeType = this.getServletContext().getMimeType(pictureName);
- // System.out.println("mimeType = " + mimeType);
- // 原文網址:https://kknews.cc/code/n5xn4yq.html
- String userAgent = request.getHeader("User-agent");
- userAgent = userAgent.toLowerCase();
- System.out.println("userAgent");
- System.out.println(userAgent);
- boolean isInternetExplorer = false;
- if (userAgent.indexOf("trident") != -1 || userAgent.indexOf("wow64") != -1 || userAgent.indexOf("edge") != -1
- || userAgent.indexOf("edg") != -1) {
- System.out.println("Yes. It's Internet Explorer Browser. It's Microsoft Edge Browser");
- isInternetExplorer = true;
- } else {
- isInternetExplorer = false;
- }
- if (false == isInternetExplorer) {
- // Google, Firefox, Opera瀏覽器
- pictureName = new String(pictureName.getBytes(), "ISO8859-1");
- } else {
- // Internet Explorer, Microsoft Edge瀏覽器
- pictureName = URLEncoder.encode(pictureName, "UTF-8");
- pictureName = pictureName.replace("+", "%20");
- }
- // 清空response
- response.reset();
- // 設定response的Header
- /* FireFox瀏覽器 必須在 fileName前後加上 \" 才能避免瀏覽器擷取到空格就停止擷取檔案名稱。 */
- response.addHeader("Content-Disposition", "attachment;filename=\"" + pictureName + "\"");
- // 這行程式碼設定檔案的名稱,為了避免中文字變亂碼,必須做額外處理。
- response.addHeader("Content-Length", String.valueOf(fileLength));
- response.setContentType("application/octet-stream");
- outputStream = response.getOutputStream();
- byte[] byteBuffer = new byte[(int) fileLength];
- int length = 0;
- try {
- while ((length = inputStream.read(byteBuffer)) != -1) {
- outputStream.write(byteBuffer, 0, length);
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (outputStream != null) {
- outputStream.close();
- outputStream = null;
- }
- if (inputStream != null) {
- inputStream.close();
- inputStream = null;
- }
- }
- }// end of doGet() method
- }
Add Comment
Please, Sign In to add comment