1. 程式人生 > >Java進階學習第十三天(檔案上傳與下載、JavaMail郵件開發)

Java進階學習第十三天(檔案上傳與下載、JavaMail郵件開發)

一、檔案上傳與下載

1、檔案上傳
案例:登錄檔單/儲存商品等相關模組!
頭像 / 商品圖片(資料庫:儲存圖片路徑 / 圖片儲存到伺服器中指定的目錄)

2、檔案上傳要點
① 表單提交方式:post
② 表單中有檔案上傳的表單項: <input type=”file” />
③ 指定表單型別:
◆ 預設型別:enctype="application/x-www-form-urlencoded"
◆ 檔案上傳型別:enctype="multipart/form-data"

3、手動實現檔案上傳

<body>	
  	 <form name="frm_test" action="${pageContext.request.contextPath }/upload" method="post" enctype="multipart/form-data">
  	 	 使用者名稱:<input type="text" name="userName">  <br/>
  	 	 檔案:<input type="file" name="file_img">   <br/>
  	 	<input type="submit" value="註冊">
   	 </form>
</body>
public class UploadServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		/*
		request.getParameter(""); // GET/POST
		request.getQueryString(); // 獲取GET提交的資料 
		request.getInputStream(); // 獲取post提交的資料   */
		
		//1. 獲取表單資料流
		InputStream in =  request.getInputStream();
		//2. 轉換流
		InputStreamReader inStream = new InputStreamReader(in, "UTF-8");
		//3. 緩衝流
		BufferedReader reader = new BufferedReader(inStream);
		// 輸出資料
		String str = null;
		while ((str = reader.readLine()) != null) {
			System.out.println(str);
		}
		// 關閉
		reader.close();
		inStream.close();
		in.close();
}

輸出結果

------WebKitFormBoundaryGoQviatB7iM1dhPr
Content-Disposition: form-data; name="userName"       

Jack
------WebKitFormBoundaryGoQviatB7iM1dhPr
Content-Disposition: form-data; name="file_img"; filename="reamde.txt"
Content-Type: text/plain                                  

test!!!!!!!!!!!!!
test!!!!!!!!!!!!!
------WebKitFormBoundaryGoQviatB7iM1dhPr--

4、總結
最終獲取資料,要對上面的結果進行解析!
檔案上傳,在開發中經常用,每次都寫解析程式(工具類)
也可以使用開源的檔案上傳元件:FileUpload元件

5、Apache提供的檔案上傳元件:FileUpload元件(FileItemFactory、ServletFileUpload、FileItem)
① 下載原始碼
② 專案中引入jar檔案
commons-fileupload-1.2.1.jar 【檔案上傳元件核心jar包】
commons-io-1.4.jar 【封裝了對檔案處理的相關工具類】

public class UploadServlet extends HttpServlet {
	// upload目錄,儲存上傳的資源
	public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException 
		try {
			// 1. 檔案上傳工廠類(把每一個請求表單封裝為一個Fileltem物件)
			FileItemFactory factory = new DiskFileItemFactory();
			// 2. 建立檔案上傳核心工具類(可以獲取所有的Fileltem物件)
			ServletFileUpload upload = new ServletFileUpload(factory);
			// 1)設定單個檔案允許的最大的大小:30M
			upload.setFileSizeMax(30*1024*1024);
			// 2)設定檔案上傳表單允許的總大小:80M
			upload.setSizeMax(80*1024*1024);
			// 3)設定上傳表單檔名的編碼
			// 相當於:request.setCharacterEncoding("UTF-8");
			upload.setHeaderEncoding("UTF-8");
			// 3. 判斷: 當前表單是否為檔案上傳表單
			if (upload.isMultipartContent(request)){
				// 4. 把請求資料轉換為一個個FileItem物件,再用集合封裝
				//獲取所有檔案上傳項
				List<FileItem> list = upload.parseRequest(request);
				// 遍歷: 得到每一個上傳的資料
				for (FileItem item: list){
					// 判斷:普通文字資料
					if (item.isFormField()){
						// 普通文字資料
						String fieldName = item.getFieldName();	// 表單元素名稱
						String content = item.getString();		// 表單元素名稱對應的資料
						//item.getString("UTF-8");  指定編碼
						System.out.println(fieldName + " " + content);
					}
					// 上傳檔案(檔案流) ----> 上傳到upload目錄下
					else {
						// 檔案上傳表單
						String fieldName = item.getFieldName();	// 表單元素名稱
						String name = item.getName();			// 檔名				
						String content = item.getString();		// 表單元素名稱對應的資料
						String content = item.getString("utf-8");		//獲取上傳資料,處理中文
						String type = item.getContentType();	// 檔案型別
						InputStream in = item.getInputStream(); // 上傳檔案流
						/*
						 *  四、檔名重名
						 *  對於不同使用者readme.txt檔案,不希望覆蓋!
						 *  後臺處理:給使用者新增一個唯一標記
						 */
						// a. 隨機生成一個唯一標記
						String id = UUID.randomUUID().toString();
						// b. 與檔名拼接
						name = id +"#"+ name;
						// 獲取上傳基路徑
						String path = getServletContext().getRealPath("/upload");
						// 建立目標檔案
						File file = new File(path,name);	
						// 工具類,檔案上傳
						item.write(file);
						item.delete(); //刪除系統產生的臨時檔案
						System.out.println();
					}	
				}	
			}
			else {
				System.out.println("當前表單不是檔案上傳表單,處理失敗!");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}	
}

6、完整案例:檔案上傳與下載
① Index.jsp

<body>	
  		<a href="${pageContext.request.contextPath }/upload.jsp">檔案上傳</a> &nbsp;&nbsp;&nbsp;
  		<a href="${pageContext.request.contextPath }/fileServlet?method=downList">檔案下載</a> 	
</body>

② Upload.jsp

<body>	
  	 <form name="frm_test" action="${pageContext.request.contextPath }/fileServlet?method=upload" method="post" enctype="multipart/form-data">
  	 	 <%--<input type="hidden" name="method" value="upload">--%>	 
  	 	 使用者名稱:<input type="text" name="userName">  <br/>
  	 	 檔案:  <input type="file" name="file_img">   <br/>
  	 	<input type="submit" value="提交">
   	 </form>
</body>

③ Downlist.jsp

<body>	
	<table border="1" align="center">
		<tr>
			<th>序號</th>
			<th>檔名</th>
			<th>操作</th>
		</tr>
		<c:forEach var="en" items="${requestScope.fileNames}" varStatus="vs">
			<tr>
				<td>${vs.count }</td>
				<td>${en.value }</td>
				<td>
					<%--<a href="${pageContext.request.contextPath }/fileServlet?method=down&..">下載</a>--%>
					<!-- 構建一個地址  -->
					<c:url var="url" value="fileServlet">
						<c:param name="method" value="down"></c:param>
						<c:param name="fileName" value="${en.key}"></c:param>
					</c:url>
					<!-- 使用上面地址 -->
					<a href="${url }">下載</a>
				</td>
			</tr>
		</c:forEach>
	</table>  		
  </body>

④ FileServlet.java

/**
 * 處理檔案上傳與下載
 */
public class FileServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		// 獲取請求引數: 區分不同的操作型別
		String method = request.getParameter("method");
		if ("upload".equals(method)) {
			// 上傳
			upload(request,response);
		}
		else if ("downList".equals(method)) {
			// 進入下載列表
			downList(request,response);
		}
		else if ("down".equals(method)) {
			// 下載
			down(request,response);
		}
	}

	/**
	 * 1. 上傳
	 */
	private void upload(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {	
		try {
			// 1. 建立檔案上傳工廠類
			FileItemFactory factory = new DiskFileItemFactory();
			// 2. 建立檔案上傳核心類物件
			ServletFileUpload upload = new ServletFileUpload(factory);
			// 設定大小限制引數
			upload.setFileSizeMax(30*1024*1024);	// 單個檔案大小限制:30M
			upload.setSizeMax(50*1024*1024);		// 總檔案大小限制:50M
			upload.setHeaderEncoding("UTF-8");		// 對中文檔案編碼處理
			// 判斷表單是否為檔案上傳表單
			if (upload.isMultipartContent(request)) {
				// 3. 把請求資料轉換為FileItem集合
				List<FileItem> list = upload.parseRequest(request);
				// 遍歷
				for (FileItem item : list){
					// 判斷:普通文字資料
					if (item.isFormField()){
						// 獲取名稱
						String name = item.getFieldName();
						// 獲取值
						String value = item.getString("utf-8");
						System.out.println(value);
					} 
					// 檔案表單項
					else {
						/******** 檔案上傳 ***********/
						// a. 獲取檔名稱
						String name = item.getName();
						// ----處理上傳檔名重名問題----
						// a1. 先得到唯一標記
						String id = UUID.randomUUID().toString();
						// a2. 拼接檔名
						name = id + "#" + name;						
						// b. 得到上傳目錄(當前專案路徑下)
						String basePath = getServletContext().getRealPath("/upload");
						// c. 建立要上傳的檔案物件
						File file = new File(basePath,name);
						// d. 上傳
						item.write(file);
						item.delete();  // 刪除元件執行時產生的臨時檔案
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 2. 進入下載列表
	 */
	private void downList(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		// 實現思路:先獲取upload目錄下所有檔案的檔名,再儲存;跳轉到down.jsp列表展示
		//1. 初始化map集合Map<包含唯一標記的檔名, 簡短檔名>  ;
		Map<String,String> fileNames = new HashMap<String,String>();
		//2. 獲取上傳目錄,及其下所有的檔案的檔名
		String bathPath = getServletContext().getRealPath("/upload");
		// 目錄
		File file = new File(bathPath);
		// 目錄下,所有檔名
		String list[] = file.list();
		// 遍歷,封裝
		if (list != null && list.length > 0){
			for (int i=0; i<list.length; i++){
				// 全名
				String fileName = list[i];
				// 短名
				String shortName = fileName.substring(fileName.lastIndexOf("#")+1);
				// 封裝
				fileNames.put(fileName, shortName);
			}
		}
		// 3. 儲存到request域
		request.setAttribute("fileNames", fileNames);
		// 4. 轉發
		request.getRequestDispatcher("/downlist.jsp").forward(request, response);
	}

	/**
	 *  3. 處理下載
	 */
	private void down(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		// 獲取使用者下載的檔名稱(url地址後追加資料,意味著get提交方式)
		String fileName = request.getParameter("fileName");
		fileName = new String(fileName.getBytes("ISO8859-1"),"UTF-8");
		// 先獲取上傳目錄路徑
		String basePath = getServletContext().getRealPath("/upload");
		// 獲取一個檔案流
		InputStream in = new FileInputStream(new File(basePath,fileName));
		// 如果檔名是中文,需要進行url編碼
		fileName = URLEncoder.encode(fileName, "UTF-8");
		// 設定下載的響應頭
		response.setHeader("content-disposition", "attachment;fileName=" + fileName);
		// 獲取response位元組流
		OutputStream out = response.getOutputStream();
		byte[] b = new byte[1024];
		int len = -1;
		while ((len = in.read(b)) != -1){
			out.write(b, 0, len);
		}
		// 關閉
		out.close();
		in.close();
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}
}

二、JavaMail郵件開發

1、開發中,郵件的應用
註冊,填寫生日; 後期系統會自動傳送生日祝賀
發貨,發貨提醒!郵件提醒!

2、郵件:
① 發郵件;【程式中如何發郵件?】
② 收郵件;【很多客戶端工具:foxmail 】

3、郵件開發準備(環境搭建)
① 本地搭建一個郵件伺服器(易郵伺服器,eyoumailserversetup.exe)
② 新建郵箱賬號
◆ 步驟1:新建域名: 工具, 伺服器設定, 單域名框中輸入 itcast.com
◆ 步驟2:新建郵箱賬號: [email protected]
③ 安裝foxmail
◆ 配置郵件傳送伺服器(smtp): localhost 25
◆ 郵件接收伺服器(pop3): localhost 110
◆ 再新建賬號,就可以接收郵件了!

4、郵件開發之普通郵件
JavaMail開發,先引入jar檔案:
activation.jar【如果使用jdk1.6或以上版本,可以不用這個jar檔案】
mail.jar【郵件傳送核心包】

/**
 * 傳送一封普通郵件
 */
public class App_SendMail {
	@Test
	public void testSend() throws Exception {		
		//0. 郵件引數
		Properties prop = new Properties();
		prop.put("mail.transport.protocol", "smtp");// 指定協議
		prop.put("mail.smtp.host", "localhost");	// 主機   stmp.qq.com
		prop.put("mail.smtp.port", 25);				// 埠
		prop.put("mail.smtp.auth", "true");			// 使用者密碼認證
		prop.put("mail.debug", "true");				// 除錯模式
		//1. 建立一個郵件的會話
		Session session = Session.getDefaultInstance(prop);
		//2. 建立郵件體物件 (整封郵件物件)
		MimeMessage message = new MimeMessage(session);
		//3. 設定郵件體引數: 
		//3.1 標題
		message.setSubject("我的第一封郵件	");
		//3.2 郵件傳送時間
		message.setSentDate(new Date());
		//3.3 發件人
		message.setSender(new InternetAddress("[email protected]"));
		//3.4 接收人
		message.setRecipient(RecipientType.TO, new InternetAddress("[email protected]"));
		//3.5內容
		message.setText("正文XXXXXXXXXX");  // 簡單純文字郵件
		//message.setContent("<a href='#'>超連結</a>"); //傳送超連結
		message.saveChanges();   // 儲存郵件(可選)
		//4. 傳送
		Transport trans = session.getTransport();
		trans.connect("zhangsan", "123456");
		// 傳送郵件
		trans.sendMessage(message, message.getAllRecipients());
		trans.close();
	}
}

6、郵件開發之帶圖片

/**
 * 帶圖片資源的郵件
 */
public class App_2SendWithImg {
	// 初始化引數
	private static Properties prop;
	// 發件人
	private static InternetAddress sendMan = null;
	static {
		prop = new Properties();
		prop.put("mail.transport.protocol", "smtp");// 指定協議
		prop.put("mail.smtp.host", "localhost");	// 主機   stmp.qq.com
		prop.put("mail.smtp.port", 25);				// 埠
		prop.put("mail.smtp.auth", "true");			// 使用者密碼認證
		prop.put("mail.debug", "true");				// 除錯模式
		try {
			sendMan = new InternetAddress("[email protected]");
		} catch (AddressException e) {
			throw new RuntimeException(e);
		}
	}

	@Test
	public void testSend() throws Exception {
		// 1. 建立郵件會話
		Session session = Session.getDefaultInstance(prop);
		// 2. 建立郵件物件
		MimeMessage message = new MimeMessage(session);
		// 3. 設定引數:標題、發件人、收件人、傳送時間、內容
		message.setSubject("帶圖片郵件");
		message.setSender(sendMan);
		message.setRecipient(RecipientType.TO, new InternetAddress("[email protected]"));
		message.setSentDate(new Date());		
		/********設定郵件內容: 多功能使用者郵件 (related)********/
		// 4.1 構建一個多功能郵件塊
		MimeMultipart related = new MimeMultipart("related");
		// 4.2 構建多功能郵件塊內容 = 左側文字 + 右側圖片資源
		MimeBodyPart content = new MimeBodyPart();
		MimeBodyPart resource = new MimeBodyPart();	
		// 設定具體內容: a.資源(圖片)
		String filePath = App_2SendWithImg.class.getResource("8.jpg").getPath();
		DataSource ds = new FileDataSource(new File(filePath));
		DataHandler handler = new DataHandler(ds);
		resource.setDataHandler(handler);
		resource.setContentID("8.jpg");   // 設定資源名稱,給外來鍵引用	
		// 設定具體內容: b.文字
		content.setContent("<img src='cid:8.jpg'/>傳送的圖片", "text/html;charset=UTF-8");
		related.addBodyPart(content);
		related.addBodyPart(resource);
		/********4.3 把構建的複雜郵件快,新增到郵件中********/
		message.setContent(related);
		// 5. 傳送
		Transport trans = session.getTransport();
		trans.connect("zhangsan", "123456");
		trans.sendMessage(message, message.getAllRecipients());
		trans.close();
	}
}

7、郵件開發之帶圖片 + 附件

/**
 * 帶圖片資源以及附件的郵件
 */
public class App_3ImgAndAtta {
	// 初始化引數
	private static Properties prop;
	// 發件人
	private static InternetAddress sendMan = null;
	static {
		prop = new Properties();
		prop.put("mail.transport.protocol", "smtp");// 指定協議
		prop.put("mail.smtp.host", "localhost");	// 主機   stmp.qq.com
		prop.put("mail.smtp.port", 25);				// 埠
		prop.put("mail.smtp.auth", "true");			// 使用者密碼認證
		prop.put("mail.debug", "true");				// 除錯模式
		try {
			sendMan = new InternetAddress("[email protected]");
		} catch (AddressException e) {
			throw new RuntimeException(e);
		}
	}

	@Test
	public void testSend() throws Exception {
		// 1. 建立郵件會話
		Session session = Session.getDefaultInstance(prop);
		// 2. 建立郵件物件
		MimeMessage message = new MimeMessage(session);
		// 3. 設定引數:標題、發件人、收件人、傳送時間、內容
		message.setSubject("帶圖片郵件");
		message.setSender(sendMan);
		message.setRecipient(RecipientType.TO, new InternetAddress("[email protected]"));
		message.setSentDate(new Date());
		/*
		 * 帶附件(圖片)郵件開發
		 */
		// 構建一個總的郵件塊
		MimeMultipart mixed = new MimeMultipart("mixed");
		// ---> 總郵件快,設定到郵件物件中
		message.setContent(mixed);
		// 左側: (文字+圖片資源)
		MimeBodyPart left = new MimeBodyPart();
		// 右側: 附件
		MimeBodyPart right = new MimeBodyPart();
		// 設定到總郵件塊
		mixed.addBodyPart(left);
		mixed.addBodyPart(right);
		/********附件********/
		String attr_path = this.getClass().getResource("a.docx").getPath();
		DataSource attr_ds = new FileDataSource(new File(attr_path));
		DataHandler attr_handler = new DataHandler(attr_ds);
		right.setDataHandler(attr_handler);
		right.setFileName("a.docx");
		/********設定郵件內容: 多功能使用者郵件 (related)********/
		// 4.1 構建一個多功能郵件塊
		MimeMultipart related = new MimeMultipart("related");
		// ----> 設定到總郵件快的左側中
		left.setContent(related);
		// 4.2 構建多功能郵件塊內容 = 左側文字 + 右側圖片資源
		MimeBodyPart content = new MimeBodyPart();
		MimeBodyPart resource = new MimeBodyPart();
		// 設定具體內容: a.資源(圖片)
		String filePath = App_3ImgAndAtta.class.getResource("8.jpg").getPath();
		DataSource ds = new FileDataSource(new File(filePath));
		DataHandler handler = new DataHandler(ds);
		resource.setDataHandler(handler);
		resource.setContentID("8.jpg");   // 設定資源名稱,給外來鍵引用
		// 設定具體內容: b.文字
		content.setContent("<img src='cid:8.jpg'/>傳送的圖片", "text/html;charset=UTF-8");
		related.addBodyPart(content);
		related.addBodyPart(resource);
		// 5. 傳送
		Transport trans = session.getTransport();
		trans.connect("zhangsan", "123456");
		trans.sendMessage(message, message.getAllRecipients());
		trans.close();
	}
}

8、注意
如果是web專案,因為javaee自帶的有郵件功能,可能存在問題。需要刪除javaee中mail包,我們要用自己的mail.jar檔案功能!