guava批量上传

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package com.example.login.service;

import com.example.login.util.FileUploadUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Date;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.stream.IntStream;

public class UploadToServer {

public static String postFile(String url, File file) throws ClientProtocolException, IOException {
String res = null;
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httppost = new HttpPost(url);
FileBody bin = new FileBody(file);
StringBody destination = new StringBody("", ContentType.create("text/plain",Charset.forName("UTF-8")));
StringBody type = new StringBody("file", ContentType.create("text/plain",Charset.forName("UTF-8")));
HttpEntity reqEntity = MultipartEntityBuilder.create().addPart("file", bin)
.setMode(HttpMultipartMode.RFC6532)
.setCharset(Charset.forName("UTF-8"))
.addPart("destination", destination)
.addPart("type", type)
.build();
httppost.setEntity(reqEntity);
CloseableHttpResponse response = httpClient.execute(httppost);
HttpEntity entity = response.getEntity();
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
res = EntityUtils.toString(entity, "UTF-8");
response.close();
} else {
res = EntityUtils.toString(entity, "UTF-8");
response.close();
throw new IllegalArgumentException(res);
}
return res;
}
public static void main(String[] args) throws ClientProtocolException, IOException {
// 设置默认工厂类
ExecutorService executorService = Executors.newFixedThreadPool(10);
System.out.println("[开始]:"+new Date().toLocaleString());
IntStream.rangeClosed(1,10).parallel().forEach(e->{
Future<String> submit = executorService.submit(new uploadThread(e));
});
executorService.shutdown();
}
public static String uploadThreadDo(Integer count) throws IOException {
File file = new File(FileUploadUtils.pathPre+"Temp\\tomcatWebPackage"+count+".zip");
// String postFile = postFile("http://xxxxxx/uploads/uploadFile", file);
String postFile = postFile("http://xxxxxx:8081/uploads/uploadFile", file);
System.out.println("["+count+"][结束]:"+new Date().toLocaleString());
return "success:"+count;
}
static class uploadThread implements Callable<String> {
Integer count;
public uploadThread(Integer count) {
this.count=count;
}
@Override
public String call() throws IOException {
return uploadThreadDo(count);
}
}
}