圖片請(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 "/WEB-INF/jsp/file/see/71.jsp" not found
……
一開始請(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ò)了;
// 請(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();
}
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);
}
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國(guó)IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國(guó)家
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國(guó)一站式人才培養(yǎng)平臺(tái)、一站式人才輸送平臺(tái)。2014年4月3日在美國(guó)成功上市,融資1
北大課工場(chǎng)是北京大學(xué)校辦產(chǎn)業(yè)為響應(yīng)國(guó)家深化產(chǎn)教融合/校企合作的政策,積極推進(jìn)“中國(guó)制造2025”,實(shí)現(xiàn)中華民族偉大復(fù)興的升級(jí)產(chǎn)業(yè)鏈。利用北京大學(xué)優(yōu)質(zhì)教育資源及背
博為峰,中國(guó)職業(yè)人才培訓(xùn)領(lǐng)域的先行者
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項(xiàng)目經(jīng)理從事移動(dòng)互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
浪潮集團(tuán)項(xiàng)目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺(tái)面向?qū)ο箝_發(fā)經(jīng)驗(yàn),技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫(kù),具有快速界面開發(fā)的能力,對(duì)瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁(yè)制作和網(wǎng)頁(yè)游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗(yàn)。曾經(jīng)歷任德國(guó)Software AG 技術(shù)顧問,美國(guó)Dachieve 系統(tǒng)架構(gòu)師,美國(guó)AngelEngineers Inc. 系統(tǒng)架構(gòu)師。