首页>代码>Spring MVC+Mybatis+Ehcache+Apache Shiro+Bootstrap整合开发java仓库管理系统源码>/WMS/src/main/java/com/ken/wms/common/controller/FileSourceHandler.java
package com.ken.wms.common.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * 处理文件下载请求
 *
 * @author Ken
 */
@Controller
@RequestMapping("/commons/fileSource")
public class FileSourceHandler {

    @RequestMapping(value = "download/{fileName:.+}", method = RequestMethod.GET)
    public void fileDownload(@PathVariable("fileName") String fileName, HttpServletRequest request,
                             HttpServletResponse response) throws IOException {

        if (fileName == null)
            return;

        // 获取文件
        ServletContext context = request.getServletContext();
        String directory = context.getRealPath("/WEB-INF/download");
        Path file = Paths.get(directory, fileName);
        if (Files.exists(file)) {
            // 设置响应头
            response.addHeader("Content-Disposition", "attachment;filename=" + file.getFileName());
            Files.copy(file, response.getOutputStream());
            response.getOutputStream().flush();
        }
    }
}
最近下载更多
genyuan2014  LV6 2024年5月7日
微信网友_6512020493586432  LV2 2024年4月7日
jiayongchao258  LV9 2024年3月27日
nyb4613  LV8 2024年3月5日
WBelong  LV8 2023年12月26日
ssh123  LV10 2023年9月28日
13165798872  LV11 2023年9月7日
weilaizhisheng  LV21 2023年7月4日
karthurly  LV3 2023年3月16日
无名氏111  LV33 2023年2月24日
最近浏览更多
zhao44413 2月21日
暂无贡献等级
无念27149 2024年12月24日
暂无贡献等级
bluerstar  LV1 2024年10月27日
lz88888  LV12 2024年10月17日
233002037  LV3 2024年10月12日
月灬兆离  LV1 2024年9月17日
wanjie 2024年8月20日
暂无贡献等级
DelinDelin 2024年8月3日
暂无贡献等级
nightcoren  LV1 2024年5月26日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友