java文件下载,出现路径找不到File not found(拒绝访问)

(1)可能出现的问题是: File file = new File(文件路径) : 文件路径没有截止到文件名,所以找不到;

(2)下载的文件包含中文字符: 也会出现此问题;

建议下载文件设置为英文字符或数字;

下载文件的流程:


    /**
     * 下载文件
     * @param id
     * @param res
     * @param request
     */
    @ApiOperation(value="下载文件", notes="下载文件", produces="application/json")
    @GetMapping(value="/download/{id}", produces = "application/json")
    public void downloadTemplate(@PathVariable("id") Long id, HttpServletResponse res, HttpServletResponse request) {
        MesProductionFilesEntity entity = mesFilesService.queryObject(id);
        if (null != entity) {
            String filepath = null;
            try {
                filepath =entity.getFilePath();
                downloadResultFile(res, request, filepath, entity.getFileName());
                logger.info("下载成功!");
            } catch (Exception e) {
                e.printStackTrace();
                logger.error("下载文件异常");
            }
        }else {
            logger.error("下载失败:根据ID 未能找到文件信息!");
        }
    }
    /**
     * 下载文件的方法
     * @param res response对象
     * @param filePath 待下载的文件名
     * @param downloadName 下载给用户的文件名
     */
    private void downloadResultFile(HttpServletResponse res, HttpServletResponse request, String filePath, String downloadName) {

        try {
       if (request.getHeader("User-Agent") != null && request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0) {
                downloadName = URLEncoder.encode(downloadName, "UTF-8");
            } else {
                downloadName = new String(downloadName.getBytes("UTF-8"), "UTF-8");
            }
        }
        catch (UnsupportedEncodingException ue) {
            logger.error("download filename convert encoding exception ", ue);
        }

        res.setContentType("application/octet-stream");
        res.addHeader("Content-Disposition", "attachment; filename=" + downloadName);
        BufferedInputStream bis = null;
        BufferedOutputStream out = null;
        File file = new File(filePath);
        try {
            bis = new BufferedInputStream(new FileInputStream(file));
            out = new BufferedOutputStream(res.getOutputStream());
            byte[] buff = new byte[2048];
            while (true) {
                int bytesRead;
                if (-1 == (bytesRead = bis.read(buff, 0, buff.length))) {
                    break;
                }
                out.write(buff, 0, bytesRead);
            }
            out.flush();
        } catch (IOException e) {
            logger.error("output template file exception", e);
        } finally {
            IOUtils.closeQuietly(bis);
            IOUtils.closeQuietly(out);
        }
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!要将Java文件转换为Base64编码,首先确保要转换的文件存在,并且在您的代码中正确指定了文件路径。如果不到文件,可能有以下几个原因: 1. 文件路径错误:请确认您提供的文件路径是正确的,并且文件确实存在于该路径下。可以尝试使用绝对路径来确保准确性。 2. 文件权限问题:请确保您的代码具有足够的权限来访问该文件。在某些情况下,您可能需要以管理员身份运行程序。 3. 文件不存在:如果文件确实不存在,您可以检查文件名的拼写错误,并确保文件已经创建或上传到正确的位置。 以下是一个将Java文件转换为Base64的示例代码,您可以参考: ```java import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.Base64; public class FileToBase64Converter { public static void main(String[] args) { String filePath = "path/to/your/file.java"; File file = new File(filePath); if (file.exists()) { try { FileInputStream fileInputStream = new FileInputStream(file); byte[] fileBytes = new byte[(int) file.length()]; fileInputStream.read(fileBytes); fileInputStream.close(); String base64String = Base64.getEncoder().encodeToString(fileBytes); System.out.println(base64String); } catch (IOException e) { e.printStackTrace(); } } else { System.out.println("File not found!"); } } } ``` 请将`filePath`替换为要转换的Java文件的实际路径。如果文件存在且路径正确,此代码将打印文件的Base64编码字符串。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值