IO流输出图片 第二种兼容IOS
来源:互联网 发布:android 网络请求mvp 编辑:程序博客网 时间:2024/06/04 01:36
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();
}
}
阅读全文
0 0
- IO流输出图片 第二种兼容IOS
- IO流-第二课
- 从图片第二字开始输出
- IO(输出流)
- IO流输出输入
- java io输出流
- IO输出输入流
- IO字符流输出
- 上传图片兼容ios和安卓
- IO流(拷贝图片)
- IO流复制图片
- IO流--拷贝图片
- IO流复制图片
- IO流拷贝图片
- IO流复制图片
- IO流---字节输出流
- 输入和输出--IO流
- IO流输出系统信息
- 经典cnn检测网络
- log4j2的一个实例
- Ubuntu16.04.3安装Docker,配置镜像加速器及其基本使用
- 关于linux下的core
- Docker网络解决方案
- IO流输出图片 第二种兼容IOS
- (N)Telephony分析(七)之DataConnection建立
- RHEL7.4 发布 将淘汰 Btrfs 文件系统
- redis 学习笔记(一)
- 0欧姆电阻作用
- 带你吃透RTMP
- 博客里没有文章的原因
- 10:简单密码
- Docker源码分析(四):Docker Daemon之NewDaemon实现