springMVC easyUI filebox 單個檔案上傳
阿新 • • 發佈:2019-02-10
被這個檔案上傳坑到現在,還是自己技術問題,照著之前extjs專案那邊的上傳例項,愣是上傳不了
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));