add a post multipartFile for test
This commit is contained in:
parent
4dbbcd2f55
commit
75666ffde7
|
@ -21,9 +21,11 @@ import org.apache.commons.httpclient.methods.PostMethod;
|
||||||
import org.apache.commons.httpclient.methods.multipart.FilePart;
|
import org.apache.commons.httpclient.methods.multipart.FilePart;
|
||||||
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
|
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
|
||||||
import org.apache.commons.httpclient.methods.multipart.Part;
|
import org.apache.commons.httpclient.methods.multipart.Part;
|
||||||
|
import org.apache.commons.httpclient.methods.multipart.PartSource;
|
||||||
import org.apache.commons.httpclient.methods.multipart.StringPart;
|
import org.apache.commons.httpclient.methods.multipart.StringPart;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
import org.springframework.web.multipart.commons.CommonsMultipartFile;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class HttpRequester {
|
public class HttpRequester {
|
||||||
|
@ -115,21 +117,19 @@ public class HttpRequester {
|
||||||
|
|
||||||
public HttpResponse postFiles(Map<String, String> headers, String url,
|
public HttpResponse postFiles(Map<String, String> headers, String url,
|
||||||
String filePartName, List<File> files, String stringPartName,
|
String filePartName, List<File> files, String stringPartName,
|
||||||
List<String> strings) {
|
List<String> strings){
|
||||||
if (!url.startsWith("http"))
|
if (!url.startsWith("http"))
|
||||||
url = "http://" + url;
|
url = "http://" + url;
|
||||||
PostMethod postMethod = new PostMethod(url);
|
PostMethod postMethod = new PostMethod(url);
|
||||||
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (headers != null) {
|
if (headers != null) {
|
||||||
|
|
||||||
for (Entry<String, String> entry : headers.entrySet()) {
|
for (Entry<String, String> entry : headers.entrySet()) {
|
||||||
Header header=new Header(entry.getKey(), entry.getValue());
|
Header header = new Header(entry.getKey(), entry.getValue());
|
||||||
postMethod.addRequestHeader(header);
|
postMethod.addRequestHeader(header);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
Part[] parts = new Part[files.size() + strings.size()];
|
Part[] parts = new Part[files.size() + strings.size()];
|
||||||
for (int i = 0; i < files.size(); i++) {
|
for (int i = 0; i < files.size(); i++) {
|
||||||
|
@ -148,6 +148,40 @@ public class HttpRequester {
|
||||||
} finally {
|
} finally {
|
||||||
postMethod.releaseConnection();
|
postMethod.releaseConnection();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
public HttpResponse postFilesMulti(Map<String, String> headers, String url,
|
||||||
|
String filePartName, CommonsMultipartFile[] paramFiles, String stringPartName,
|
||||||
|
List<String> strings) {
|
||||||
|
if (!url.startsWith("http"))
|
||||||
|
url = "http://" + url;
|
||||||
|
PostMethod postMethod = new PostMethod(url);
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (headers != null) {
|
||||||
|
|
||||||
|
for (Entry<String, String> entry : headers.entrySet()) {
|
||||||
|
Header header = new Header(entry.getKey(), entry.getValue());
|
||||||
|
postMethod.addRequestHeader(header);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
Part[] parts = new Part[paramFiles.length + strings.size()];
|
||||||
|
for (int i = 0; i < paramFiles.length; i++) {
|
||||||
|
parts[i] = new FilePart(filePartName, (PartSource) paramFiles[i]);
|
||||||
|
}
|
||||||
|
for (int i = 0; i < strings.size(); i++) {
|
||||||
|
parts[i + paramFiles.length] = new StringPart(stringPartName,
|
||||||
|
strings.get(i));
|
||||||
|
}
|
||||||
|
postMethod.setRequestEntity(new MultipartRequestEntity(parts,
|
||||||
|
postMethod.getParams()));
|
||||||
|
return buildResponseWithMethod(postMethod);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
} finally {
|
||||||
|
postMethod.releaseConnection();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -215,6 +249,7 @@ public class HttpRequester {
|
||||||
return buildHttpResponse(urlConnection,
|
return buildHttpResponse(urlConnection,
|
||||||
readConnectionInputStream(in));
|
readConnectionInputStream(in));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
in = urlConnection.getErrorStream();
|
in = urlConnection.getErrorStream();
|
||||||
try {
|
try {
|
||||||
return buildHttpResponse(urlConnection,
|
return buildHttpResponse(urlConnection,
|
||||||
|
|
Loading…
Reference in New Issue