springMVC easyUI filebox 單個文件上傳
來源:程序員人生 發布時間:2014-09-19 22:50:32 閱讀次數:3183次
被這個文件上傳坑到現在,還是自己技術問題,照著之前extjs項目那邊的上傳實例,愣是上傳不了
到后面就查了下springMVC的文件上傳,按照那樣搞定了http://blog.csdn.net/jadyer/article/details/7575934
easyui 前端文件
<form id="brandAddForm" method="post" enctype="multipart/form-data">
<table cellpadding="5">
<tr>
<td>名稱:</td>
<td><input class="easyui-textbox" type="text" name="name" data-options="required:true" style="width:200px"></input></td>
</tr>
<tr>
<td>描述:</td>
<td><input class="easyui-textbox" name="description" data-options="multiline:true" style="height:100px;width:200px"></input></td>
</tr>
<tr>
<td>URL:</td>
<td><input class="easyui-textbox" name="url" style="width:200px"></td>
</tr>
<tr>
<td>LOGO:</td>
<td><input class="easyui-filebox" name="sourceFile" style="width:200px"></td>
</tr>
<tr>
<td>關鍵詞:</td>
<td><input class="easyui-textbox" type="text" name="keywords" data-options="" style="width:200px"></input></td>
</tr>
</table>
</form>
springMVC 配置文件
<!-- SpringMVC上傳文件時,需要配置MultipartResolver處理器 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"/>
<!-- 指定所上傳文件的總大小不能超過200KB。注意maxUploadSize屬性的限制不是針對單個文件,而是所有文件的容量之和 -->
<property name="maxUploadSize" value="200000"/>
</bean>
<!-- SpringMVC在超出上傳文件限制時,會拋出org.springframework.web.multipart.MaxUploadSizeExceededException -->
<!-- 該異常是SpringMVC在檢查上傳的文件信息時拋出來的,而且此時還沒有進入到Controller方法中 -->
<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<!-- 遇到MaxUploadSizeExceededException異常時,自動跳轉到/WEB-INF/jsp/error_fileupload.jsp頁面 -->
<prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">error_fileupload</prop>
</props>
</property>
</bean>
model
public class BrandModel {
private Integer id;
private String name;
private String description;
private String url;
private String icon;
private MultipartFile sourceFile; //對應<span style="font-family: Arial, Helvetica, sans-serif;"><input class="easyui-filebox" name="sourceFile" style="width:200px"></span>
//set/get
}
service 復制到你自己的目錄
FileUtils.copyInputStreamToFile(brandModel.getSourceFile().getInputStream(), new File(targetPath));
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈