1. 程式人生 > >HTML開啟攝像頭進行拍照上傳

HTML開啟攝像頭進行拍照上傳

複製程式碼
  1 package com.servlet;
  2 
  3 import java.io.File;
  4 import java.io.FileOutputStream;
  5 import java.io.IOException;
  6 import java.io.InputStream;
  7 import java.io.OutputStream;
  8 import java.util.Date;
  9 import java.util.List;
 10 
 11 import javax.servlet.ServletException;
 12 import
javax.servlet.http.HttpServlet; 13 import javax.servlet.http.HttpServletRequest; 14 import javax.servlet.http.HttpServletResponse; 15 16 import org.apache.commons.fileupload.FileItem; 17 import org.apache.commons.fileupload.FileUploadException; 18 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
19 import org.apache.commons.fileupload.servlet.ServletFileUpload; 20 21 import sun.misc.BASE64Decoder; 22 23 /** 24 * Servlet implementation class webcam 25 */ 26 public class webcam extends HttpServlet { 27 private static final long serialVersionUID = 1L; 28 29 /** 30 * @see
HttpServlet#HttpServlet() 31 */ 32 public webcam() { 33 super(); 34 // TODO Auto-generated constructor stub 35 } 36 37 /** 38 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse 39 * response) 40 */ 41 protected void doGet(HttpServletRequest request, 42 HttpServletResponse response) throws ServletException, IOException { 43 // TODO Auto-generated method stub 44 doPost(request, response); 45 } 46 47 /** 48 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse 49 * response) 50 */ 51 protected void doPost(HttpServletRequest request, 52 HttpServletResponse response) throws ServletException, IOException { 53 request.setCharacterEncoding("utf-8"); 54 response.setCharacterEncoding("utf-8"); 55 response.setContentType("text/html;char=utf-8"); 56 57 // 獲得磁碟檔案條目工廠 58 DiskFileItemFactory factory = new DiskFileItemFactory(); 59 // 獲取檔案需要上傳到的路徑 60 // String path = request.getRealPath("/upload"); 61 String path = "d:/shangchuan/"; 62 63 // 如果資料夾不存在 將建立資料夾 64 if (!new File(path).exists()) { 65 new File(path).mkdirs(); 66 } 67 68 // 如果沒以下兩行設定的話,上傳大的 檔案 會佔用 很多記憶體, 69 // 設定暫時存放的 儲存室 , 這個儲存室,可以和 最終儲存檔案 的目錄不同 70 /** 71 * 原理 它是先存到 暫時儲存室,然後在真正寫到 對應目錄的硬碟上, 按理來說 當上傳一個檔案時,其實是上傳了兩份,第一個是以 .tem 72 * 格式的 然後再將其真正寫到 對應目錄的硬碟上 73 */ 74 factory.setRepository(new File(path)); 75 // 設定 快取的大小,當上傳檔案的容量超過該快取時,直接放到 暫時儲存室 76 factory.setSizeThreshold(1024 * 1024); 77 78 // 高水平的API檔案上傳處理 79 ServletFileUpload upload = new ServletFileUpload(factory); 80 81 try { 82 // 可以上傳多個檔案 83 List<FileItem> list = (List<FileItem>) upload.parseRequest(request); 84 85 for (FileItem item : list) { 86 // 獲取表單的屬性名字 87 String name = item.getFieldName(); 88 89 // 如果獲取的 表單資訊是普通的 文字 資訊 90 if (item.isFormField()) { 91 // 獲取使用者具體輸入的字串 ,名字起得挺好,因為表單提交過來的是 字串型別的 92 String imgStr = item.getString(); 93 94 // base64解碼並生成圖片 95 BASE64Decoder decoder = new BASE64Decoder(); 96 try { 97 // Base64解碼 98 byte[] bytes = decoder.decodeBuffer(imgStr); 99 // for (int i = 0; i < bytes.length; ++i) { 100 // if (bytes[i] < 0) {// 調整異常資料 101 // bytes[i] += 256; 102 // } 103 // } 104 // 生成jpeg圖片 105 OutputStream out = new FileOutputStream("d:/ceshi.jpg"); 106 out.write(bytes); 107 out.flush(); 108 out.close(); 109 } catch (Exception e) { 110 e.printStackTrace(); 111 } 112 // request.setAttribute(name, value); 113 }else { 114 // 對傳入的非 簡單的字串進行處理 ,比如說二進位制的 圖片,電影這些 115 /** 116 * 以下三步,主要獲取 上傳檔案的名字 117 */ 118 // 獲取路徑名 119 String value = item.getName(); 120 // 索引到最後一個反斜槓 121 int start = value.lastIndexOf("\\"); 122 // 擷取 上傳檔案的 字串名字,加1是 去掉反斜槓, 123 String filename = value.substring(start + 1); 124 // 將當前時間戳 作為的檔名 125 String newfilename = Long.toString(new Date().getTime()) 126 + filename.substring(filename.indexOf('.')); 127 request.setAttribute(name, newfilename); 128 129 // 真正寫到磁碟上 130 // 它丟擲的異常 用exception 捕捉 131 132 // item.write( new File(path,filename) );//第三方提供的 133 134 // 手動寫的 135 OutputStream out = new FileOutputStream(new File(path, 136 newfilename)); 137 138 InputStream in = item.getInputStream(); 139 140 int length = 0; 141 byte[] buf = new byte[1024]; 142 143 System.out.println("獲取上傳檔案的總共的容量:" + item.getSize()); 144 145 // in.read(buf) 每次讀到的資料存放在 buf 陣列中 146 while ((length = in.read(buf)) != -1) { 147 // 在 buf 陣列中 取出資料 寫到 (輸出流)磁碟上 148 out.write(buf, 0, length); 149 } 150 in.close(); 151 out.close(); 152 } 153 } 154 155 } catch (FileUploadException e) { 156 // TODO Auto-generated catch block 157 e.printStackTrace(); 158 } catch (Exception e) { 159 // TODO Auto-generated catch block 160 e.printStackTrace(); 161 } 162 } 163 }
複製程式碼