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发送post请求上传文件
Java中可以使用HttpURLConnection或者Apache HttpClient等工具类进行文件下载,通过设置请求头和请求参数,发送get或post请求获取文件。 使用Java压缩多个文件为zip包 Java中可以使用ZipOutputStream类将多个文件...
主要介绍了URLConnection发送HTTP请求的方法,主要介绍了如何通过Java(模拟浏览器)发送HTTP请求,有兴趣的可以了解一下
使用Java的HttpURLConnection类连接到服务器,并发送GET请求获取要下载的文件的基本信息,包括文件名、文件大小等。 2.2 分块下载 将要下载的文件分成多个块,每个块使用一个线程进行下载。每个线程需要记录已下载...
HttpUrlConnection默认传输数据是将数据全部写到内存中再发送到服务端,Volley就是采用默认的方式,这样在上传大文件时很容易就out of memory,有一种解决办法是设置每次传输流的大小: 已知文件大小:connection ....
8.6 即时访问网络图文件展示——HttpURLConnection 8.7 手机气象局,实时卫星云图——HttpURLConnection与URLConnection和运行线程 8.8 通过网络播放MP3——Runnable存储FileOutputStream技巧 8.9 设置远程下载音乐为...
8.6 即时访问网络图文件展示——HttpURLConnection 8.7 手机气象局,实时卫星云图——HttpURLConnection与URLConnection和运行线程 8.8 通过网络播放MP3——Runnable存储FileOutputStream技巧 8.9 设置远程下载音乐为...
8.6 即时访问网络图文件展示——HttpURLConnection 8.7 手机气象局,实时卫星云图——HttpURLConnection与URLConnection和运行线程 8.8 通过网络播放MP3——Runnable存储FileOutputStream技巧 8.9 设置远程下载音乐为...
7.7使用runONUiThread()\HttpURLConnection完成文件下载操作 68 7.8 掌握AsyncTask异步任务下载网络资源 70 7.9 DatePickerDialog、TimePickerDialog的使用 76 8.1 ListView、SimpleAdapter和ArrayAdapter的使用 ...
网页抓取:使用Java的网络编程库(如HttpURLConnection、Apache HttpClient等)来发送HTTP请求,获取网页的HTML内容。 网页解析:利用Java的HTML解析库(如Jsoup、HtmlCleaner等)来解析网页的HTML结构,提取出所需...
8.6 即时访问网络图文件展示——HttpURLConnection 8.7 手机气象局,实时卫星云图——HttpURLConnection与URLConnection和运行线程 8.8 通过网络播放MP3——Runnable存储FileOutputStream技巧 8.9 设置远程下载音乐为...
8.2 在程序里浏览网页——WebView.loadUrl 8.3 嵌入HTML标记的程序——WebView.loadData 8.4 设计前往打开网页功能——Intent与Uri.parse 8.5 ...BaseAdapter 8.6 即时访问网络图文件展示——HttpURLConnection 8.7 手机气象局...
8.6 即时访问网络图文件展示——HttpURLConnection 8.7 手机气象局,实时卫星云图——HttpURLConnection与URLConnection和运行线程 8.8 通过网络播放MP3——Runnable存储FileOutputStream技巧 8.9 设置远程下载...
我们平时在开发Android应用的时候不可避免地都需要用到网络技术,而多数情况下应用程序都会使用HTTP协议来发送和接收网络数据。Android系统中主要提供了两种方式来进行HTTP通信,HttpURLConnection和HttpClient,...
8.6 即时访问网络图文件展示——HttpURLConnection 8.7 手机气象局,实时卫星云图——HttpURLConnection与URLConnection和运行线程 8.8 通过网络播放MP3——Runnable存储FileOutputStream技巧 8.9 设置远程下载音乐为...
8.6 即时访问网络图文件展示——HttpURLConnection 8.7 手机气象局,实时卫星云图——HttpURLConnection与URLConnection和运行线程 8.8 通过网络播放MP3——Runnable存储FileOutputStream技巧 8.9 设置远程下载音乐为...
8.6 即时访问网络图文件展示——HttpURLConnection 8.7 手机气象局,实时卫星云图——HttpURLConnection与URLConnection和运行线程 8.8 通过网络播放MP3——Runnable存储FileOutputStream技巧 8.9 设置远程下载音乐为...