首页>代码>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();
        }
    }
}
最近下载更多
qwe71656099  LV3 2025年10月13日
stonewan  LV10 2025年7月29日
krispeng  LV15 2025年6月4日
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日
最近浏览更多
qwe71656099  LV3 2025年10月13日
stonewan  LV10 2025年7月29日
krispeng  LV15 2025年6月4日
微信网友_6040315240812544  LV8 2025年3月21日
zhao44413 2025年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日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友