IO流输出图片
jsp直接转入路径
public void indexApp(@RequestParam("fileType")String fileType,
@RequestParam("detailPath")String detailPath,HttpServletRequest request, HttpServletResponse response){
String patht=configuration.getValue(fileType);//获取配置文件中图片的路径
String path=patht+"/"+detailPath;
File file = new File(path);
FileInputStream fis;
try {
fis = new FileInputStream(file);
l file.length();
byte[] temp = new byte[(int) size];
fis.read(temp, 0, (int) size);
fis.close();
byte[] data = temp;
response.setContentType("image/JPG");
OutputStream out = response.getOutputStream();
out.write(data);
out.flush();
out.close();
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("++++++++++++++++系统找不到指定文件,文件路径是===》"+path);
}
}
第二种可兼容IOS
@RequestMapping(value="/getPicture")
public void indexApp(@RequestParam("fileType")String fileType,
@RequestParam("detailPath")String detailPath,HttpServletRequest request, HttpServletResponse response){
try {
detailPath = new String(detailPath.getBytes("ISO-8859-1"),"utf-8");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String patht=null;
try {
patht= Configuration.getInstance().getValue(fileType);//获取配置文件中图片的路径
logger.error("图片路径="+patht);
}
catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("++++++++++++++++系统获取配置文件中图片的路径异常===》");
logger.error("系统获取配置文件中图片的路径异常");
}
String path=patht+"/"+detailPath;
/*response.setContentType("multipart/form-data");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
// 2.设置文件头:最后一个参数是设置下载文件名
response.setHeader("Content-Disposition", "attachment;fileName=" + detailPath);
ServletOutputStream out; */
// 通过文件路径获得File对象
/*
File html_file = new File(path);
FileInputStream inputStream=null;
try {
inputStream = new FileInputStream(html_file);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// 3.通过response获取ServletOutputStream对象(out)
try {
out = response.getOutputStream();
int b = 0;
byte[] buffer = new byte[1024];
while ((b = inputStream.read(buffer)) != -1) {
// 4.写到输出流(out)中
out.write(buffer, 0, b);
}
inputStream.close();
out.flush();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
*/
try {
File f = new File(path);
if (!f.exists()) {
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("utf-8");
response.getWriter().write("您下载的文件不存在!");
return;
}
BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
byte[] buf = new byte[1024];
int len = 0;
response.reset(); // 非常重要
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "must-revalidate, no-transform");
response.setDateHeader("Expires", 0L);
URL u;
u = new URL("file:///" + path);
response.setContentType(u.openConnection().getContentType());
response.setHeader("Content-Disposition", "inline; filename=" + f.getName());
// 文件名应该编码成UTF-8
OutputStream out1 = response.getOutputStream();
while ((len = br.read(buf)) > 0)
out1.write(buf, 0, len);
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}finally{
out1.close();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}