欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringMVC实现文件上传和下载功能

程序员文章站 2024-02-22 12:24:52
本文实例为大家分享了android九宫格图片展示的具体代码,供大家参考,具体内容如下 文件上传  第一步,加入jar包: commons-fileupload-1...

本文实例为大家分享了android九宫格图片展示的具体代码,供大家参考,具体内容如下

文件上传 

第一步,加入jar包:

commons-fileupload-1.3.1.jar
commons-io-2.4.jar

第二步,在springmvc配置文件中配置commonsmultipartresovler

<bean id="multipartresolver" class="org.springframework.web.multipart.commons.commonsmultipartresolver">
 <property name="defaultencoding" value="utf-8"></property>
  //最大上传文件大小
 <property name="maxuploadsize" value="1048576"></property>
</bean> 

第三步,前端表单   注意 【post请求,file类型,enctype="multipart/form-data"】

<form action="${pagecontext.request.contextpath }/testupload" method="post" enctype="multipart/form-data">
 file:<input type="file" name="file"><br>
 desc:<input type="text" name="desc"><br>
 <input type="submit" value="submit"><br>
</form><br>

第四步,在controller层创建方法

@requestmapping(value="/testupload",method=requestmethod.post)
    private string testupload(httpservletrequest request,@requestparam(value="desc")string desc,@requestparam(value="file") commonsmultipartfile file) {

    inputstream inputstream = null;
    outputstream outputstream = null;
    servletcontext servletcontext = request.getservletcontext();
    //获取文件存放的真实路径
    string realpath = servletcontext.getrealpath("/upload");
    //为了避免多次上传同一个文件导致命名重复,在文件名前加uuid前缀
    string prefix=uuid.randomuuid().tostring();
    prefix=prefix.replace("-", "");
    string filename=prefix+"_"+file.getoriginalfilename();
 
    file file2=new file(realpath);
    //检查文件目录是否存在,若不存在就创建目录
    if(!file2.exists()){
    file2.mkdirs();
    }
 
    try {
     inputstream=file.getinputstream();
    outputstream=new fileoutputstream(new file(realpath+"/"+filename));
     //设置缓冲区
     byte[]buffer=new byte[1024];
     int len=0;
     //循环检测文件是否上传完成,未完成就向写入输出流
     while((len=inputstream.read(buffer)) != -1){
     outputstream.write(buffer, 0, len);
     outputstream.flush();
     }
    } catch (ioexception e) {
    // todo auto-generated catch block
    e.printstacktrace();
    } finally{
     //关闭输入输出流
     if(outputstream !=null){
     try {
     outputstream.close();
     inputstream.close();
     } catch (ioexception e) {
     // todo auto-generated catch block
     e.printstacktrace();
     }
     }
   }
 
 return "success";
 }

 文件下载 

用responseentity<byte[]> 返回值完成文件下载;在jsp页面给出链接即可。

jsp页面链接地址:

复制代码 代码如下:
<a href="${pagecontext.request.contextpath }/testresponseentity" rel="external nofollow" >下载链接</a> 

在controller层创建方法

@requestmapping(value="/testresponseentity")
 responseentity<byte[]>testresponseentity(httpservletrequest request)throws exception{
 servletcontext servletcontext = request.getservletcontext();
 //获取要下载的文件的文件名
 string filename="喜剧之王.mp3";
 //获取要下载的文件的真实路径
 string realpath = servletcontext.getrealpath("/web-inf/"+filename);
 //创建输入流
 inputstream inputstream=new fileinputstream(new file(realpath));
 
 byte[]body=new byte[inputstream.available()];
 inputstream.read(body);
 multivaluemap<string, string>headers=new httpheaders();
 
 //设置头信息和字符集
 filename = new string(filename.getbytes("gbk"),"iso8859-1");
 headers.set("content-disposition", "attachment;filename="+filename);
 
 httpstatus statuscode = httpstatus.ok;
 responseentity<byte[]>responseentity =new responseentity<byte[]>(body, headers, statuscode);
 
 return responseentity;
 }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。