您的位置:首页 > 理论基础 > 计算机网络

java网络编程基础夯实08-基于HTTP编程(单线程)

2015-03-08 22:50 567 查看
一.基于HTTP协议网络图片下载

package com.markliu.download;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * 从网络获取图片到本地
 * @author MarkLiu
 */
public class DownloadImage {

	public static void main(String[] args) {
		
		String path = "http://my.csdn.net/uploads/avatar/D/B/8/1_mark_lq.jpg";
		
		try {
			byte[] imgBytes = getNetImageBytes(path);
			String fileName = "E:\\mark.jpg";
			writeImageToDisk(imgBytes, fileName);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	
	/**
	 * 将图片保存到指定路径
	 * @param imgBytes
	 * @param fileName
	 */
	public static void writeImageToDisk(byte[] imgBytes, String fileName){

		FileOutputStream outStream = null;
		try {
			File file = new File(fileName);
			outStream = new FileOutputStream(file);
			outStream.write(imgBytes);
			outStream.flush();
			System.out.println("图片已保存:" + fileName);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (outStream != null) {
					outStream.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	/**
	 * 根据地址获得数据的字节流
	 * @param strUrl 网络连接地址
	 * @return
	 * @throws Exception 
	 */
	public static byte[] getNetImageBytes(String path) throws Exception {
		
		URL url = new URL(path);  // 将字符串path封装成URL对象
		// 获取基于HTTP协议的链接对象
		HttpURLConnection httpConn = (HttpURLConnection)url.openConnection();
		httpConn.setRequestMethod("GET");	// 设置请求方式
		httpConn.setConnectTimeout(5 * 1000); // 设置请求超时时间5s
		
		if (httpConn.getResponseCode() == 200) { // 200-299 用于表示请求成功
			InputStream inStream = httpConn.getInputStream();//通过输入流获取图片数据
			byte[] imgBytes = readInputStreamBytes(inStream);	 //得到图片的二进制数据
			return imgBytes;
		}
		return null;
	}
	
	/**
	 * 读取输入流中的数据,可以抽象出一个Stream工具类
	 * @param inStream 输入流
	 * @return 字节数组
	 * @throws Exception
	 */
	public static byte[] readInputStreamBytes(InputStream inStream) throws Exception{
		/*
		 * ByteArrayOutputStream: 此类实现了一个输出流,其中的数据被写入一个 byte
		 * 数组。缓冲区会随着数据的不断写入而自动增长。 可使用 toByteArray()和 toString()获取数据。
		 * ByteArrayOutputStream 是用来缓存数据的,向它的内部缓冲区写入数据,缓冲区自动增长,
		 * 当写入完成时可以从中提取数据。由于这个原因,ByteArrayOutputStream常用于存储数据以用于一次写入。
		 */
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int len = 0;
		while( (len=inStream.read(buffer)) != -1 ){
			outStream.write(buffer, 0, len);
		}
		inStream.close();
		return outStream.toByteArray();
	}
}


二. 测试发现的问题

测试发现有的图片路径可以下载(如:http://www.google.com.hk/images/srpr/logo3w.png),而有的不能下载!(如:http://my.csdn.net/uploads/avatar/D/B/8/1_mark_lq.jpg)求解答。。。
有人说是IIS服务器403问题

在使用IIS的时候,如果遇到403相关的错误,执行访问被禁止

下面是导致此错误信息的两个常见原因:

1、您没有足够的执行许可

  例如,如果试图访问的ASP页所在的目录权限设为“无”,或者,试图执行的CGI脚 本所在的目录权限为“只允许脚本”,将出现此错误信息。

  若要修改执行权限,请在Microsoft管理控制台(MMC)中右击目录,然后依次单击 属性和目录选项卡,确保为试图访问的内容设置适当的执行权限。

2、您没有将试图执行的文件类型的脚本映射设置为识别所使用的谓词(例如,GET或POST)

  若要验证这一点,请在MMC中右击目录,依次单击属性、目录选项卡和配置,然后验 证相应文件类型的脚本映射是否设置为允许所使用的谓词。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: