java读取本地图片和下载可选择路径保存图片

   1. 读取本地图片
   前台 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)