Skip to content

Commit 682f3ce

Browse files
committed
文件上传
1 parent 6e25ece commit 682f3ce

4 files changed

Lines changed: 105 additions & 11 deletions

File tree

src/cn/aofeng/demo/httpclient/FluentApi.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import org.apache.http.message.BasicNameValuePair;
1616
import org.apache.log4j.Logger;
1717

18+
import cn.aofeng.demo.httpclient.server.SimpleHttpServer;
19+
1820
/**
1921
* 使用Fluent API快速地进行简单的HTTP的请求和响应处理,启动{@link SimpleHttpServer}作为请求的服务端。
2022
*

src/cn/aofeng/demo/httpclient/HttpClientBasic.java

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,26 @@
33
*/
44
package cn.aofeng.demo.httpclient;
55

6+
import java.io.File;
67
import java.io.IOException;
78
import java.net.URISyntaxException;
9+
import java.util.ArrayList;
10+
import java.util.List;
811

912
import org.apache.http.Header;
1013
import org.apache.http.HttpEntity;
14+
import org.apache.http.NameValuePair;
1115
import org.apache.http.StatusLine;
1216
import org.apache.http.client.ClientProtocolException;
17+
import org.apache.http.client.entity.UrlEncodedFormEntity;
1318
import org.apache.http.client.methods.CloseableHttpResponse;
1419
import org.apache.http.client.methods.HttpGet;
1520
import org.apache.http.client.methods.HttpPost;
21+
import org.apache.http.entity.ContentType;
22+
import org.apache.http.entity.FileEntity;
1623
import org.apache.http.impl.client.CloseableHttpClient;
1724
import org.apache.http.impl.client.HttpClients;
25+
import org.apache.http.message.BasicNameValuePair;
1826
import org.apache.http.util.EntityUtils;
1927
import org.apache.log4j.Logger;
2028

@@ -37,7 +45,31 @@ public void get() throws URISyntaxException, ClientProtocolException, IOExceptio
3745
CloseableHttpResponse response = client.execute(get);
3846
processResponse(response);
3947
}
40-
48+
49+
public void post() throws ClientProtocolException, IOException {
50+
List<NameValuePair> params = new ArrayList<NameValuePair>();
51+
params.add(new BasicNameValuePair("chinese", "中文"));
52+
params.add(new BasicNameValuePair("english", "英文"));
53+
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, _charset);
54+
55+
CloseableHttpClient client = HttpClients.createDefault();
56+
HttpPost post = new HttpPost(_targetHost+"/post");
57+
post.addHeader("Cookie", "character=abcdefghijklmnopqrstuvwxyz; sign=abc-123-jkl-098");
58+
post.setEntity(entity);
59+
CloseableHttpResponse response = client.execute(post);
60+
processResponse(response);
61+
}
62+
63+
public void sendFile(String filePath) throws UnsupportedOperationException, IOException {
64+
CloseableHttpClient client = HttpClients.createDefault();
65+
HttpPost post = new HttpPost(_targetHost+"/file");
66+
File file = new File(filePath);
67+
FileEntity entity = new FileEntity(file, ContentType.create(ContentType.TEXT_PLAIN.getMimeType(), _charset));
68+
post.setEntity(entity);
69+
CloseableHttpResponse response = client.execute(post);
70+
processResponse(response);
71+
}
72+
4173
private void processResponse(CloseableHttpResponse response)
4274
throws UnsupportedOperationException, IOException {
4375
try {
@@ -65,21 +97,14 @@ private void processResponse(CloseableHttpResponse response)
6597
}
6698
}
6799

68-
public void post() throws ClientProtocolException, IOException {
69-
CloseableHttpClient client = HttpClients.createDefault();
70-
HttpPost post = new HttpPost(_targetHost+"/post");
71-
post.addHeader("Cookie", "character=abcdefghijklmnopqrstuvwxyz; sign=abc-123-jkl-098");
72-
CloseableHttpResponse response = client.execute(post);
73-
processResponse(response);
74-
}
75-
76100
/**
77101
* @param args
78102
*/
79103
public static void main(String[] args) throws Exception {
80104
HttpClientBasic basic = new HttpClientBasic();
81-
basic.get();
82-
basic.post();
105+
// basic.get();
106+
// basic.post();
107+
basic.sendFile("/devdata/projects/open_source/mine/JavaTutorial/LICENSE");
83108
}
84109

85110
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* 创建时间:2016-8-18
3+
*/
4+
package cn.aofeng.demo.httpclient.server;
5+
6+
import java.io.File;
7+
import java.io.FileOutputStream;
8+
import java.io.IOException;
9+
import java.io.InputStream;
10+
import java.io.OutputStream;
11+
12+
import org.apache.commons.io.IOUtils;
13+
import org.apache.log4j.Logger;
14+
15+
import com.sun.net.httpserver.HttpExchange;
16+
import com.sun.net.httpserver.HttpHandler;
17+
18+
/**
19+
* 二进制处理器。
20+
*
21+
* @author <a href="mailto:[email protected]">聂勇</a>
22+
*/
23+
public class BinaryHandler extends AbstractHandler implements HttpHandler {
24+
25+
private static Logger _logger = Logger.getLogger(BinaryHandler.class);
26+
27+
private String _dir = "/home/nieyong/temp/JavaTutorial";
28+
29+
public BinaryHandler(String charset) {
30+
super(charset);
31+
}
32+
33+
@Override
34+
public void handle(HttpExchange httpEx) throws IOException {
35+
super.handleHeader(httpEx);
36+
handleRequest(httpEx);
37+
String content = "收到一个二进制的请求";
38+
super.handleResponse(httpEx, content);
39+
}
40+
41+
/**
42+
* 处理请求。
43+
* @throws IOException
44+
*/
45+
public String handleRequest(HttpExchange httpEx) throws IOException {
46+
OutputStream outs = null;
47+
InputStream ins = null;
48+
try {
49+
File file = new File(_dir, ""+System.currentTimeMillis());
50+
if (!file.exists()) {
51+
file.createNewFile();
52+
}
53+
outs = new FileOutputStream(file);
54+
ins = httpEx.getRequestBody();
55+
IOUtils.copyLarge(ins, outs);
56+
} catch (Exception e) {
57+
_logger.error("read request or write file occurs error", e);
58+
} finally {
59+
IOUtils.closeQuietly(ins);
60+
IOUtils.closeQuietly(outs);
61+
}
62+
63+
return null;
64+
}
65+
66+
}

src/cn/aofeng/demo/httpclient/server/SimpleHttpServer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public static void main(String[] args) {
3030
HttpServer server = HttpServer.create(new InetSocketAddress(port), 128);
3131
server.createContext("/get", new CharacterHandler(_charset));
3232
server.createContext("/post", new CharacterHandler(_charset));
33+
server.createContext("/file", new BinaryHandler(_charset));
3334
server.start();
3435
_logger.info("http server already started, listen port:"+port);
3536
} catch (IOException e) {

0 commit comments

Comments
 (0)