本文主要介紹在SpringMVC環境下實現文件上傳的方式,是對自己工作中的經驗的一點總結,第一次寫博文,還請大家多多包涵。
環境篇
本次使用的環境是Maven+SpringMVC+MyBatis+Postgresql,因爲只是個單純的文件上傳,本文並沒有涉及數據庫,MyBatis+Postgresql方面不會涉及。
Maven
Maven環境的搭建此處不再贅述,其他博文都有很詳細的說明,這裏主要說明一下在SpringMVC中需要添加的依賴,如下所示。
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.2</version>
</dependency>
這裏需要注意的一點是,commons-fileupload這個jar包的1.0版本是包含commons-io這個jar包的,因此,如果使用的是commons-fileupload的1.0版本,不用引用commons-io的jar包
SpringMVC
SpringMVC的具體搭建過程此處也不再過多贅述,主要說明一下爲了實現文件上傳所需要加的配置。
爲了實現文件上傳,需要在springContext.xml中加入如下的內容
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="104857600" />
<property name="maxInMemorySize" value="4096" />
<property name="defaultEncoding" value="UTF-8"></property>
</bean>
這裏加的bean主要是問了接收頁面傳過來的文件對象,在這個配置裏面設置了上傳文件最大值以及文字編碼。
代碼篇
本篇主要介紹文件上傳這個功能的從後臺到前臺的代碼實現,將會在下面的篇幅中一次介紹。
JSP
首先來看JSP上的實現,具體代碼如下。
<form action="${pageContext.request.contextPath}/upload" method="post" enctype="multipart/form-data">
<table>
<tr>
<td>ID:</td>
<td><input type="text" name="id"></td>
</tr>
<c:forEach items ="${bean.fileList}" var = "file" varStatus="status">
<tr>
<td>filePath"${status.index}":</td>
<td><input type="file" name="fileList[${status.index}]"></td>
</tr>
</c:forEach>
<tr>
<td colspan="2"><input type="submit" value="insert"></td>
</tr>
</table>
</form>
這裏只貼出了form表單部分,其他部分沒有貼出。
JSP中需要着重注意的一點是,form標籤中的enctype屬性必不可少,這個屬性如果不添加的話,後臺只能接收到文件名,具體文件內容是接收不到的。
Controller
下面將貼出Controller類的代碼。
@RequestMapping("/fileupload")
public ModelAndView showMessage() {
//設置頁面現實的內容,此部分內容可以從數據庫讀取,此處是隨便設置的值
FileDataContext bean = new FileDataContext();
bean.setId("testId");
List<CommonsMultipartFile> tempList = new ArrayList<CommonsMultipartFile>();
for (int i = 0; i < 2; i++) {
tempList.add(null);
}
bean.setFileList(tempList);
ModelAndView mv = new ModelAndView("fileupload");// 指定視圖
// 向視圖中添加所要展示或使用的內容,將在頁面中使用
mv.addObject("bean", bean);
return mv;
}
@RequestMapping("/upload")
public ModelAndView uploadFile(HttpServletRequest request, FileDataContext bean) {
//這部分的控制檯輸出是DEBUG用的,實際代碼中可不寫
System.out.println(bean.getId());
System.out.println(bean.getFileList().get(0).getOriginalFilename());
System.out.println(request.getCharacterEncoding());
File tempFile;
//這部分循環是循環上傳文件至服務器
try {
for (int i = 0; i < bean.getFileList().size(); i++) {
//上傳的服務器地址
String path = "/Users/skylia/Downloads/" + new Date().getTime()
+ bean.getFileList().get(i).getOriginalFilename();
//生成新文件
tempFile = new File(path);
//上傳文件數據
bean.getFileList().get(i).transferTo(tempFile);
}
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//指定視圖
ModelAndView mv = new ModelAndView("fileupload");
//添加視圖上所要顯示的內容
mv.addObject("bean", bean);
return mv;
}
這部分的Controller類的代碼中,我將分別說明這兩個方法
第一個方法showMessage
這個方法是進入這個頁面時所調用的方法,主要是設置初期表示時,想要在頁面中顯示的值,這個值可以從前一個頁面傳過來,也可以從數據庫讀取,請根據自己的需要修改值的類型和來源。
第二個方法uploadFile
這個方法實現了文件上傳,在DataBean中接收文件時需要使用multipart提供的文件類。
實際上,實現文件上傳的方法有三種。
1.採用流的方式上傳文件
2.採用multipart提供的file.transfer方法上傳文件
3.使用spring mvc提供的類的方法上傳文件
此處使用的是第二種方法,因爲第二種方法經測試是效率最高的。
DataBean
下面貼出DataBean的相關代碼
public class FileDataContext {
//普通類型的值
private String id;
//文件類型的值
private List<CommonsMultipartFile> fileList;
private int size;
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public List<CommonsMultipartFile> getFileList() {
return fileList;
}
public void setFileList(List<CommonsMultipartFile> fileList) {
this.fileList = fileList;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
這個Bean是用來承接頁面內容的,在這裏需要注意的一點是CommonsMultipartFile這個類型。
這個類型是multipart提供的用來承接文件數據的類,請務必將此類型的參數對應到jsp頁面上的file輸入框(即type=file的input標籤)。這樣的話就可以收到頁面上傳來的客戶端的文件數據了。
結束語
至此,文件上傳的功能就實現了,大家可以根據自己的需要,再添加更新數據庫等功能,謝謝閱讀!
希望大家可以共同進步,如果有什麼寫的錯誤的地方,請指正,謝謝!