猿问

jsp 页面 图片请求成功后 请求路径被当成jsp页面地址

问题描述

图片请求成功,图片正常显示,但是后台报错误;

页面图片请求

<img src="/file/see/${item.image}" />
Request URL: http://127.0.0.1:8081/file/see/71
Request Method: GET
Status Code: 200 OK
Remote Address: 127.0.0.1:8081
Referrer Policy: no-referrer-when-downgrade
Content-Disposition: inline;fileName=20180905185410754FFL.png
Content-Type: image/png
Date: Thu, 06 Sep 2018 02:54:41 GMT
Server: Apache-Coyote/1.1
Transfer-Encoding: chunked
Accept: image/webp,image/apng,image/*,*/*;q=0.8
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9
Connection: keep-alive
Cookie: sid=9bf5a6b7-36b7-46ae-8fdb-04ffc662986c; tset=test123; JSESSIONID=338F787EDC932735E50D98AC13B866EC
Host: 127.0.0.1:8081
Referer: http://127.0.0.1:8081/
User-Agent: Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Mobile Safari/537.36

控制台报错信息

九月 06, 2018 10:27:03 上午 org.apache.catalina.core.ApplicationDispatcher invoke
严重: Servlet.service() for servlet jsp threw exception
javax.servlet.ServletException: File &quot;/WEB-INF/jsp/file/see/71.jsp&quot; not found
……

问题出现的环境背景及自己尝试过哪些方法

一开始请求方法是由json返回值的,后来我把返回值去掉还是不行;
网上搜索说可能是response重复调用问题?
我推测可能是jsp页面把这个请求地址当成静态页面取获取了,结果就后台找不到页面所有报错了;

相关代码

// 请把代码文本粘贴到下方(请勿用图片代替代码)
图片请求方法

    @RequestMapping("/see/{id}")
    public void seeUrl(@PathVariable("id")Integer id) {
        sysFileService.fileDownload(id, false, true, response);
    }
    
    @Override
    public void fileDownload(Integer id, Boolean isName, Boolean isOpen, HttpServletResponse response) {
        TbFile tbFile = tbFileMapper.selectByPrimaryKey(id);
        if( null != tbFile) {
            File file = new File(tbFile.getSaveDir() + "/" + tbFile.getSaveName() + "." + tbFile.getFileSuffix());
            if(file.exists()){
                try {
                    FileUtils.fileDownload(file, tbFile, response, isName, isOpen);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
    
    public static void fileDownload(File file, TbFile tbFile, HttpServletResponse response, Boolean isName, Boolean isOpen) throws FileNotFoundException, IOException {

        String fullName = "";
        //是否使用生成的文件名
        if(null == isName) {
            fullName = new String(tbFile.getFileName().getBytes(),"ISO-8859-1") + "."+tbFile.getFileSuffix();
        } else {
            fullName = new String(tbFile.getSaveName().getBytes(),"ISO-8859-1") + "."+tbFile.getFileSuffix();
        }
        //是否打开
        if(null == isOpen) {
            //强制下载
            response.setContentType("application/force-download");
            response.setHeader("Content-Disposition", "attachment;fileName=" + fullName);
        }else {
            //浏览器查看
            response.setContentType(tbFile.getFileType());
            response.setHeader("Content-Disposition", "inline;fileName=" + fullName);
        }

        streamWrite(new FileInputStream(file), response.getOutputStream());

        logger.info(
                "fileDownload---" +
                    "\t文件名:" + tbFile.getFileName() +
                    "\t文件后缀类型:" + tbFile.getFileType() +
                    "\t文件大小:" + tbFile.getFileSize() +
                    "\t生成文件目录:" + tbFile.getSaveDir() +
                    "\t生成文件名:" + tbFile.getSaveName()
        );
    }
    
    public static void streamWrite(InputStream is, OutputStream os) throws IOException {
        byte[] b = new byte[1024];
        int len;
        while((len = is.read(b)) != -1) {
            os.write(b, 0, len);
        }
        os.flush();
        os.close();
        is.close();
    }

你期待的结果是什么?实际看到的错误信息又是什么?

1.这个请求路径还需要再配置不访问jsp文件吗
2.还是后台response的时候给设置一个值返回成功后不再请求

汪汪一只猫
浏览 791回答 1
1回答
随时随地看视频慕课网APP

相关分类

Java
我要回答