104_JavaWeb_文件上傳_ServletFileUpload_下載
文件上傳,HTTP 協(xié)議的說(shuō)明
commons-fileupload.jar 常用 API 介紹說(shuō)明
1 導(dǎo)包 commons-fileupload.jar 依賴(lài) commons-io.jar
2 常用類(lèi)
ServletFileUpload 類(lèi),用于解析上傳的數(shù)據(jù)
FileItem 類(lèi),表示每一個(gè)表單項(xiàng)
boolean ServletFileUpload.isMultipartContent(HttpServletRequest request);
判斷當(dāng)前上傳的數(shù)據(jù)格式是否是多段的格式
public List
boolean FileItem.isFormField() 判斷當(dāng)前這個(gè)表單項(xiàng),是否是普通的表單項(xiàng)。還是上傳的文件類(lèi)型。
true 表示普通類(lèi)型的表單項(xiàng)
false 表示上傳的文件類(lèi)型
String FileItem.getFieldName() 獲取表單項(xiàng)的 name 屬性值
String FileItem.getString() 獲取當(dāng)前表單項(xiàng)的值。
String FileItem.getName(); 獲取上傳的文件名
void FileItem.write( file ); 將上傳的文件寫(xiě)到 參數(shù) file 所指向抽硬盤(pán)位置
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
public class uploadServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); // 解決亂碼問(wèn)題 //1 先判斷上傳的數(shù)據(jù)是否多段數(shù)據(jù)(只有是多段的數(shù)據(jù),才是文件上傳的) if (ServletFileUpload.isMultipartContent(request)){ //創(chuàng)建FileItemFactory工廠實(shí)現(xiàn)類(lèi) FileItemFactory fileitemfactory = new DiskFileItemFactory(); // 創(chuàng)建用于解析上傳數(shù)據(jù)的工具類(lèi)ServletFileUpload類(lèi) ServletFileUpload servletFileUpload = new ServletFileUpload(fileitemfactory); try { // 解析上傳的數(shù)據(jù),得到每一個(gè)表單項(xiàng)FileItem List
文件下載
下載的常用 API:
response.getOutputStream();
servletContext.getResourceAsStream();
servletContext.getMimeType();
response.setContentType();
public class Download extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 1、獲取要下載的文件名 String downloadFileName = "2.png"; // 2、讀取要下載的文件內(nèi)容 (通過(guò)ServletContext對(duì)象可以讀取) ServletContext servletContext = getServletContext(); //獲取要下載的文件類(lèi)型 String type = servletContext.getMimeType("/file/" + downloadFileName); // 4、在回傳前,通過(guò)響應(yīng)頭告訴客戶(hù)端返回的數(shù)據(jù)類(lèi)型 response.setContentType(type); // 5、還要告訴客戶(hù)端收到的數(shù)據(jù)是用于下載使用(還是使用響應(yīng)頭) // Content-Disposition響應(yīng)頭,表示收到的數(shù)據(jù)怎么處理 // attachment表示附件,表示下載使用 // filename= 表示指定下載的文件名 // url編碼是把漢字轉(zhuǎn)換成為%xx%xx的格式 // 如果是火狐瀏覽器使用Base64編碼 if (request.getHeader("User-agent").contains("Firefox")){ response.setHeader("Content-Disposition", "attachment; filename==?UTF-8?B?" + new BASE64Encoder().encode("中國(guó).jpg".getBytes("UTF-8")) + "?="); } else { // 如果不是火狐,是IE或谷歌,使用URL編碼操作 response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("打卡.jpg", "UTF-8")); } /** * /斜杠被服務(wù)器解析表示地址為http://ip:prot/工程名/ 映射 到代碼的Web目錄 */ InputStream resourceAsStream = servletContext.getResourceAsStream("/files/" + downloadFileName); // 獲取響應(yīng)的輸出流 OutputStream outputStream = response.getOutputStream(); //3、把下載的文件內(nèi)容回傳給客戶(hù)端 // 讀取輸入流中全部的數(shù)據(jù),復(fù)制給輸出流,輸出給客戶(hù)端 IOUtils.copy(resourceAsStream, outputStream); } }
Java Servlet web前端
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶(hù)投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。