亲测好用
jsp页面忽略
一:controller方法
/**
* 上传文件后显示文件的功能
* @param request
* @param response
* @param url
* @throws IOException
*/
@RequestMapping(value = "doViewByAccessory")
public void doViewByAccessory(HttpServletRequest request, HttpServletResponse response,String url) throws IOException {
try {
fileInfoService.doViewByAccessory(request,url,response);
} catch (BusinessException e) {
e.printStackTrace();
}
}
二:service层代码
/**
* 回显方法
* @param request
* @param url
* @param response
*/
public void doViewByAccessory(HttpServletRequest request, String url, HttpServletResponse response) {
try {
final ServletOutputStream sos = response.getOutputStream(); // 获得ServletOutputStream对象
response.setContentType("image/jpg; charset=GBK");
FileInputStream is = null;
try {
File file = new File(url);
is = new FileInputStream(file);
response.reset();
int bytesRead = 0;
byte[] buffer = new byte[8 * 1024];
while ((bytesRead = is.read(buffer)) != -1) {
sos.write(buffer, 0, bytesRead);
sos.flush();
}
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
sos.close();
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}