您好,欢迎访问三七文档
当前位置:首页 > 办公文档 > 其它办公文档 > SpringMVCfileuploadexample中英对照
——疯狂大白菜1/11SpringMVCfileuploadexampleSpringMVC文件上传示例文件上传示例文件上传示例文件上传示例SpringcomeswithMultipartResolverMultipartResolverMultipartResolverMultipartResolvertohandlethefileuploadinwebapplication.TheCommonsMultipartResolverCommonsMultipartResolverCommonsMultipartResolverCommonsMultipartResolverisacommonMultipartResolverimplementation,whichusetheApachecommonsuploadlibrarytohandlethefileuploadinaform.Inthistutorial,itshowshowtohandlethefileuploadinSpringMVCwebapplication.在web应用中,使用Spring和MultipartResolverMultipartResolverMultipartResolverMultipartResolver一起配合来处理上传文件。CommonsMultipartResolverCommonsMultipartResolverCommonsMultipartResolverCommonsMultipartResolver是普通的MultipartResolverMultipartResolverMultipartResolverMultipartResolver接口,使用Apache上传库来处理在表单中上传文件。在本文中,它将展示在SpringMVC框架web应用中如何处理上传文件。1.1.1.1.FileUploadDependencyFileUploadDependencyFileUploadDependencyFileUploadDependency文件上传依赖TouseCommonsMultipartResolverCommonsMultipartResolverCommonsMultipartResolverCommonsMultipartResolvertohandlethefileupload,youneedtogetthecommonscommonscommonscommons----fileupload.jarfileupload.jarfileupload.jarfileupload.jarandcommonscommonscommonscommons----io.jario.jario.jario.jarlibraries.为了使用CommonsMultipartResolverCommonsMultipartResolverCommonsMultipartResolverCommonsMultipartResolver来处理文件上传,必须要得到commonscommonscommonscommons----fileupload.jarfileupload.jarfileupload.jarfileupload.jar和commonscommonscommonscommons----io.jio.jio.jio.jarararar库。!--Springframework--dependencygroupIdorg.springframework/groupIdartifactIdspring/artifactIdversion2.5.6/version/dependency!--SpringMVCframework--dependency——疯狂大白菜2/11groupIdorg.springframework/groupIdartifactIdspring-webmvc/artifactIdversion2.5.6/version/dependency!--ApacheCommonsUpload--dependencygroupIdcommons-fileupload/groupIdartifactIdcommons-fileupload/artifactIdversion1.2.2/version/dependency!--ApacheCommonsUpload--dependencygroupIdcommons-io/groupIdartifactIdcommons-io/artifactIdversion1.3.2/version/dependency!--JSTL--dependencygroupIdjavax.servlet/groupIdartifactIdjstl/artifactIdversion1.1.2/version——疯狂大白菜3/11/dependencydependencygroupIdtaglibs/groupIdartifactIdstandard/artifactIdversion1.1.2/version/dependency2.2.2.2.ModelModelModelModel模型CreateaMultipartFileMultipartFileMultipartFileMultipartFilevariabletostoretheuploadedfile.Alternatively,youcanusethebyte[]byte[]byte[]byte[]tostoreit,butimoreprefertousetheMultipartFileMultipartFileMultipartFileMultipartFile,becauseitcangettheuploadedfiledetail(filename,filesize…)easily.创建一个MultipartFile变量用来存储上传文件。二者选一,你可以选择使用byte[]来存储它,但我更喜欢使用MultipartFile,因为它可以更容易得到上传文件的详细信息(文件名,文件大小…)packagecom.mkyong.common.model;importorg.springframework.web.multipart.MultipartFile;publicclassFileUpload{MultipartFilefile;//getterandsettermethods}3.3.3.3.FileUploadControllerFileUploadControllerFileUploadControllerFileUploadController文件上传控制ExtendstheSimpleFormControllerandhandlethefileuploadformlikeanormalform.扩展SimpleFormController并且使处理文件上传表单想普通的表单。——疯狂大白菜4/11FileUploadController.javaFileUploadController.javaFileUploadController.javaFileUploadController.javapackagecom.mkyong.common.controller;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importorg.springframework.validation.BindException;importorg.springframework.web.multipart.MultipartFile;importorg.springframework.web.servlet.ModelAndView;importorg.springframework.web.servlet.mvc.SimpleFormController;importcom.mkyong.common.model.FileUpload;publicclassFileUploadControllerextendsSimpleFormController{publicFileUploadController(){setCommandClass(FileUpload.class);setCommandName(fileUploadForm);}@OverrideprotectedModelAndViewonSubmit(HttpServletRequestrequest,HttpServletResponseresponse,Objectcommand,BindExceptionerrors)throwsException{FileUploadfile=(FileUpload)command;MultipartFilemultipartFile=file.getFile();StringfileName=;if(multipartFile!=null){fileName=multipartFile.getOriginalFilename();//dowhateveryouwant}returnnewModelAndView(FileUploadSuccess,fileName,fileName);}}NoteNoteNoteNote注意注意注意注意Ifyouareusingthebyte[]byte[]byte[]byte[]tostoretheuploadedfile,youhavetoregistertheByteArrayMultipartFileEditorByteArrayMultipartFileEditorByteArrayMultipartFileEditorByteArrayMultipartFileEditorclasstoguideSpringtohandletheconversionbetweenthemultipartobjectandbytearray.——疯狂大白菜5/11如果你使用byte[]来存储上传的文件,你必须注册ByteArrayMultipartFileEditor类来向导Spring处理多部件对象(multipartobject)和比特数组(bytearray)之间的转换。publicclassFileUploadControllerextendsSimpleFormController{//...@OverrideprotectedvoidinitBinder(HttpServletRequestrequest,ServletRequestDataBinderbinder)throwsServletException{//Convertmultipartobjecttobyte[]binder.registerCustomEditor(byte[].class,newByteArrayMultipartFileEditor());}4.4.4.4.FileUploadVal
本文标题:SpringMVCfileuploadexample中英对照
链接地址:https://www.777doc.com/doc-12610 .html