httpConnection上传文件

from:http://blog.csdn.net/wangpeng047/article/details/38303865

/**
     * 上传文件时提交的数据格式
     -----------------------------22448196075966
	 Content-Disposition: form-data; name="access_token"
	
	 accessss
	 -----------------------------22448196075966
	 Content-Disposition: form-data; name="type"
	
	 变量值
     -----------------------------22448196075966
     Content-Disposition: form-data; name="file"; filename="s.txt"
     Content-Type: application/octet-stream

     文件内容
     -----------------------------22448196075966--
     */
    public static void uploadImage(String requestUrl,File  file,String requestMethod) throws IOException{
		//分割符
    	String boundary="-----------------------------32034106127045";
    	String boundary2="-------------------------------32034106127045";
		//换行符
    	String enter="\r\n";
    	URL url = new URL(requestUrl);
        HttpURLConnection httpUrlConn = (HttpURLConnection)url.openConnection();
        httpUrlConn.setDoOutput(true);
        httpUrlConn.setDoInput(true);
        //设置请求头
        httpUrlConn.setRequestProperty("Content-Type", "multipart/form-data; boundary="+boundary);
        httpUrlConn.setRequestMethod(requestMethod);
        OutputStream outputStream = httpUrlConn.getOutputStream();
        
        StringBuilder sb=new StringBuilder();
        sb.append(boundary2);
        sb.append(enter);
        String disponsition="Content-Disposition: form-data; name=\"file\"; filename=\""+file.getName()+"\"";
        sb.append(disponsition);
        sb.append(enter);
        //更具不同的类型经行设置
        sb.append("Content-Type: audio/wav");
        sb.append(enter);
        sb.append(enter);
        outputStream.write(sb.toString().getBytes());
        byte[] b=new byte[1024];
        BufferedInputStream in=new  BufferedInputStream(new FileInputStream(file)) ;
        int c=-1;
        while((c=in.read(b))!=-1){
        	 outputStream.write(b,0,c);
        }
        in.close();
        outputStream.write(enter.getBytes());
        outputStream.write((boundary2+"--").getBytes());
        outputStream.close();
        
        
        StringBuffer buffer=new StringBuffer();
        InputStream inputStream = httpUrlConn.getInputStream();
        InputStreamReader inputStreamReader = new InputStreamReader(
            inputStream, "utf-8");
        BufferedReader bufferedReader = new BufferedReader(
            inputStreamReader);

        String str = null;
        while ( (str = bufferedReader.readLine()) != null)
        {
            buffer.append(str);
        }
        bufferedReader.close();
        inputStreamReader.close();
        // 释放资源
        inputStream.close();
        inputStream = null;
        httpUrlConn.disconnect();
        System.out.println(buffer);
        
        FileOutputStream fileOut=new FileOutputStream("c:\\sdf.txt");
        fileOut.write(buffer.toString().getBytes());
        fileOut.close();
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值