KuoHsiangYu

GetPictureFile

May 3rd, 2020
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.54 KB | None | 0 0
  1. package com.controller;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.OutputStream;
  6. import java.net.URLEncoder;
  7. import java.sql.SQLException;
  8.  
  9. import javax.servlet.RequestDispatcher;
  10. import javax.servlet.ServletException;
  11. import javax.servlet.annotation.WebServlet;
  12. import javax.servlet.http.HttpServlet;
  13. import javax.servlet.http.HttpServletRequest;
  14. import javax.servlet.http.HttpServletResponse;
  15.  
  16. import com.dao.IPictureTableDao;
  17. import com.dao.impl.PictureTableMSSQLDao;
  18. import com.model.PictureTable;
  19.  
  20. @WebServlet("/GetPictureFile")
  21. public class GetPictureFile extends HttpServlet {
  22.     private static final long serialVersionUID = 1L;
  23.  
  24.     public GetPictureFile() {
  25.         super();
  26.     }
  27.  
  28.     @Override
  29.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  30.             throws ServletException, IOException {
  31.         // response.getWriter().append("Served at: ").append(request.getContextPath());
  32.  
  33.         String indexStr = "";
  34.         int index = 0;
  35.         indexStr = request.getParameter("id");
  36.  
  37.         // System.out.println("indexStr = " + indexStr);
  38.  
  39.         if (indexStr == null) {
  40.             // 如果沒有 query string 就結束這段程式。
  41.             System.out.println("無 query string ,抓取圖片id失敗。");
  42.             RequestDispatcher requestDispatcher = request.getRequestDispatcher("viewAllPicture.jsp");
  43.             requestDispatcher.forward(request, response);
  44.             return;
  45.         }
  46.  
  47.         try {
  48.             index = Integer.parseInt(indexStr);
  49.         } catch (NumberFormatException e) {
  50.             e.printStackTrace();
  51.             // 如果不是合法數字就結束這段程式。
  52.             System.out.println("圖片id非合法數字。");
  53.             RequestDispatcher requestDispatcher = request.getRequestDispatcher("viewAllPicture.jsp");
  54.             requestDispatcher.forward(request, response);
  55.             return;
  56.         }
  57.  
  58.         IPictureTableDao pictureDao = new PictureTableMSSQLDao();
  59.         PictureTable pictureTable = pictureDao.getPictureWithBlobById(index);
  60.  
  61.         if (pictureTable == null) {
  62.             // 查無資料,結束程式。
  63.             System.out.println("查無資料,結束程式。");
  64.             RequestDispatcher requestDispatcher = request.getRequestDispatcher("viewAllPicture.jsp");
  65.             requestDispatcher.forward(request, response);
  66.             return;
  67.         }
  68.  
  69.         String pictureName = pictureTable.getPictureName();
  70.         InputStream inputStream = null;
  71.         long fileLength = 0L;
  72.         try {
  73.             inputStream = pictureTable.getFile2().getBinaryStream();
  74.             fileLength = pictureTable.getFile2().length();
  75.         } catch (SQLException e) {
  76.             e.printStackTrace();
  77.         }
  78.         OutputStream outputStream = null;
  79.  
  80.         // String mimeType = this.getServletContext().getMimeType(pictureName);
  81.         // System.out.println("mimeType = " + mimeType);
  82.  
  83.         // 原文網址:https://kknews.cc/code/n5xn4yq.html
  84.         String userAgent = request.getHeader("User-agent");
  85.         userAgent = userAgent.toLowerCase();
  86.         System.out.println("userAgent");
  87.         System.out.println(userAgent);
  88.  
  89.         boolean isInternetExplorer = false;
  90.         if (userAgent.indexOf("trident") != -1 || userAgent.indexOf("wow64") != -1 || userAgent.indexOf("edge") != -1
  91.                 || userAgent.indexOf("edg") != -1) {
  92.             System.out.println("Yes. It's Internet Explorer Browser. It's Microsoft Edge Browser");
  93.             isInternetExplorer = true;
  94.         } else {
  95.             isInternetExplorer = false;
  96.         }
  97.  
  98.         if (false == isInternetExplorer) {
  99.             // Google, Firefox, Opera瀏覽器
  100.             pictureName = new String(pictureName.getBytes(), "ISO8859-1");
  101.         } else {
  102.             // Internet Explorer, Microsoft Edge瀏覽器
  103.             pictureName = URLEncoder.encode(pictureName, "UTF-8");
  104.             pictureName = pictureName.replace("+", "%20");
  105.         }
  106.  
  107.         // 清空response
  108.         response.reset();
  109.         // 設定response的Header
  110.         /* FireFox瀏覽器 必須在 fileName前後加上 \" 才能避免瀏覽器擷取到空格就停止擷取檔案名稱。 */
  111.         response.addHeader("Content-Disposition", "attachment;filename=\"" + pictureName + "\"");
  112.         // 這行程式碼設定檔案的名稱,為了避免中文字變亂碼,必須做額外處理。
  113.  
  114.         response.addHeader("Content-Length", String.valueOf(fileLength));
  115.         response.setContentType("application/octet-stream");
  116.  
  117.         outputStream = response.getOutputStream();
  118.         byte[] byteBuffer = new byte[(int) fileLength];
  119.         int length = 0;
  120.         try {
  121.             while ((length = inputStream.read(byteBuffer)) != -1) {
  122.                 outputStream.write(byteBuffer, 0, length);
  123.             }
  124.         } catch (IOException e) {
  125.             e.printStackTrace();
  126.         } finally {
  127.             if (outputStream != null) {
  128.                 outputStream.close();
  129.                 outputStream = null;
  130.             }
  131.             if (inputStream != null) {
  132.                 inputStream.close();
  133.                 inputStream = null;
  134.             }
  135.         }
  136.  
  137.     }// end of doGet() method
  138. }
Add Comment
Please, Sign In to add comment