admin管理员组

文章数量:1642473

超详细的文件上传和下载

前言Ⅰ:@RequestParam和@RequestPart的区别

@RequestPart

  • @RequestPart这个注解用在multipart/form-data表单提交请求的方法上。
  • 支持的请求方法的方式MultipartFile,属于Spring的MultipartResolver类。这个请求是通过http协议传输的

@RequestParam

  • @RequestParam支持application/json,也同样支持multipart/form-data请求

代码演示

  • 测试一:使用@RequestPart接收json数据

    结论@RequestPart可以将jsonDatajson数据转换为Person对象

    /**
         * 测试 RequestParam 和 RequestPart区别2
         * @param person
         * @param file1
         * @return
         * @throws IOException
         * @throws ServletException
         */
    @RequestMapping(value = "/upload3",method = RequestMethod.POST)
    @ResponseBody
    public String readFile1(@RequestPart("person") Person person , @RequestPart("file1") MultipartFile file1)  {
        StringBuilder sb = new StringBuilder();
        sb.append(file1.getOriginalFilename()).append(";;;");
        return person.toString() + ":::" + sb.toString();
    }
    
    参数名参数值参数类型
    file1一个文件file
    person{“name”:“cvzhanshi”,“age”:23}application/json

    测试结果:能够用对象去接收json

  • 测试二:使用@RequestParam接收json数据

    结论@RequestParam对于jsonDatajson数据只能用String字符串来接收

    /**
         * 测试 RequestParam 和 RequestPart区别2
         * @param person
         * @param file1
         * @return
         * @throws IOException
         * @throws ServletException
         */
    @RequestMapping(value = "/upload3",method = RequestMethod.POST)
    @ResponseBody
    public String readFile1(@RequestParam("person") Person person , @RequestPart("file1") MultipartFile file1)  {
        StringBuilder sb = new StringBuilder();
        sb.append(file1.getOriginalFilename()).append(";;;");
        return person.toString() + ":::" + sb.toString();
    }
    

​ 测试结果:报错 2022-10-08 10:19:48.434 WARN 3440 — [nio-8888-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.method.annotation.MethodArgumentConversionNotSupportedException: Failed to convert value of type ‘java.lang.String’ to required type ‘cn.cvzhanshi.test.entity.Person’; nested exception is java.lang.IllegalStateException: Cannot convert value of type ‘java.lang.String’ to required type ‘cn.cvzhanshi.test.entity.Person’: no matching editors or conversion strategy found]

报错说明:只能接收解析String类型的

区别(总结)

  • 当请求为multipart/form-data时,@RequestParam只能接收String类型name-value值,@RequestPart可以接收复杂的请求域(像json、xml
  • @RequestParam 依赖Converter or PropertyEditor进行数据解析, @RequestPart参考'Content-Type' header,依赖HttpMessageConverters进行数据解析
  • 当请求头中指定Content-Type:multipart/form-data时,传递的json参数,@RequestPart注解可以用对象来接收,@RequestParam只能用字符串接收
  • @RequestParam适用于name-valueString类型的请求域,@RequestPart适用于复杂的请求域(像JSON,XML);他们最大的不同是,当请求方法的请求参数类型不再是String类型的时候

前言Ⅱ:getParameter() 和 getPart() 区别

代码测试

/**
     * 测试 RequestParam 和 RequestPart区别1
     * @param request
     * @param name
     * @param file1
     * @return
     * @throws IOException
     * @throws ServletException
     */
@RequestMapping(value = "/upload1",method = RequestMethod.POST)
@ResponseBody
public String readFile(HttpServletRequest request,@RequestParam("name") String name ,@RequestPart("file1") MultipartFile file1) throws IOException, ServletException {

    String ageParameter = request.getParameter("age");
    Part agePart = request.getPart("age");

    String file2Parameter = request.getParameter("file2");
    Part file2Part = request.getPart("file2");

    Enumeration<String> parameterNames = request.getParameterNames();
    Collection<Part> parts = request.getParts();
    return "";
}

传递参数

keyvaluetype
namecvzhanshitext
age18text
file1新建文本文档.txtfile
file2美女.jpgfile

DeBug分析

  • request:可以看到request有两个 Parameter 和 两个 Part 参数

  • Parameter和Part:request.getParameter获得的值以及request.getPart获得的值

    结果发现:使用 request.getParameter() 获取文件类型参数为空,使用 request.getPart() 获取非文件类型为空

  • ParameterNames和Parts:getParameterNames()和getParts()获取参数名

    结果:使用 request.getParameterNames() 只能获取非文件类型的参数名, 使用 request.getParts() 可以获取全部参数名

结论

  • Part 能获取所有的请求参数的参数名,而 Parameter 只能获取非文件类型的参数名
  • Part 不能获得非文件类型参数的参数值,Part获得二进制的输入流
  • Parameter 只能获得非文件类型参数值
  • 使用 Part 获取文件很方便,可以获取文件大小文件类型

Ⅰ:文件上传

① form-data 类型上传

form-data 类型即常用的表单提交,可以通过两种方式接收前端传过来的文件

  • 通过上述讲的Part 接收字节流,即@RequestPart注解以及request.getParts()
  • 通过MultipartFile类来接收前端传过来的文件

说明:两种方法差不多

前端接口提交

上传文件的后端代码(接口)

/**
     * form-data 类型(前端表单上传)
     * @param request
     * @param name
     * @param file3
     * @param photo
     * @return
     * @throws IOException
     * @throws ServletException
     */
@RequestMapping(value = "/upload", method = RequestMethod.POST)
@ResponseBody
public String readFile(HttpServletRequest request,
                       @RequestParam("name") String name,
                       @RequestPart("file1") MultipartFile file3,
                       @RequestPart("photo") MultipartFile photo
                      ) throws IOException, ServletException {



    System.out.println(name);
    /*
            第一种 : 使用 MultipartFile 封装好的 transferTo() 方法保存文件
            photo.transferTo(new File(path + photo.getOriginalFilename()));
         */
    /*
            第二种 :  使用 MultipartFile 字节流保存文件
            fileUtil(file3, String.valueOf(path));
         */


    /*第三种 :用 Part 接收文件字节流
            Part file2 = request.getPart("file2");
            file2.write(path + file2.getSubmittedFileName());
            request.getParts() 获取的是全部参数(name,age,file1,file2),包括文件参数和非文件参数
		 */
    for (Part part : request.getParts()) {
        // 获取文件类型
        part.getContentType();
        // 获取文件大小
        part.getSize();
        // 获取文件名
        part.getSubmittedFileName();
        // 获取参数名 (name,age,file1,file2)
        part.getName();
        if(part.getContentType()!=null){
            part.write(path + part.getSubmittedFileName());
        }else{
            // 可以获取文本参数值,文本参数 part.getContentType()为 null
            System.out.println(request.getParameter(part.getName()));
        }
    }
    return "success";
}


/**
     * 字节流上传文件工具方法
     * @param file
     * @param path
     * @return
     */
public String fileUtil(MultipartFile file, String path) {
    if (!file.isEmpty()) {
        try {
            // 转化成字节流
            byte[] bytes = file.getBytes();
            bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(
                new File(path + file.getOriginalFilename())));
            // 写出
            bufferedOutputStream.write(bytes);
            // 关闭资源
            bufferedOutputStream.close();
            return file.getOriginalFilename() + " 上传成功";
        } catch (Exception e) {
            return file.getOriginalFilename() + " failed to upload ---> " + e;
        }
    } else {
        return file.getOriginalFilename() + "You failed to upload file was empty.";
    }
}

测试

② binary 类型上传

binary 这一类型,指的就是一些二进制文件类型,如application/pdf,指定了特定二进制文件的MIME类型。就像对于text文件类型若没有特定的子类型

(subtype),就使用 text/plain。类似的,二进制文件没有特定或已知的 subtype,即使用 application/octet-stream,这是应用程序文件的默认值。

对于application/octet-stream,只能提交二进制,而且只能提交一个二进制,如果提交文件的话,只能提交一个文件,后台接收参数只能有一个,而且只能是流

(或者字节数组)

前端接口提交

上传文件的后端代码(接口)

/**
     * binary 类型上传
     * @param request
     * @return
     * @throws IOException
     */
@RequestMapping(value = "/upload2",method = RequestMethod.POST)
public String upload2(HttpServletRequest request) throws IOException {
    ServletInputStream inputStream = null;
    FileOutputStream fileOutputStream = null;
    try {
        // 获取上传的文件流
        inputStream = request.getInputStream();
        // 定义输出流 上传到哪里
        fileOutputStream = new FileOutputStream(new File(path + "a.pdf"));

        int len;
        byte[] bytes = new byte[1024];
        while((len = inputStream.read(bytes))!=-1){
            fileOutputStream.write(bytes,0,len);
        }
    } catch (IOException e) {
        e.printStackTrace();
        return "上传失败";
    } finally {
        // 关闭资源
        if(fileOutputStream!=null){
            fileOutputStream.close();
        }
        if(inputStream!=null){
            inputStream.close();
        }
    }
    return "上传成功";
}

测试

说明:part 使用起来比较方便,接受的就是字节流,读取文件类型,文件名,文件大小也比较方便

③ 配置说明

如果不自定义配置的话,他会默认上传大小1M\

SpringBoot项目的话通过配置文件配置

server:
  port: 8888
spring:
  servlet:
    multipart:
      max-file-size: 10MB   # 最大上传大小
      max-request-size: 100MB   # 单文件最大上传大小 

Ⅱ:文件下载

① 补充说明

Content-Disposition
  • 在常规的 HTTP 应答中,Content-Disposition 响应头指示回复的内容该以何种形式展示,是以内联的形式(即网页或者页面的一部分),还是以附件的形式下载并保存到本地
  • 在 multipart/form-data 类型的应答消息体中,Content-Disposition 消息头可以被用在 multipart 消息体的子部分中,用来给出其对应字段的相关信息。各个子部分由在Content-Type 中定义的分隔符分隔。用在消息体自身则无实际意义。

语法

  • 作为消息主体中的消息头:在 HTTP 场景中,第一个参数或者是 inline(默认值,表示回复中的消息体会以页面的一部分或者整个页面的形式展示),或者是 attachment(意味着消息体应该被下载到本地;大多数浏览器会呈现一个“保存为”的对话框,将 filename 的值预填为下载后的文件名,假如它存在的话)。

    Content-Disposition: inline
    Content-Disposition: attachment
    Content-Disposition: attachment; filename="filename.jpg"
    
  • 作为 multipart body 中的消息头:在 HTTP 场景中。第一个参数总是固定不变的 form-data;附加的参数不区分大小写,并且拥有参数值,参数名与参数值用等号 ('=') 连接,参数值用双引号括起来。参数之间用分号 (';') 分隔。

    Content-Disposition: form-data
    Content-Disposition: form-data; name="fieldName"
    Content-Disposition: form-data; name="fieldName"; filename="filename.jpg"
    
response.setContentType()的String参数及对应类型
文件扩展名Content-Type(Mime-Type)文件扩展名Content-Type(Mime-Type)
.*( 二进制流,不知道下载文件类型)application/octet-stream.tifimage/tiff
.001application/x-001.301application/x-301
.323text/h323.906application/x-906
.907drawing/907.a11application/x-a11
.acpaudio/x-mei-aac.aiapplication/postscript
.aifaudio/aiff.aifcaudio/aiff
.aiffaudio/aiff.anvapplication/x-anv
.asatext/asa.asfvideo/x-ms-asf
.asptext/asp.asxvideo/x-ms-asf
.auaudio/basic.avivideo/avi
.awfapplication/vnd.adobe.workflow.biztext/xml
.bmpapplication/x-bmp.botapplication/x-bot
.c4tapplication/x-c4t.c90application/x-c90
.calapplication/x-cals.catapplication/vnd.ms-pki.seccat
.cdfapplication/x-netcdf.cdrapplication/x-cdr
.celapplication/x-cel.cerapplication/x-x509-ca-cert
.cg4application/x-g4.cgmapplication/x-cgm
.citapplication/x-cit.classjava/*
.cmltext/xml.cmpapplication/x-cmp
.cmxapplication/x-cmx.cotapplication/x-cot
.crlapplication/pkix-crl.crtapplication/x-x509-ca-cert
.csiapplication/x-csi.csstext/css
.cutapplication/x-cut.dbfapplication/x-dbf
.dbmapplication/x-dbm.dbxapplication/x-dbx
.dcdtext/xml.dcxapplication/x-dcx
.derapplication/x-x509-ca-cert.dgnapplication/x-dgn
.dibapplication/x-dib.dllapplication/x-msdownload
.docapplication/msword.dotapplication/msword
.drwapplication/x-drw.dtdtext/xml
.dwfModel/vnd.dwf.dwfapplication/x-dwf
.dwgapplication/x-dwg.dxbapplication/x-dxb
.dxfapplication/x-dxf.ednapplication/vnd.adobe.edn
.emfapplication/x-emf.emlmessage/rfc822
.enttext/xml.epiapplication/x-epi
.epsapplication/x-ps.epsapplication/postscript
.etdapplication/x-ebx.exeapplication/x-msdownload
.faximage/fax.fdfapplication/vnd.fdf
.fifapplication/fractals.fotext/xml
.frmapplication/x-frm.g4application/x-g4
.gbrapplication/x-gbr.application/x-
.gifimage/gif.gl2application/x-gl2
.gp4application/x-gp4.hglapplication/x-hgl
.hmrapplication/x-hmr.hpgapplication/x-hpgl
.hplapplication/x-hpl.hqxapplication/mac-binhex40
.hrfapplication/x-hrf.htaapplication/hta
.htctext/x-component.htmtext/html
.htmltext/html.htttext/webviewhtml
.htxtext/html.icbapplication/x-icb
.icoimage/x-icon.icoapplication/x-ico
.iffapplication/x-iff.ig4application/x-g4
.igsapplication/x-igs.iiiapplication/x-iphone
.imgapplication/x-img.insapplication/x-internet-signup
.ispapplication/x-internet-signup.IVFvideo/x-ivf
.javajava/*.jfifimage/jpeg
.jpeimage/jpeg.jpeapplication/x-jpe
.jpegimage/jpeg.jpgimage/jpeg
.jpgapplication/x-jpg.jsapplication/x-javascript
.jsptext/html.la1audio/x-liquid-file
.larapplication/x-laplayer-reg.latexapplication/x-latex
.lavsaudio/x-liquid-secure.lbmapplication/x-lbm
.lmsffaudio/x-la-lms.lsapplication/x-javascript
.ltrapplication/x-ltr.m1vvideo/x-mpeg
.m2vvideo/x-mpeg.m3uaudio/mpegurl
.m4evideo/mpeg4.macapplication/x-mac
.manapplication/x-troff-man.mathtext/xml
.mdbapplication/msaccess.mdbapplication/x-mdb
.mfpapplication/x-shockwave-flash.mhtmessage/rfc822
.mhtmlmessage/rfc822.miapplication/x-mi
.midaudio/mid.midiaudio/mid
.milapplication/x-mil.mmltext/xml
.mndaudio/x-musicnet-download.mnsaudio/x-musicnet-stream
.mochaapplication/x-javascript.movievideo/x-sgi-movie
.mp1audio/mp1.mp2audio/mp2
.mp2vvideo/mpeg.mp3audio/mp3
.mp4video/mpeg4.mpavideo/x-mpg
.mpdapplication/vnd.ms-project.mpevideo/x-mpeg
.mpegvideo/mpg.mpgvideo/mpg
.mpgaaudio/rn-mpeg.mppapplication/vnd.ms-project
.mpsvideo/x-mpeg.mptapplication/vnd.ms-project
.mpvvideo/mpg.mpv2video/mpeg
.mpwapplication/vnd.ms-project.mpxapplication/vnd.ms-project
.mtxtext/xml.mxpapplication/x-mmxp
image/pnetvue.nrfapplication/x-nrf
.nwsmessage/rfc822.odctext/x-ms-odc
.outapplication/x-out.p10application/pkcs10
.p12application/x-pkcs12.p7bapplication/x-pkcs7-certificates
.p7capplication/pkcs7-mime.p7mapplication/pkcs7-mime
.p7rapplication/x-pkcs7-certreqresp.p7sapplication/pkcs7-signature
.pc5application/x-pc5.pciapplication/x-pci
.pclapplication/x-pcl.pcxapplication/x-pcx
.pdfapplication/pdf.pdfapplication/pdf
.pdxapplication/vnd.adobe.pdx.pfxapplication/x-pkcs12
.pglapplication/x-pgl.picapplication/x-pic
.pkoapplication/vnd.ms-pki.pko.plapplication/x-perl
.plgtext/html.plsaudio/scpls
.pltapplication/x-plt.pngimage/png
.pngapplication/x-png.potapplication/vnd.ms-powerpoint
.ppaapplication/vnd.ms-powerpoint.ppmapplication/x-ppm
.ppsapplication/vnd.ms-powerpoint.pptapplication/vnd.ms-powerpoint
.pptapplication/x-ppt.prapplication/x-pr
.prfapplication/pics-rules.prnapplication/x-prn
.prtapplication/x-prt.psapplication/x-ps
.psapplication/postscript.ptnapplication/x-ptn
.pwzapplication/vnd.ms-powerpoint.r3ttext/vnd.rn-realtext3d
.raaudio/vnd.rn-realaudio.ramaudio/x-pn-realaudio
.rasapplication/x-ras.ratapplication/rat-file
.rdftext/xml.recapplication/vnd.rn-recording
.redapplication/x-red.rgbapplication/x-rgb
.rjsapplication/vnd.rn-realsystem-rjs.rjtapplication/vnd.rn-realsystem-rjt
.rlcapplication/x-rlc.rleapplication/x-rle
.rmapplication/vnd.rn-realmedia.rmfapplication/vnd.adobe.rmf
.rmiaudio/mid.rmjapplication/vnd.rn-realsystem-rmj
.rmmaudio/x-pn-realaudio.rmpapplication/vnd.rn-rn_music_package
.rmsapplication/vnd.rn-realmedia-secure.rmvbapplication/vnd.rn-realmedia-vbr
.rmxapplication/vnd.rn-realsystem-rmx.rnxapplication/vnd.rn-realplayer
.rpimage/vnd.rn-realpix.rpmaudio/x-pn-realaudio-plugin
.rsmlapplication/vnd.rn-rsml.rttext/vnd.rn-realtext
.rtfapplication/msword.rtfapplication/x-rtf
.rvvideo/vnd.rn-realvideo.samapplication/x-sam
.satapplication/x-sat.sdpapplication/sdp
.sdwapplication/x-sdw.sitapplication/x-stuffit
.slbapplication/x-slb.sldapplication/x-sld
.slkdrawing/x-slk.smiapplication/smil
.smilapplication/smil.smkapplication/x-smk
.sndaudio/basic.soltext/plain
.sortext/plain.spcapplication/x-pkcs7-certificates
.splapplication/futuresplash.spptext/xml
.ssmapplication/streamingmedia.sstapplication/vnd.ms-pki.certstore
.stlapplication/vnd.ms-pki.stl.stmtext/html
.styapplication/x-sty.svgtext/xml
.swfapplication/x-shockwave-flash.tdfapplication/x-tdf
.tg4application/x-tg4.tgaapplication/x-tga
.tifimage/tiff.tifapplication/x-tif
.tiffimage/tiff.tldtext/xml
.topdrawing/x-top.torrentapplication/x-bittorrent
.tsdtext/xml.txttext/plain
.uinapplication/x-icq.ulstext/iuls
.vcftext/x-vcard.vdaapplication/x-vda
.vdxapplication/vnd.visio.vmltext/xml
.vpgapplication/x-vpeg005.vsdapplication/vnd.visio
.vsdapplication/x-vsd.vssapplication/vnd.visio
.vstapplication/vnd.visio.vstapplication/x-vst
.vswapplication/vnd.visio.vsxapplication/vnd.visio
.vtxapplication/vnd.visio.vxmltext/xml
.wavaudio/wav.waxaudio/x-ms-wax
.wb1application/x-wb1.wb2application/x-wb2
.wb3application/x-wb3.wbmpimage/vnd.wap.wbmp
.wizapplication/msword.wk3application/x-wk3
.wk4application/x-wk4.wkqapplication/x-wkq
.wksapplication/x-wks.wmvideo/x-ms-wm
.wmaaudio/x-ms-wma.wmdapplication/x-ms-wmd
.wmfapplication/x-wmf.wmltext/vnd.wap.wml
.wmvvideo/x-ms-wmv.wmxvideo/x-ms-wmx
.wmzapplication/x-ms-wmz.wp6application/x-wp6
.wpdapplication/x-wpd.wpgapplication/x-wpg
.wplapplication/vnd.ms-wpl.wq1application/x-wq1
.wr1application/x-wr1.wriapplication/x-wri
.wrkapplication/x-wrk.wsapplication/x-ws
.ws2application/x-ws.wsctext/scriptlet
.wsdltext/xml.wvxvideo/x-ms-wvx
.xdpapplication/vnd.adobe.xdp.xdrtext/xml
.xfdapplication/vnd.adobe.xfd.xfdfapplication/vnd.adobe.xfdf
.xhtmltext/html.xlsapplication/vnd.ms-excel
.xlsapplication/x-xls.xlwapplication/x-xlw
.xmltext/xml.xplaudio/scpls
.xqtext/xml.xqltext/xml
.xquerytext/xml.xsdtext/xml
.xsltext/xml.xslttext/xml
.xwdapplication/x-xwd.x_bapplication/x-x_b
.sisapplication/vnd.symbian.install.sisxapplication/vnd.symbian.install
.x_tapplication/x-x_t.ipaapplication/vnd.iphone
.apkapplication/vnd.android.package-archive.xapapplication/x-silverlight-app

② 下载本地资源

下载代码

/**
     * 下载本地资源
     * @param fileName  下载的文件名
     * @param response  响应头
     * @param isOnLine  是否默认下载  true浏览器中打开
     * @throws IOException
     */
@GetMapping("/download")
public void download(String fileName, HttpServletResponse response, boolean isOnLine) throws IOException {
    // 路径可以指定当前项目相对路径
    File file = new File(path + fileName);
    if (file.exists()) {
        // 把文件转化成流
        FileInputStream fileInputStream = new FileInputStream(file);
        ServletOutputStream outputStream = response.getOutputStream();
        // 是否立即下载
        if(!isOnLine){
            response.setContentType("application/octet-stream");
            // 如果文件名为中文需要设置编码
            response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode("背景.jpg", "utf8"));
        }
        byte[] bytes = new byte[1024];
        int len;
        // 把文件流写入响应头
        while ((len = fileInputStream.read(bytes)) != -1) {
            outputStream.write(bytes, 0, len);
        }
    }
}

测试链接:http://localhost:8888/download?fileName=背景.jpg

​ 或者http://localhost:8888/download?isOnLine=true&fileName=背景.jpg

③ 下载网络资源

代码演示

说明:首先要获取到正确的url才能获取到想要的资源

/**
     * 下载网络资源
     * @param response
     * @throws IOException
     */
@RequestMapping("/downLoadMusic")
public void downloadNetworkFile(HttpServletResponse response) throws IOException {
    // 创建url对象
    URL url = new URL("https://m701.music.126/20221009103058/70acd1fa2256e82ae0a90f833aec1072/jdyyaac/obj/w5rDlsOJwrLDjj7CmsOj/18195365565/53a1/ad15/9e1c/1b3457995065a152f56f61c38eda1914.m4a");
    // 建立连接
    URLConnection urlConnection = url.openConnection();
    // 获取到输入流
    InputStream inputStream = urlConnection.getInputStream();
    // 获取到响应头的输出流
    ServletOutputStream outputStream = response.getOutputStream();
    // 设置响应头
    response.setContentType("application/octet-stream");
    response.setHeader("Content-Disposition","attachment;fileName=" + URLEncoder.encode("爱似水仙.m4a", "utf8"));
    int len;
    // 通过字节流下载下来
    byte[] bytes = new byte[1024];
    while ((len = inputStream.read(bytes)) != -1){
        outputStream.write(bytes,0, len);
    }
    // 关闭资源
    inputStream.close();
    outputStream.close();
}

测试链接:http://localhost:8888/downLoadMusic

参考文章1

参考文章2

本文标签: 文件上传详细bootSpring