Java Spring MVC 上传下载文件配置及controller方法详解
程序员文章站
2024-03-13 11:40:15
下载:
1.在spring-mvc中配置(用于100m以下的文件下载)
下载:
1.在spring-mvc中配置(用于100m以下的文件下载)
<bean class="org.springframework.web.servlet.mvc.annotation.annotationmethodhandleradapter"> <property name="messageconverters"> <list> <!--配置下载返回类型--> <bean class="org.springframework.http.converter.bytearrayhttpmessageconverter"/> <bean class="org.springframework.http.converter.stringhttpmessageconverter"> <!--配置编码方式--> <property name="supportedmediatypes" value="application/json; charset=utf-8" /> </bean> </list> </property> </bean>
下载文件代码
@requestmapping("/file/{name.rp}") public responseentity<byte[]> filedownload(@pathvariable("name.rp")string name, httpservletrequest request,httpservletresponse response) { // @pathvariable string name, // @requestparam("name")string name, // system.out.println("<name>"+name); // system.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); responseentity<byte[]> re = null; try { /** * css,js,json,gif,png,bmp,jpg,ico,doc,docx,xls,xlsx,txt,swf,pdf * **/ //下载防止静态加载干扰 feelutile f=new feelutile(); name=f.getfileformat(name); string pathstring="c:\\tempdirectory\\"+name; file file=new file(pathstring); httpheaders headers=new httpheaders(); //string filename=urlencoder.encode(name, "utf-8");//为了解决中文名称乱码问题 string filename=new string(name.getbytes("utf-8"),"utf-8"); byte[] by=fileutils.readfiletobytearray(file); headers.setcontenttype(mediatype.application_octet_stream); //urlencoder.encode(filename, "utf-8") headers.setcontentdispositionformdata("attachment",filename); headers.setcontentlength(by.length); re=new responseentity<byte[]>(by, headers, httpstatus.created); } catch (exception e) { e.printstacktrace(); try { request.getrequestdispatcher("/error/404.jsp").forward(request, response); } catch (servletexception e1) { // todo auto-generated catch block e1.printstacktrace(); } catch (ioexception e1) { // todo auto-generated catch block e1.printstacktrace(); } } return re; }
上传文件:
1在spring-mvc中配置
<!--4.文件上传 配置 file upload --> <bean id="multipartresolver" class="org.springframework.web.multipart.commons.commonsmultipartresolver"> <property name="defaultencoding"> <value>utf-8</value> </property> <property name="maxuploadsize"> <value>1048576000</value> </property> <property name="maxinmemorysize"> <value>40960</value> </property> </bean>
在controller中代码如下
@requestmapping(value="/upload", method = requestmethod.post) @responsebody public json upload(doc doc, @requestparam("uploadfile") commonsmultipartfile file) { json j = new json(); try { string realpath = this.servletcontext.getrealpath("/upload"); string uploadfilefilename = file.getoriginalfilename(); string uploadfilefilenamewithoutspace = uploadfilefilename.replaceall(" ", ""); string filetype = uploadfilefilenamewithoutspace.substring(uploadfilefilenamewithoutspace.lastindexof(".")); file targetfile = new file(realpath+file.separator, uploadfilefilenamewithoutspace); if (targetfile.exists()) { targetfile.delete(); } file.getfileitem().write(targetfile); docservice.upload(doc,uploadfilefilenamewithoutspace); j.setsuccess(true); j.setmsg("upload manual successfully"); }catch (exception e) { logger.error(exceptionutil.getexceptionmessage(e)); j.setmsg("upload manual unsuccessfully"); } return j; }
以上所述是小编给大家介绍的java spring mvc 上传下载文件配置及controller方法详解,希望对大家有所帮助
上一篇: Java创建文件且写入内容的方法
下一篇: java 反射和动态代理详解及实例代码