在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Java  HTML/ jsp 頁(yè)面 圖片請(qǐng)求成功后 請(qǐng)求路徑被當(dāng)成jsp頁(yè)面地址

jsp 頁(yè)面 圖片請(qǐng)求成功后 請(qǐng)求路徑被當(dāng)成jsp頁(yè)面地址

問題描述

圖片請(qǐng)求成功,圖片正常顯示,但是后臺(tái)報(bào)錯(cuò)誤;

頁(yè)面圖片請(qǐng)求

<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

控制臺(tái)報(bào)錯(cuò)信息

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

問題出現(xiàn)的環(huán)境背景及自己嘗試過哪些方法

一開始請(qǐng)求方法是由json返回值的,后來我把返回值去掉還是不行;
網(wǎng)上搜索說可能是response重復(fù)調(diào)用問題?
我推測(cè)可能是jsp頁(yè)面把這個(gè)請(qǐng)求地址當(dāng)成靜態(tài)頁(yè)面取獲取了,結(jié)果就后臺(tái)找不到頁(yè)面所有報(bào)錯(cuò)了;

相關(guān)代碼

// 請(qǐng)把代碼文本粘貼到下方(請(qǐng)勿用圖片代替代碼)
圖片請(qǐng)求方法

    @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) {
            //強(qiáng)制下載
            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();
    }

你期待的結(jié)果是什么?實(shí)際看到的錯(cuò)誤信息又是什么?

1.這個(gè)請(qǐng)求路徑還需要再配置不訪問jsp文件嗎
2.還是后臺(tái)response的時(shí)候給設(shè)置一個(gè)值返回成功后不再請(qǐng)求

回答
編輯回答
我不懂

問題原因
我在問題描述的時(shí)候忘記貼controller的頭部信息了;

    @Autowired
    private HttpServletResponse response; 

因?yàn)槲以谡?qǐng)求層注入了response,后面的文件請(qǐng)求對(duì)象返回都使用了這個(gè),導(dǎo)致了報(bào)錯(cuò);
修改為

    @RequestMapping("/see/{id}")
    public void seeUrl(@PathVariable("id")Integer id, HttpServletResponse httpServletResponse) {
        sysFileService.fileDownload(id, false, true, httpServletResponse);
    }

參考鏈接

2018年7月12日 11:01