您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息

SpringMVC文件上传的方法介绍(代码)

2024/4/24 7:05:05发布4次查看
本篇文章给大家带来的内容是关于springmvc文件上传的方法介绍(代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
springmvc为文件上传提供了直接的支持,这种支持是用即插即用的multipartresolver实现的。springmvc使用apache commons fileupload技术实现了一个multipartresolver实现类:commonsmultipartresolver。因此,springmvc的文件上传还需要依赖apache commons fileupload的组件。
1. 添加pom依赖
<dependency>      <groupid>commons-io</groupid>      <artifactid>commons-io</artifactid>      <version>2.3</version>    </dependency>    <dependency>      <groupid>commons-fileupload</groupid>      <artifactid>commons-fileupload</artifactid>      <version>1.3.2</version>    </dependency>
2. 配置文件上传bean
在spring mvc配置文件中增加一个文件上传bean。
<!-- springmvc上传文件时,需要配置multipartresolver处理器 -->    <bean id="multipartresolver" class="org.springframework.web.multipart.commons.commonsmultipartresolver">        <property name="defaultencoding" value="utf-8" />    </bean>
3.文件上传
文件上传是项目开发中最常见的功能。为了能上传文件,必须将表单的method设置为post,并将enctype设置为multipart/form-data。只有在这样的情况下,浏览器才会把用户选择的文件以二进制数据发送给服务器。
上传文件界面:upload_form.jsp
<%@ page contenttype="text/html;charset=utf-8" language="java" %><html><head>    <title>文件上传</title></head><body><!-- 上传单个对象 注意表单的method属性设为post,enctype属性设为multipart/form-data --><form method="post" action="/springmvcdemo1/upload" enctype="multipart/form-data">    <input type="file" name="file" /><br/><br/>    <input type="submit" value="上传" /></form><!-- 上传多个对象 注意表单的method属性设为post,enctype属性设为multipart/form-data --><form method="post" action="/springmvcdemo1/uploadmultifiles" enctype="multipart/form-data">    <p>文件1:<input type="file" name="file" /></p>    <p>文件2:<input type="file" name="file" /></p>    <p>文件3:<input type="file" name="file" /></p>    <!-- 同时传递其他业务字段 -->    <p>用户名:<input type="text" name="username" /></p>    <p>密码:<input type="password" name="password" /></p>    <p><input type="submit" value="上传" /></p></form></body></html>
上传结果返回界面:upload_result.jsp
<%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%><!doctype><html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"><title>insert title here</title></head><body>    <h2>上传结果为:${message}</h2></body></html>
注意:要提前创建好存储文件的文件夹,比如我的路径为:d:staticresourcestestimgupload。
filecontroller.java
@controller@requestmapping(/springmvcdemo1)public class filecontroller {    /**     * 跳转到上传页面     * @getmapping 是一个组合注解,是@requestmapping(method = requestmethod.get)的缩写。     */    @getmapping(/gotouploadform)    public string index() {        return /upload_form.jsp;    }    /**     * 上传单个文件     * 通过multipartfile读取文件信息,如果文件为空跳转到结果页并给出提示;     * 如果不为空读取文件流并写入到指定目录,最后将结果展示到页面     * @param multipartfile     * @postmapping 是一个组合注解,是@requestmapping(method = requestmethod.post)的缩写。     */    @postmapping(/upload)    public string uploadsinglefile(@requestparam(file) multipartfile multipartfile, httpservletrequest request){        if (multipartfile.isempty()){            request.setattribute(message,  please select a file to upload ');            return /upload_result.jsp;        }        try {            string contenttype = multipartfile.getcontenttype();            string originalfilename = multipartfile.getoriginalfilename();            byte[] bytes = multipartfile.getbytes();            system.out.println(上传文件名为--> + originalfilename);            system.out.println(上传文件类型为--> + contenttype);            system.out.println(上传文件大小为-->+bytes.length);            //filepath为存储路径            string filepath = d:/staticresourcestest;            system.out.println(filepath--> + filepath);            //存储在staticresourcestest下的imgupload文件夹下            file parentpath = new file(filepath, imgupload);            system.out.println(上传目的地为-->+parentpath.getabsolutepath());            try {                file destfile = new file(parentpath,originalfilename);//上传目的地                fileutils.writebytearraytofile(destfile,multipartfile.getbytes());            } catch (exception e) {                e.printstacktrace();            }            request.setattribute(message,  you successfully uploaded ' + multipartfile.getoriginalfilename() + ');        } catch (ioexception e) {            e.printstacktrace();        }        return /upload_result.jsp;    }    /**     * 上传多个文件,同时接受业务数据     * @param origfiles     * @param request     * @param user     * @return     */    @postmapping(/uploadmultifiles)    public string uploadmultifiles(@requestparam(file) list<multipartfile> origfiles, httpservletrequest request, user user) {        //user为实体类        system.out.println(user==+user);        if (origfiles.isempty()) {            request.setattribute(message,  please select a file to upload ');            return /upload_result.jsp;        }        try {            for (multipartfile origfile : origfiles) {                string contenttype = origfile.getcontenttype();                string filename = origfile.getoriginalfilename();                byte[] bytes = origfile.getbytes();                system.out.println(上传文件名为--> + filename);                system.out.println(上传文件类型为--> + contenttype);                system.out.println(上传文件大小为-->+bytes.length);                string filepath = d:/staticresourcestest;                system.out.println(上传目的地为-->+filepath);                try {                    //上传目的地(staticresourcestest文件夹下)                    file destfile = new file(filepath,filename);                    fileutils.writebytearraytofile(destfile,origfile.getbytes());                } catch (exception e) {                    e.printstacktrace();                }            }            request.setattribute(message,  you successfully uploaded ');        } catch (ioexception e) {            e.printstacktrace();        }        return /upload_result.jsp;    }}
本篇文章到这里就已经全部结束了,更多其他精彩内容可以关注的java教程视频栏目!
以上就是springmvc文件上传的方法介绍(代码)的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录