上回我們說(shuō)了下文件下載的方式有哪些,這次我們從不同的環(huán)境下簡(jiǎn)單來(lái)說(shuō)說(shuō)文件上傳的方式有哪些。
文件上傳的方式
Servlet2.5 方式
Servlet3.0 方式
SpringMVC 方式
案例實(shí)操
Servlet2.5 方式
文件上傳涉及到前臺(tái)頁(yè)面的編寫(xiě)和后臺(tái)服務(wù)器端代碼的編寫(xiě),前臺(tái)發(fā)送文件,后臺(tái)接收并保存文件,這才是一個(gè)完整的文件上傳。
1) 前臺(tái)頁(yè)面
在做文件上傳的時(shí)候,會(huì)有一個(gè)上傳文件的界面,首先我們需要一個(gè)表單,并且表單的請(qǐng)求方式為 POST;其次我們的 form 表單的 enctype 必須設(shè)為”multipart/form-data”即 enctype="multipart/form-data" 意思是設(shè)置表單的 MIME 編碼。默認(rèn)情況下這個(gè)編碼格式是 ”application/x-www-form-urlencoded”,不能用于文件上傳;只有使用了 multipart/form-data 才能完整地傳遞文件數(shù)據(jù)。
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="html" contenteditable="false" cid="n19" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(255, 255, 255); position: relative !important; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>上傳文件</title>
</head>
<body>
<form action="uploadServlet" method="post" enctype="multipart/form-data">
文件:<input type="file" name="myfile"/>
<input type="submit" value="上傳" />
</form>
</body>
</html></pre>
2) 后臺(tái) commons-fileupload 的使用
首先需要導(dǎo)入第三方j(luò)ar包,http://commons.apache.org/ 下載 commons-io 和 commons-fileupload 兩個(gè)jar的資源。解壓并導(dǎo)入到項(xiàng)目中。commons-fileupload.jar 是文件上傳的核心包 commons-io.jar 是 fileupload 的依賴包,同時(shí)又是一個(gè)工具包。

介紹一下使用到的幾個(gè)核心類
DiskFileItemFactory – 設(shè)置磁盤(pán)空間,保存臨時(shí)文件。只是一個(gè)工具類
ServletFileUpload – 文件上傳的核心類,此類接收 request,并解析
ServletFileUpload.parseRequest(request); – List 解析 request
1、創(chuàng)建一個(gè) DiskFileItemFactory 工廠類,并制定臨時(shí)文件和大小
2、創(chuàng)建 ServletFileUpload 核心類,接收臨時(shí)文件,做請(qǐng)求的轉(zhuǎn)換
3、通過(guò) ServletFileUpload 類轉(zhuǎn)換原始請(qǐng)求,得到 FileItem 集合
4、遍歷集合中的各個(gè)元素并處理
5、判斷每個(gè)元素是否是普通表單項(xiàng),如果是則按照普通表單項(xiàng)處理
6、如果不是普通表單項(xiàng),則是文件,通過(guò)處理的方式進(jìn)行處理(上傳)
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" contenteditable="false" cid="n33" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(255, 255, 255); position: relative !important; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public class UploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
?
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 設(shè)定編碼,可以獲取中文文件名
request.setCharacterEncoding("UTF-8");
// 獲取tomcat下的upload目錄的路徑
String path = getServletContext().getRealPath("/upload");
// 臨時(shí)文件目錄
String tempPath = getServletContext().getRealPath("/temp");
// 檢查我們是否有文件上傳請(qǐng)求
// boolean isMultipart = ServletFileUpload.isMultipartContent(req);
// 1、聲明DiskFileItemFactory工廠類,用于在指定磁盤(pán)上設(shè)置一個(gè)臨時(shí)目錄
DiskFileItemFactory disk = new DiskFileItemFactory(1024 * 10, new File(tempPath));
// 2、聲明ServletFileUpload,接收上面的臨時(shí)文件。也可以默認(rèn)值
ServletFileUpload up = new ServletFileUpload(disk);
// 3、解析request
try {
List<FileItem> list = up.parseRequest(request);
if (list.size() > 0) {
for (FileItem file : list) {
// 判斷是否是普通的表單項(xiàng)
if (file.isFormField()) {
String fieldName = file.getFieldName();
// 中文亂碼,此時(shí)還需要指定獲取數(shù)據(jù)的編碼方式
// String value = file.getString();
String value = file.getString("UTF-8");
System.out.println(fieldName + "=" + value);
} else { // 說(shuō)明是一個(gè)文件
// 獲取文件本身的名稱
String fileName = file.getName();
System.out.println(file.getFieldName());
// 處理文件名稱
fileName = fileName.substring(fileName.lastIndexOf("\") + 1);
System.out.println("old Name : " + fileName);
// 修改名稱
String extName = fileName.substring(fileName.lastIndexOf("."));
String newName = UUID.randomUUID().toString().replace("-", "") + extName;
// 保存新的名稱,并寫(xiě)出到新文件中
file.write(new File(path + "/" + newName));
System.out.println("文件名是:" + fileName);
System.out.println("文件大小是:" + file.getSize());
file.delete();
}
}
}
} catch (FileUploadException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
?
}</pre>
Servlet3.0 方式
使用注解 @MultipartConfig 將一個(gè) Servlet 標(biāo)識(shí)為支持文件上傳。Servlet3.0 將 multipart/form-data 的 POST 請(qǐng)求封裝成 Part,通過(guò) Part 對(duì)上傳的文件進(jìn)行操作。
前臺(tái)
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="html" contenteditable="false" cid="n38" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(255, 255, 255); position: relative !important; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>上傳文件</title>
</head>
<body>
<form action="upload" method="post" enctype="multipart/form-data">
姓名:<input type="text" name="uname"/>
文件:<input type="file" name="myfile"/>
<input type="submit" value="上傳" />
</form>
</body>
</html></pre>
后臺(tái)
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" contenteditable="false" cid="n40" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(255, 255, 255); position: relative !important; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">@WebServlet("/upload")
@MultipartConfig
public class UploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
?
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("上傳文件...");
// 設(shè)置編碼
request.setCharacterEncoding("UTF-8");
// 獲取普通表單項(xiàng)參數(shù)
String uname = request.getParameter("uname");
System.out.println(uname);
// 上傳文件
// 得到part對(duì)象 request.getpart(name):name代表的是表單中file元素的name屬性值
Part part = request.getPart("myfile");
// 得到文件存放的路徑
String path = request.getServletContext().getRealPath("/");
// 得到文件名
String fileName = part.getSubmittedFileName();
// 上傳
part.write(path + fileName);
}
?
}</pre>
SpringMVC 方式
Pom 文件修改 添加 commons-fileupload 依賴
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="xml" contenteditable="false" cid="n44" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(255, 255, 255); position: relative !important; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><dependency>
?
<groupId>commons-fileupload</groupId>
?
<artifactId>commons-fileupload</artifactId>
?
<version>1.3.2</version>
?
</dependency> </pre>
servlet-context.xml
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="xml" contenteditable="false" cid="n46" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(255, 255, 255); position: relative !important; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
?
<property name="maxUploadSize">
?
<value>104857600</value>
?
</property>
?
<property name="maxInMemorySize">
?
<value>4096</value>
?
</property>
?
</bean> </pre>
FileController
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" contenteditable="false" cid="n48" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(255, 255, 255); position: relative !important; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">import java.io.File;
?
import java.io.IOException;
?
import javax.servlet.http.HttpServletRequest;
?
import org.springframework.stereotype.Controller;
?
import org.springframework.web.bind.annotation.RequestMapping;
?
import org.springframework.web.multipart.MultipartFile;
?
import org.springframework.web.multipart.MultipartHttpServletRequest;
?
import org.springframework.web.servlet.ModelAndView;
?
@Controller
?
public class FileController {
?
@RequestMapping("/uploadFile")
?
public ModelAndView uploadFile(HttpServletRequest request){
?
ModelAndView mv=new ModelAndView();
?
mv.setViewName("result");
?
MultipartHttpServletRequest mr=(MultipartHttpServletRequest) request;
?
MultipartFile multipartFile= mr.getFile("file");
?
String path=request.getSession().getServletContext().getRealPath("upload");
?
System.out.println(path);
?
if(null!=multipartFile&&!multipartFile.isEmpty()){
?
String fileName=multipartFile.getOriginalFilename();
?
try {
?
multipartFile.transferTo(new File(path,fileName));
?
mv.addObject("msg", "文件上傳成功!");
?
} catch (Exception e) {
?
mv.addObject("msg", "上傳失敗!");
?
e.printStackTrace();
?
}
?
}
?
return mv;
?
}
?
} </pre>
前臺(tái)表單
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="jsp" contenteditable="false" cid="n50" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(255, 255, 255); position: relative !important; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><form action="uploadFile" method="post" enctype="multipart/form-data">
?
<input type="file" name="file"/>
?
<button type="submit"> 提交</button>
?
</form> </pre>
擴(kuò)展~MIME
MIME(Multipurpose Internet Mail Extensions)多用途互聯(lián)網(wǎng)郵件擴(kuò)展類型。是設(shè)定某種擴(kuò)展名的文件用一種應(yīng)用程序來(lái)打開(kāi)的方式類型,當(dāng)該擴(kuò)展名文件被訪問(wèn)的時(shí)候,瀏覽器會(huì)自動(dòng)使用指定應(yīng)用程序來(lái)打開(kāi)。多用于指定一些客戶端自定義的文件名,以及一些媒體文件打開(kāi)方式。
它是一個(gè)互聯(lián)網(wǎng)標(biāo)準(zhǔn),擴(kuò)展了電子郵件標(biāo)準(zhǔn),使其能夠支持:
非ASCII字符文本;非文本格式附件(二進(jìn)制、聲音、圖像等);由多部分(multiple parts)組成的消息體;包含非ASCII字符的頭信息(Header information)。
這個(gè)標(biāo)準(zhǔn)被定義在RFC 2045、RFC 2046、RFC 2047、RFC 2048、RFC 2049等RFC中。 MIME改善了由RFC 822轉(zhuǎn)變而來(lái)的RFC 2822,這些舊標(biāo)準(zhǔn)規(guī)定電子郵件標(biāo)準(zhǔn)并不允許在郵件消息中使用7位ASCII字符集以外的字符。正因如此,一些非英語(yǔ)字符消息和二進(jìn)制文件,圖像,聲音等非文字消息原本都不能在電子郵件中傳輸(MIME可以)。MIME規(guī)定了用于表示各種各樣的數(shù)據(jù)類型的符號(hào)化方法。 此外,在萬(wàn)維網(wǎng)中使用的HTTP協(xié)議中也使用了MIME的框架,標(biāo)準(zhǔn)被擴(kuò)展為互聯(lián)網(wǎng)媒體類型。
查看不同文件對(duì)應(yīng)的 MIME 類型,推薦大家一種方式,以 Tomcat為例,它下面的 web.xml 文件可以查看所有的MIME類型,通過(guò) Ctrl + F 搜索快速找到你想知道的文件對(duì)應(yīng)的 MIME 類型。
