下载文件--接口

@RequestMapping(value = "download",method = RequestMethod.GET)
    public ResponseEntity<byte[]> download(HttpServletResponse response, HttpServletRequest request) throws Exception{

        //预下载文件路径******
        String preDownload_path = "//172.12.34.11/8c2fa5bc-7916-8310-ee65-c77c8a7916ae/TempFile/UploadFile/uploadjob_12796/testlinux总结01.pdf";
        //C:/Users/Administrator/Desktop/Learn/pdf/temp8.pdf
        //C:/Users/Administrator/Desktop/Learn/查询语句.docx
        int index = preDownload_path.lastIndexOf("/");
        //下载后的文件名
        String downloadName = preDownload_path.substring(index+1);
        System.out.println(index);
        System.out.println(downloadName);

        response.setCharacterEncoding("utf-8");
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment;fileName="+java.net.URLEncoder.encode(downloadName,"UTF-8"));

        InputStream is = null;
        OutputStream os = null;
        try {
            is = new FileInputStream(new File(preDownload_path));
            os = response.getOutputStream();
            byte[] b = new byte[2048];
            int length;
            while ((length = is.read(b)) > 0) {
                os.write(b, 0, length);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            os.close();
            is.close();
        }
        return null;
    }
常见的媒体格式类型如下: 
   text/html : HTML格式
    text/plain :纯文本格式     
    text/xml :  XML格式
    image/gif :gif图片格式   
    image/jpeg :jpg图片格式
    image/png:png图片格式

以application开头的媒体格式类型:
   application/xhtml+xml :XHTML格式
   application/xml     : XML数据格式
   application/atom+xml  :Atom XML聚合格式   
   application/json    : JSON数据格式
   application/pdf       :pdf格式 
   application/msword  : Word文档格式
   application/octet-stream : 二进制流数据(如常见的文件下载)
   application/x-www-form-urlencoded : <form encType=””>中默认的encType,form表单数据被编码为key/value格式发送到服务器(表单默认的提交数据的格式)


另外一种常见的媒体格式是上传文件之时使用的:
   multipart/form-data : 需要在表单中进行文件上传时,就需要使用该格式

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值