zoukankan      html  css  js  c++  java
  • 文件下载(使用springmvc框架中ResponseEntity对象)

    package com.atguigu.test;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStream;
    
    import javax.servlet.http.HttpSession;
    
    import org.springframework.http.HttpHeaders;
    import org.springframework.http.HttpStatus;
    import org.springframework.http.ResponseEntity;
    import org.springframework.stereotype.Controller;
    import org.springframework.util.MultiValueMap;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class UploadAndDown {
        @RequestMapping("/down")
        public ResponseEntity<byte[]> down(HttpSession session) throws Exception{
            //获取下载文件的路径 (获取tomcat服务器的位置)
            String realPath = session.getServletContext().getRealPath("img");
            String finalPath = realPath+ File.separator +"1.jpg";
            //创建字节输入流
            InputStream in = new FileInputStream(finalPath);
            //available():获取输入流所读取的文件的最大字节数
            byte[] body = new byte[in.available()];
            //把字节读取到数组中
            in.read(body);
            //设置请求头
            MultiValueMap<String, String> headers = new HttpHeaders();
            headers.add("Content-Disposition", "attachment;filename=aaa.jpg");
            //设置响应状态
            HttpStatus statusCode = HttpStatus.OK;
            in.close();
    
            
            ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(body, headers, statusCode);
            return entity;
            
        }
    
    }
  • 相关阅读:
    ARKit 初体验
    基于树莓派的微型气象站设计与开发(Windows 10 IoT Core)
    UWP开发-重新理解MVVM
    UWP开发-二维变换以及三维变换
    WebRTC for UWP
    swift4.0 Http 请求
    Swift4 Json
    UNITY VR 视频/图片 开发心得(二)
    UNITY VR 视频/图片 开发心得(一)
    UWP开发中的方向传感器
  • 原文地址:https://www.cnblogs.com/lsk-130602/p/12239046.html
Copyright © 2011-2022 走看看