Spring MVC在任何應用程序中提供了對文件上傳功能的支持。本教程使用org.springframework.web.multipart.commons.CommonsMultipartResolver
並要求apache commons fileupload和apache commons io依賴關係。
上傳到Spring MVC應用程序的文件將被包裝在一個MultipartFile對象中。所有你需要做的就是編寫一個類型屬性的域類MultipartFile
。該接口具有獲取名稱和上傳文件例如內容的方法getBytes()
,getInputStream()
,getOriginalFilename()
, getSize()
,isEmpty()
和tranferTo()
。
例如,要將上傳的文件保存到文件系統,可以使用transferTo方法:
File file =
new
File(...);
multipartFile.transferTo(file);
|
您需要創建一個具有必要屬性的簡單域類,另一個用於存儲類型的文件List<MultipartFile>
。
爲了建立這個例子,我寫了這個域的clas:
public
class
Product
implements
Serializable
{
private
static
final
long
serialVersionUID = 74458L;
@NotNull
@Size
(min=
1
, max=
10
)
private
String name;
private
String description;
private
List<MultipartFile> images;
public
String getName() {
return
name;
}
public
void
setName(String name) {
this
.name = name;
}
public
String getDescription() {
return
description;
}
public
void
setDescription(String description) {
this
.description = description;
}
public
List<MultipartFile> getImages() {
return
images;
}
public
void
setImages(List<MultipartFile> images) {
this
.images = images;
}
}
|
在控制器類中,您將獲得域類中上傳文件的預填充詳細信息。只需獲取詳細信息並按照應用程序設計將文件存儲在文件系統或數據庫中即可。
@Controller
public
class
DemoProductController
{
@RequestMapping
(
"/save-product"
)
public
String uploadResources( HttpServletRequest servletRequest,
@ModelAttribute
Product product,
Model model)
{
//Get the uploaded files and store them
List<MultipartFile> files = product.getImages();
List<String> fileNames =
new
ArrayList<String>();
if
(
null
!= files && files.size() >
0
)
{
for
(MultipartFile multipartFile : files) {
String fileName = multipartFile.getOriginalFilename();
fileNames.add(fileName);
File imageFile =
new
File(servletRequest.getServletContext().getRealPath(
"/image"
), fileName);
try
{
multipartFile.transferTo(imageFile);
}
catch
(IOException e)
{
e.printStackTrace();
}
}
}
// Here, you can save the product details in database
model.addAttribute(
"product"
, product);
return
"viewProductDetail"
;
}
@RequestMapping
(value =
"/product-input-form"
)
public
String inputProduct(Model model) {
model.addAttribute(
"product"
,
new
Product());
return
"productForm"
;
}
}
|
爲了支持多部分的請求,你需要在配置文件中聲明下面的bean。
<bean id=
"multipartResolver"
class
=
"org.springframework.web.multipart.commons.CommonsMultipartResolver"
>
<property name=
"maxUploadSize"
value=
"20848820"
/>
</bean>
|
另外,您可能希望將服務器上的文件存儲路徑映射爲資源。
<mvc:resources mapping=
"/image/**"
location=
"/image/"
/>
|
用於這個例子的完整配置文件是:
<beans xmlns=
"http://www.springframework.org/schema/beans"
xsi:schemaLocation="http:
//www.springframework.org/schema/beans
http:
//www.springframework.org/schema/beans/spring-beans-3.0.xsd
http:
//www.springframework.org/schema/context/
http:
//www.springframework.org/schema/context/spring-context-3.0.xsd
http:
//www.springframework.org/schema/mvc
http:
//www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-
package
=
"com.howtodoinjava.demo"
/>
<mvc:resources mapping=
"/image/**"
location=
"/image/"
/>
"com.howtodoinjava.demo"
/>
<mvc:resources mapping=
"/image/**"
location=
"/image/"
/>
<bean
|