1、客户端发送实现
package url;
import io.IoStreamUtil;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
/**
* httpclient上传文件
* @author linwei
*
*/
public class MultipartEntityUtil {
public static String postFile(File file,String url) throws ClientProtocolException, IOException {
FileBody bin = null;
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
if(file != null) {
bin = new FileBody(file);
}
StringBody username = new StringBody("13696900475");
StringBody password = new StringBody("13696900475");
//请记住,这边传递汉字会出现乱码,解决方法如下,设置好编码格式就好
//new StringBody("汉字",Charset.forName("UTF-8")));
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("username", username);
reqEntity.addPart("password", password);
reqEntity.addPart("data", bin);
httppost.setEntity(reqEntity);
System.out.println("执行: " + httppost.getRequestLine());
HttpResponse response = httpclient.execute(httppost);
System.out.println("statusCode is " + response.getStatusLine().getStatusCode());
HttpEntity resEntity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (resEntity != null) {
System.out.println("返回长度: " + resEntity.getContentLength());
System.out.println("返回类型: " + resEntity.getContentType());
InputStream in = resEntity.getContent();
System.out.println("in is " + in);
System.out.println(IoStreamUtil.getStringFromInputStream(in));
}
if (resEntity != null) {
resEntity.consumeContent();
}
return null;
}
public static void main(String[] args) throws ClientProtocolException, IOException {
File file = new File("d:/rss.xml");
String url = "http://localhost:8080/webtest/servlet/URLTest";
postFile(file,url);
}
}
2、服务端接收实现
//commons-fileupload.jar commons-io
request.setCharacterEncoding("UTF-8");
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
try {
List items = upload.parseRequest(request);
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (item.isFormField()) {
//普通文本信息处理
String paramName = item.getFieldName();
String paramValue = item.getString();
System.out.println(paramName + ":" + paramValue);
} else {
//上传文件信息处理
String fileName = item.getName();
byte[] data = item.get();
String filePath = "d:/ssssss.txt";
FileOutputStream fos = new FileOutputStream(filePath);
fos.write(data);
fos.close();
}
}
} catch (FileUploadException e) {
e.printStackTrace();
}
}
相关推荐
java实现http接口服务端、客户端代码例子。使用jar(commons-codec-1.3.jar、commons-httpclient-3.0.1.jar、commons-logging-1.1.1.jar)
当服务器端能够正确的在Apache上部署和运行以后 再通过安卓客户端代码访问 则可以正确通信了 PS: 代码里的服务器地址 要换成本地跑的服务器地址才可以 一般为127 0 0 1 ">首先这个代码也是从别人那里下载的 但刚...
两个系统之间发送文件的客户端和服务端,例子是客户端发送pdf文件,服务端接受pdf并保存的例子,采用的是httpclient的方式发送的http请求
乐鑫Esp32学习esp32上实现本地 UDP 客户端和服务端角色,在局域网内实现通讯。博文:https://blog.csdn.net/xh870189248/article/details/80737111
httpclient带附件和不带件的例子以及SOAP请求的例子,该压缩包解压后是一个java web工程,直接引入到eclipst下就可以。要在Build Path——Configure Build Path中添加tomcat的lib包。
HttpClient&SpringMvc; 实现客户端 和服务端 get post 这种访问方式,源码 jar包 一应俱全。只供学习使用,
HttpClient Post提交多文件及多个普通参数,已经封装成工具类。 需传入 要请求的url 普通参数map 例 map.put("param1","张三"); 需要传入的文件流map 其中key为文件名 服务端接收无乱码。
本资源包含了断点续传功能的完整实现,包括客户端和服务端。服务端主要是实现支持断点续传,客户端其实就是一个下载器。
NULL 博文链接:https://skymr.iteye.com/blog/1911341
采用httpclient发送xml报文,httpServer接收报文。两个程序一看就明白可以直接调用执行不在多说。
Android客户端向服务器端请求数据的两种主要方式:HTTPURLConnection、HTTPClient;内含客户端与服务器端Demo(带有详细注释),原生方式解析json数据,希望大家多多批评指教!!
socket httpclient 这个功能经常需要用到,索性完整的总结一下,包括客户端服务端,上传至sae
Java 实现HTTP BasicAuth服务端代码,压缩包为Eclipse工程,导入即可运行
go-httpclient, golang的高级HTTP客户端 去 httpclient golang的高级HTTP客户端。特性可以链接的API直接文件上传超时HTTP代理CookieGZIP重定向策略安装go get github.com/ddliu/go-h
java模拟的http客户端,eclipse工程,直接可用
httpcore-4.3.2.jar.httpmime-4.0.1.jar,httpclient-4.3.3.jar.
通过HttpClient实现远程下载,本例子通过java代码实现
通过从客户端调用java类给服务端传输文件。