`
jinyanliang
  • 浏览: 304064 次
  • 性别: Icon_minigender_1
  • 来自: 河南开封
社区版块
存档分类
最新评论

HttpURLConnection发送文件

 
阅读更多
Java代码 
URL url = new URL(   
            "http://**********");   
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();   
            connection.setDoOutput(true);   
            connection.setRequestMethod("POST");   
            connection.setRequestProperty("Content-Type","multipart/form-data; boundary=Bounday---");   
            connection.setRequestProperty("Cache-Control","no-cache");   
               
            java.io.OutputStream os=connection.getOutputStream();   
            OutputStreamWriter osw=new OutputStreamWriter(os);   
            //BufferedWriter bWriter = new BufferedWriter(osw);   
            //发送字符串数据   
             osw.write("字符串数据");   
             osw.flush();   
                       /*发送文件数据该如何实现*/   
                
               
//      接收数据   
            DataInputStream in = new DataInputStream(connection   
                    .getInputStream());   
            byte[] bytes = new byte[2];   
            in.read(bytes);   
            //String result = new String(bytes);   
               
               
            BufferedReader inss = new BufferedReader(new InputStreamReader(connection.getInputStream()));   
             String line = null;   
             StringBuffer content= new StringBuffer();   
            while((line = inss.readLine()) != null){//line为返回值,这就可以判断是否成功、   
                content.append(line);   
            }   
            inss.close() ;   
            inss=null;   
            url=null;  

URL url = new URL(
			"http://**********");
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setDoOutput(true);
			connection.setRequestMethod("POST");
			connection.setRequestProperty("Content-Type","multipart/form-data; boundary=Bounday---");
			connection.setRequestProperty("Cache-Control","no-cache");
			
			java.io.OutputStream os=connection.getOutputStream();
			OutputStreamWriter osw=new OutputStreamWriter(os);
	  		//BufferedWriter bWriter = new BufferedWriter(osw);
	  		//发送字符串数据
	  		 osw.write("字符串数据");
	  		 osw.flush();
                       /*发送文件数据该如何实现*/
	  		 
	  		
//	  	接收数据
	  		DataInputStream in = new DataInputStream(connection
					.getInputStream());
			byte[] bytes = new byte[2];
			in.read(bytes);
			//String result = new String(bytes);
			
			
			BufferedReader inss = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			 String line = null;
			 StringBuffer content= new StringBuffer();
			while((line = inss.readLine()) != null){//line为返回值,这就可以判断是否成功、
				content.append(line);
			}
			inss.close() ;
			inss=null;
			url=null;
服务器端接收数据代码如下
Java代码 
BufferedReader bufferdeReader = request.getReader();   
        StringBuffer sb1 = new StringBuffer();   
        String tempStr = null;   
        while ((tempStr = bufferdeReader.readLine()) != null) {   
            String s = tempStr;   
            sb1.append(s);   
        }  
分享到:
评论

相关推荐

    java HttpURLConnection 发送文件和字符串信息

    主要介绍了java HttpURLConnection 发送文件和字符串信息的相关资料,需要的朋友可以参考下

    Java利用HttpURLConnection发送post请求上传文件.docx

    Java利用HttpURLConnection发送post请求上传文件

    Java实现批量下载并压缩文件.pptx.pptx

    Java中可以使用HttpURLConnection或者Apache HttpClient等工具类进行文件下载,通过设置请求头和请求参数,发送get或post请求获取文件。 使用Java压缩多个文件为zip包 Java中可以使用ZipOutputStream类将多个文件...

    URLConnection发送HTTP请求的方法_动力节点Java学院整理

    主要介绍了URLConnection发送HTTP请求的方法,主要介绍了如何通过Java(模拟浏览器)发送HTTP请求,有兴趣的可以了解一下

    Java多线程与线程安全实践-基于Http协议的断点续传

    使用Java的HttpURLConnection类连接到服务器,并发送GET请求获取要下载的文件的基本信息,包括文件名、文件大小等。 2.2 分块下载 将要下载的文件分成多个块,每个块使用一个线程进行下载。每个线程需要记录已下载...

    Android Volley扩展实现支持进度条的文件上传功能

    HttpUrlConnection默认传输数据是将数据全部写到内存中再发送到服务端,Volley就是采用默认的方式,这样在上传大文件时很容易就out of memory,有一种解决办法是设置每次传输流的大小: 已知文件大小:connection ....

    Google Android SDK开发范例大全(PDF高清完整版3)(4-3)

    8.6 即时访问网络图文件展示——HttpURLConnection 8.7 手机气象局,实时卫星云图——HttpURLConnection与URLConnection和运行线程 8.8 通过网络播放MP3——Runnable存储FileOutputStream技巧 8.9 设置远程下载音乐为...

    Google Android SDK开发范例大全(PDF完整版4)(4-4)

    8.6 即时访问网络图文件展示——HttpURLConnection 8.7 手机气象局,实时卫星云图——HttpURLConnection与URLConnection和运行线程 8.8 通过网络播放MP3——Runnable存储FileOutputStream技巧 8.9 设置远程下载音乐为...

    Google Android SDK开发范例大全(PDF高清完整版1)(4-1)

    8.6 即时访问网络图文件展示——HttpURLConnection 8.7 手机气象局,实时卫星云图——HttpURLConnection与URLConnection和运行线程 8.8 通过网络播放MP3——Runnable存储FileOutputStream技巧 8.9 设置远程下载音乐为...

    Android知识点及重要代码合集 word文档

    7.7使用runONUiThread()\HttpURLConnection完成文件下载操作 68 7.8 掌握AsyncTask异步任务下载网络资源 70 7.9 DatePickerDialog、TimePickerDialog的使用 76 8.1 ListView、SimpleAdapter和ArrayAdapter的使用 ...

    搜索链接Java网络爬虫(蜘蛛)源码-zhizhu

    网页抓取:使用Java的网络编程库(如HttpURLConnection、Apache HttpClient等)来发送HTTP请求,获取网页的HTML内容。 网页解析:利用Java的HTML解析库(如Jsoup、HtmlCleaner等)来解析网页的HTML结构,提取出所需...

    Google Android SDK开发范例大全的目录

    8.6 即时访问网络图文件展示——HttpURLConnection 8.7 手机气象局,实时卫星云图——HttpURLConnection与URLConnection和运行线程 8.8 通过网络播放MP3——Runnable存储FileOutputStream技巧 8.9 设置远程下载音乐为...

    Google+Android+SDK开发范例大全

    8.2 在程序里浏览网页——WebView.loadUrl 8.3 嵌入HTML标记的程序——WebView.loadData 8.4 设计前往打开网页功能——Intent与Uri.parse 8.5 ...BaseAdapter 8.6 即时访问网络图文件展示——HttpURLConnection 8.7 手机气象局...

    Google Android SDK开发范例大全(完整版附部分源码).pdf

    8.6 即时访问网络图文件展示——HttpURLConnection 8.7 手机气象局,实时卫星云图——HttpURLConnection与URLConnection和运行线程 8.8 通过网络播放MP3——Runnable存储FileOutputStream技巧 8.9 设置远程下载...

    Android Volley Jar框架 v2017.3.17.zip

    我们平时在开发Android应用的时候不可避免地都需要用到网络技术,而多数情况下应用程序都会使用HTTP协议来发送和接收网络数据。Android系统中主要提供了两种方式来进行HTTP通信,HttpURLConnection和HttpClient,...

    Google Android sdk 开发范例大全 部分章节代码

    8.6 即时访问网络图文件展示——HttpURLConnection 8.7 手机气象局,实时卫星云图——HttpURLConnection与URLConnection和运行线程 8.8 通过网络播放MP3——Runnable存储FileOutputStream技巧 8.9 设置远程下载音乐为...

    Google Android SDK 开发范例大全01

    8.6 即时访问网络图文件展示——HttpURLConnection 8.7 手机气象局,实时卫星云图——HttpURLConnection与URLConnection和运行线程 8.8 通过网络播放MP3——Runnable存储FileOutputStream技巧 8.9 设置远程下载音乐为...

    Google Android SDK 开发范例大全02

    8.6 即时访问网络图文件展示——HttpURLConnection 8.7 手机气象局,实时卫星云图——HttpURLConnection与URLConnection和运行线程 8.8 通过网络播放MP3——Runnable存储FileOutputStream技巧 8.9 设置远程下载音乐为...

Global site tag (gtag.js) - Google Analytics