一、文件上傳的回顧
1、整體感知
2、案例分析
(1)添加依賴
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
(2)編寫文件上傳的jsp頁面
<h2>傳統文件上傳</h2>
<form action="file/fileUpload1" method="post" enctype="multipart/form-data">
選擇文件:<input type="file" name="upload"/><br/>
<input type="submit" value="上傳文件"/>
</form>
(3)編寫文件上傳的Controller控制器
@RequestMapping("/fileUpload1")
public String fileUpload1(HttpServletRequest request) throws Exception {
// 先獲取要上傳=到哪個文件目錄
String path = request.getSession().getServletContext().getRealPath("/uploads");
System.out.println(path);
// 創建File對象,一會向該路徑下上傳文件
File file = new File(path);
// 判斷路徑是否存在,如果不存在,創建該路徑
if (!file.exists()) {
file.mkdirs();
}
// 創建磁盤文件項工廠
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload fileUpload = new ServletFileUpload(factory);
// 解析request對象
List<FileItem> list = fileUpload.parseRequest(request);
// 遍歷
for (FileItem fileItem : list) {
// 判斷文件項是普通字段,還是上傳的文件
if (fileItem.isFormField()) {
} else {
// 上傳文件項
// 獲取到上傳文件的名稱
String filename = fileItem.getName();
// 把文件的名稱設置成唯一值
String uuid = UUID.randomUUID().toString().replace("-", "");
filename = uuid + "_" + filename;
// 上傳文件
fileItem.write(new File(file, filename));
// 刪除臨時文件
fileItem.delete();
}
}
return "success";
}
在這裏,我們需要打印一下path變量,因爲path變量打印的結果是我們最終將圖片存放到的具體路徑。
(4)結果演示
二、Spring MVC傳統方式文件上傳
1、編寫jsp頁面
<h2>Spring MVC傳統方式文件上傳</h2>
<form action="file/fileUpload2" method="post" enctype="multipart/form-data">
選擇文件:<input type="file" name="upload"/><br/>
<input type="submit" value="上傳文件"/>
</form>
2、編寫文件上傳的Controller控制器
@RequestMapping("/fileUpload2")
public String fileUpload2(HttpServletRequest request, MultipartFile upload) throws Exception {
// 獲取路徑
String path = request.getSession().getServletContext().getRealPath("/uploads");
// 創建File對象,一會向該路徑下上傳文件
File file = new File(path);
// 判斷路徑是否存在,如果不存在,創建該路徑
if(!file.exists()) {
file.mkdirs();
}
// 獲取到上傳文件的名稱
String filename = upload.getOriginalFilename();
// 把文件的名稱設置成唯一值
String uuid = UUID.randomUUID().toString().replace("-", "");
filename = uuid + "_" + filename;
upload.transferTo(new File(file,filename));
return "success";
}
☆注意: Spring MVC框架提供了MultipartFile對象,該對象表示上傳的文件,要求變量名稱必須和表單file標籤的name屬性名稱相同。
3、在springmvc.xml中配置文件解析器
<!--配置文件解析器-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!--設置最大上傳文件大小爲10M-->
<property name="maxUploadSize" value="10485760"></property>
</bean>
☆注意:文件上傳解析器id的值(multipartResolver)是固定的,不可隨意命名,否則無法實現請求參數的綁定。
4、結果演示
三、Spring MVC跨服務器方式文件上傳
1、搭建圖片服務器
2、實現Spring MVC跨服務器方式文件上傳
(1)導入開發需要的jar包
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>1.18.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.18.1</version>
</dependency>
(2)編寫文件上傳的JSP頁面
<h2>Spring MVC跨服務器文件上傳</h2>
<form action="file/fileUpload3" method="post" enctype="multipart/form-data">
選擇文件:<input type="file" name="upload"/><br/>
<input type="submit" value="上傳文件"/>
</form>
(3)編寫文件上傳的Controller控制器
@RequestMapping("/fileUpload3")
public String fileUpload3(MultipartFile upload) throws Exception {
// 定義圖片服務器的請求路徑
String path = "http://localhost:8090/FileUploadServer_war_exploded//uploads/";
// 獲取到上傳文件的名稱
String filename = upload.getOriginalFilename();
// 把文件的名稱設置成唯一值
String uuid = UUID.randomUUID().toString().replace("-", "");
filename = uuid + "_" + filename;
// 向圖片服務器上傳文件
// 創建客戶端對象
Client client = Client.create();
// 連接圖片服務器
WebResource webResource = client.resource(path+filename);
// 上傳文件
webResource.put(upload.getBytes());
return "success";
}
3、常見問題解決方案
【問題1】Connection refused
解決方法:將readonly對應的值修改爲false。
【問題2】returned a response status of 409 Conflict
解決方法:在圖片服務器相應路徑下新建uploads目錄。