通过地址栏输入的文件路径进行下载(SpringMVC)

**开发过程中需要跟APP对接写接口,App端提出需求,下载附件通过地址栏回车访问直接下载,在App中才可以使用这个接口,在PC端是通过a标签href属性下载,所以这里还需要自己写一个接口,才可以满足App的需求


/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.hxrj.modules.api.web;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.hxrj.common.web.BaseController;
import com.hxrj.modules.api.Constants;
import com.hxrj.modules.api.service.ApiValidateService;

/**
 * 文件下载接口(支持资料管理中的附件下载)
 * 
 * @author xrn
 * @version 2017-11-23
 */

@Controller
@RequestMapping(value = "${adminPath}/api/download")
public class DownloadApiAct extends BaseController {

    @Autowired
    private ApiValidateService apiValidateSvc;

    String body = "\"\"";
    String message = "\"\"";
    String status = Constants.API_STATUS_FAIL;

    @RequestMapping(value = { "download", "" })
    public ResponseEntity<byte[]> file(@RequestParam String attaKey, @RequestParam String attaValue,
             HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

            String fileName = attaKey; //接收到的文件全称
            String fileUrl = attaValue; //接收到的文件完整路径

        if (fileName!=null&&fileUrl!=null) {
            String suffix = fileUrl.substring(fileUrl.lastIndexOf(".")); //获取文件后缀

            fileUrl = fileUrl.substring(fileUrl.indexOf("/userfiles"),fileUrl.lastIndexOf("/")); //将传过来的路径进行截取,保留从userfiles开始到结尾的路径
            fileUrl = fileUrl.replace("/","\\");//将截取路径中的斜杠'/'替换为反斜杠'\\'
            fileUrl = "E:\\userFiles" + fileUrl ; //拼接上磁盘中映射的文件地址
            fileName = fileName + suffix; //将传过来的文件名称拼接上文件后缀

            File file = new File(fileUrl, fileName); //文件的完整路径
            if (file.exists()) {
                response.setContentType("application/force-download");// 以二进制的形式进行传输
                response.addHeader("Content-Disposition","attachment;fileName=" + fileName);// 设置文件下载
                byte[] buffer = new byte[1024];
                FileInputStream fis = null;
                BufferedInputStream bis = null;
                try {
                    fis = new FileInputStream(file);
                    bis = new BufferedInputStream(fis);
                    OutputStream os = response.getOutputStream();
                    int i = bis.read(buffer);
                    while (i != -1) {
                        os.write(buffer, 0, i);
                        i = bis.read(buffer);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (bis != null) {
                        try {
                            bis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (fis != null) {
                        try {
                            fis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        return null;
    }

}

中间的代码是在网上找的,引用地址

https://www.cnblogs.com/lonecloud/p/5990060.html


App调用方式

访问路径
/api/download
参数 说明
attaKey 文件名称
attaValue 文件路径
阅读更多
换一批

没有更多推荐了,返回首页