|
|
@@ -1,8 +1,13 @@
|
|
|
package com.jzg.quotation.commons.utils;
|
|
|
|
|
|
-import java.io.BufferedReader;
|
|
|
-import java.io.DataOutputStream;
|
|
|
-import java.io.InputStreamReader;
|
|
|
+import org.apache.commons.httpclient.HttpClient;
|
|
|
+import org.apache.commons.httpclient.HttpException;
|
|
|
+import org.apache.commons.httpclient.HttpStatus;
|
|
|
+import org.apache.commons.httpclient.methods.PostMethod;
|
|
|
+import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
|
|
|
+import org.apache.commons.httpclient.methods.multipart.Part;
|
|
|
+
|
|
|
+import java.io.*;
|
|
|
import java.net.HttpURLConnection;
|
|
|
import java.net.URL;
|
|
|
import java.util.List;
|
|
|
@@ -74,4 +79,34 @@ public class HttpUtil {
|
|
|
System.err.println("result:" + result);
|
|
|
return result;
|
|
|
}
|
|
|
+
|
|
|
+ public static String sendPostFromFile(Part[] parts, String url){
|
|
|
+ HttpClient httpClient = new HttpClient();
|
|
|
+ PostMethod postMethod = new PostMethod(url);
|
|
|
+ postMethod.getParams().setContentCharset("UTF-8");
|
|
|
+
|
|
|
+ String response = "";
|
|
|
+
|
|
|
+ try {
|
|
|
+// Part[] parts = new Part[]{ new FilePart("file111", file) };
|
|
|
+ postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams()));
|
|
|
+ int statusCode = httpClient.executeMethod(postMethod);
|
|
|
+ if (statusCode == HttpStatus.SC_OK) {
|
|
|
+ response = postMethod.getResponseBodyAsString();
|
|
|
+ System.out.println("Upload successful. Response: " + response);
|
|
|
+ return response;
|
|
|
+// return response;
|
|
|
+ } else {
|
|
|
+ System.out.println("Upload failed. Status code: " + statusCode);
|
|
|
+ }
|
|
|
+ } catch (FileNotFoundException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ } catch (HttpException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+
|
|
|
+ return response;
|
|
|
+ }
|
|
|
}
|