1 multipart/form-data
多部件傳輸方式蚌成,也就是說選擇這種方式進(jìn)行上傳,可以上傳文件和參數(shù)
使用Postman工具進(jìn)行該方式提交數(shù)據(jù),如下圖所示
這里的file就對應(yīng)了你需要上傳的圖片蜓谋、或者其他文件;Text類型就是普通key-value參數(shù)和參數(shù)值炭分,下面使用Java進(jìn)行實現(xiàn)一下文件上傳
1.1 實現(xiàn)文件上傳
- 加入依賴包
<dependencies>
<!-- 文件上傳 -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.1</version>
</dependency>
</dependencies>
- 編寫Java類
public void getFileFromHttpRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
// 1. 創(chuàng)建磁盤文件工廠對象
DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
// 2. 創(chuàng)建文件上傳核心類
ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory);
// 2.1 設(shè)置上傳文件的編碼
servletFileUpload.setHeaderEncoding("UTF-8");
// 2.2 判斷表單是否是文件上傳表單
if (servletFileUpload.isMultipartContent(request)) { // 是多部文件上傳表單
// 3. 解析request -> 獲取表單項的集合
List<FileItem> list = servletFileUpload.parseRequest(request);
if (null != list) {
// 4. 遍歷集合獲取表單項
for (FileItem fileItem : list) {
// 5. 判斷當(dāng)前表單項 是不是普通表單項
if (fileItem.isFormField()) { // 普通表單項
String fieldName = fileItem.getFieldName();
// 設(shè)置編碼
String fieldValue = null;//設(shè)置編碼
try {
fieldValue = fileItem.getString("utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println(fieldName +" = " +fieldValue);
} else {
//文件上傳項
//獲取文件名
String fileName = fileItem.getName();
// 拼接新的文件名桃焕,使用UUID保證唯一性
String newFileName = String.format("%s_%s", UUIDUtils.getUUID(), fileName);
// 獲取輸入流
InputStream inputStream = fileItem.getInputStream();
//創(chuàng)建輸出流
//1.獲取項目的運行目錄 J:\install\apache-tomcat-9.0.41\webapps\lagou_edu_home\
String realPath = this.getServletContext().getRealPath("/");
//2.截取到 webapps目錄路徑
String wabappsPath = realPath.substring(0, realPath.indexOf("lagou_edu_home"));
//3.拼接輸出路徑,將圖片保存到 upload
FileOutputStream out = new FileOutputStream(wabappsPath+"/upload/" + newFileName);
//使用IOUtils完成 文件的copy
IOUtils.copy(inputStream,out);
//關(guān)閉流
out.close();
inputStream.close();
}
}
}
}
} catch (FileUploadException e) {
e.printStackTrace();
}
}
2 application/x-www-form-urlencoded
提交form表單,只能是key-value值
- Java獲取form表單里面的參數(shù)值
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1. 獲取post請求中from表單的數(shù)據(jù)
Map<String, String[]> parameterMap = request.getParameterMap();
parameterMap.entrySet().forEach(System.out::print);
}
3 raw
提交Json數(shù)據(jù)格式捧毛,在Servlet中使用工具類進(jìn)行解析獲取參數(shù)值
- Java獲取Json數(shù)據(jù)格式的參數(shù)值
public String getPostJSON(HttpServletRequest request){
try {
//1.從request中 獲取緩沖輸入流對象
BufferedReader reader = request.getReader();
//2.創(chuàng)建StringBuffer 保存讀取出的數(shù)據(jù)
StringBuffer sb = new StringBuffer();
//3.循環(huán)讀取
String line = null;
while((line = reader.readLine()) != null){
//將每次讀取的數(shù)據(jù) 追加到StringBuffer
sb.append(line);
}
//4.返回結(jié)果
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
總結(jié):讀取請求里面的流观堂,然后轉(zhuǎn)為Map結(jié)構(gòu)