Commit 8921a523 authored by Luochenglong's avatar Luochenglong

文件上传方法修改

parent d032d792
......@@ -221,28 +221,13 @@
<artifactId>spring-test</artifactId>
<version>5.3.14</version>
</dependency>
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>2.0.23</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.15</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.5</version>
<version>1.10</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.4</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
......@@ -250,12 +235,6 @@
<version>4.5.2</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
......
......@@ -6,28 +6,22 @@ import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.http.HttpHeaders;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.springframework.web.client.RestTemplate;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.*;
/**
* description
......@@ -69,7 +63,7 @@ public class HttpPostAttUtil {
public static String UploadFile(String url, Map<String, String> param, File file, String token, String tokentype ) throws Exception {
/*public static String UploadFile(String url, Map<String, String> param, File file, String token, String tokentype ) throws Exception {
try {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append( url)
......@@ -92,7 +86,7 @@ public class HttpPostAttUtil {
HttpPost httpPost = new HttpPost(post_url);
MultipartEntityBuilder builder=MultipartEntityBuilder.create();
builder.addBinaryBody("file",file);
HttpEntity httpEntity=(HttpEntity)builder.build();
HttpEntity httpEntity=builder.build();
httpPost.setHeader("Authorization", tokentype+" "+token);
httpPost.setEntity(httpEntity);//设置请求参数
HttpResponse response = httpClient.execute(httpPost);
......@@ -102,6 +96,38 @@ public class HttpPostAttUtil {
} catch (Exception ex) {
return ex.getMessage() ;
}
}*/
public static String UploadFile(String url, Map<String, String> param, File file, String token, String tokentype ) throws Exception {
HttpClient httpClient = new HttpClient();
HttpServletResponse response = null;
PostMethod post = new PostMethod(url);
String resultString = "";
List<Part> list = new ArrayList<>();
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
try {
list.add(new FilePart("file", file));
if (param != null) {
for (String key : param.keySet()) {
list.add(new StringPart(key, param.get(key), "UTF-8"));
}
}
Part[] parts = list.toArray(new Part[list.size()]);
MultipartRequestEntity entity = new MultipartRequestEntity(parts, new HttpMethodParams());
post.setRequestEntity(entity);
post.addRequestHeader( "Authorization", tokentype+" "+token);
System.out.println(entity.getContentType());
int status = httpClient.executeMethod(post);
String result = post.getResponseBodyAsString() ;
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
} finally {
post.releaseConnection();
}
return "";
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment