java读取本地图片和下载可选择路径保存图片
2015-09-07 19:37
519 查看
1. 读取本地图片
前台 <img src="<%=path%>xx!showPicture"
/>
2.下载可选择路径保存图片(服务器)
3.下载http形式图片
前台 <img src="<%=path%>xx!showPicture"
/>
/** * 读取本地图片,并显示 */ public void showPicture(){ LOG.info("<==StudentHisAction.toSumSetUser==>"); super.setHttpServlet(); if(StringUtils.isNotEmpty(picPath)){ picPath =picPath.replace("/", File.separator); FileInputStream is = null; try { is = new FileInputStream(picPath); int size = is.available(); //文件大小 byte[] data = new byte[size]; is.read(data); is.close(); super.getResponse().setContentType("image/*"); OutputStream os = super.getResponse().getOutputStream(); //响应输出流 os.write(data); os.close(); } catch (FileNotFoundException e) { e.printStackTrace(); }catch (IOException e) { e.printStackTrace(); } } }
2.下载可选择路径保存图片(服务器)
/** * 用途:下载服务器图片 */ public void downPic(){ LOG.info("<==StudentHisAction.downPic==>"); super.setHttpServlet(); InputStream fis = null; OutputStream toClient = null; try { if(!StringUtils.isNotEmpty(picPath)) return; // path是指欲下载的文件的路径。 File file = new File(picPath); // 取得文件名。 String filename = file.getName(); // 以流的形式下载文件。 fis = new BufferedInputStream(new FileInputStream(picPath)); byte[] buffer = new byte[fis.available()]; fis.read(buffer); // fis.close(); // 清空response super.getResponse().reset(); // 设置response的Header super.getResponse().addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes())); super.getResponse().addHeader("Content-Length", "" + file.length()); toClient = new BufferedOutputStream(response.getOutputStream()); super.getResponse().setContentType("application/octet-stream"); toClient.write(buffer); toClient.flush(); //toClient.close(); } catch (IOException ex) { ex.printStackTrace(); }finally{ if( fis !=null){ try{ fis.close(); toClient.close(); }catch (IOException ex) { ex.printStackTrace(); } } } }
3.下载http形式图片
/** * 下载服务器图片(http形式下载) */ public void downPicByHttp(){ LOG.info("<==StudentHisAction.downPicByHttp==>"); super.setHttpServlet(); if(StringUtils.isEmpty(picPath)) return; InputStream is = null; OutputStream os = null; HttpURLConnection httpURLConnection = null; try{ picPath =picPath.replaceAll("\\\\", "/"); File file = new File(picPath); String fileName = file.getName(); super.getResponse().reset(); // 清空response super.getResponse().setHeader("Content-Disposition", "attachment;filename="+new String(fileName.getBytes())); // 设置response的Header super.getResponse().setContentType("application/octet-stream"); URL url = new URL(picPath); httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setConnectTimeout(5000);//设置网络连接超时时间 httpURLConnection.setDoInput(true);//设置应用程序要从网络连接读取数据 httpURLConnection.setRequestMethod("GET");//设置方式 int responseCode = httpURLConnection.getResponseCode();//获取相应code if(responseCode==200){ is = httpURLConnection.getInputStream();//从服务器返回一个输入流 os = new java.io.BufferedOutputStream(super.getResponse().getOutputStream()); byte[] buffer = new byte[httpURLConnection.getContentLength()]; int len ; while((len=is.read(buffer,0, buffer.length))!=-1){ os.write(buffer, 0, len); } } }catch(Exception e){ e.printStackTrace(); System.out.println("下载图片失败:"+e.getMessage()); }finally{ try { is.close(); os.close(); } catch (IOException e) { e.printStackTrace(); } } }
相关文章推荐
- Java中方法用final修饰参数的作用
- eclipse 版本 代号 svn
- 回忆丶结束Java
- eclipse快捷键
- java万能转移编码
- JAVA使用正则表达式Demo
- Windows Java环境搭建
- Java下载在firefox下面名字乱码,丢失后缀名称
- Java 注解
- java开发初学者需要注意的地方
- Ubuntu14.04 LTS下快速安装JDK8
- Java CMS垃圾收集器几个过程
- Java多线程之六并发容器
- Java多线程之五线程池
- Java多线程之四内存模型及volatile
- Java多线程之二单例模式
- Java多线程之三生产者消费者模式
- Java基础针对自己薄弱环节总结10(线程下)
- java 类的总结
- JDK各个版本的新特性jdk1.5-jdk8