package com.basic.security.utils;
|
|
import com.google.gson.Gson;
|
|
import java.io.BufferedReader;
|
import java.io.DataOutputStream;
|
import java.io.File;
|
import java.io.InputStream;
|
import java.io.InputStreamReader;
|
import java.net.HttpURLConnection;
|
import java.net.URL;
|
import java.util.Map;
|
|
public class WeedUpload {
|
static Gson gson = new Gson();
|
|
public static String upload(File camera_image_path_file) {
|
String pictureUrl = "";
|
Map assignMap = null;
|
try {
|
HttpURLConnection connection = null;
|
String urlParameters = "";
|
try {
|
//Create connection
|
// URL url = new URL("http://123.207.143.213:9333/dir/assign");
|
URL url = new URL("http://192.168.222.1:9333/dir/assign");
|
connection = (HttpURLConnection) url.openConnection();
|
connection.setConnectTimeout(2 * 1000);
|
connection.setRequestMethod("POST");
|
connection.setRequestProperty("Content-Type",
|
"application/x-www-form-urlencoded");
|
connection.setRequestProperty("Content-Length",
|
Integer.toString(urlParameters.getBytes().length));
|
connection.setRequestProperty("Content-Language", "en-US");
|
connection.setUseCaches(false);
|
connection.setDoOutput(true);
|
//Send request
|
DataOutputStream wr = new DataOutputStream(
|
connection.getOutputStream());
|
wr.writeBytes(urlParameters);
|
wr.close();
|
//Get Response
|
InputStream is = connection.getInputStream();
|
// String responseStr = IOUtils.toString(is);
|
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
|
StringBuilder response = new StringBuilder(); // or StringBuffer if Java version 5+
|
String line;
|
while ((line = rd.readLine()) != null) {
|
response.append(line);
|
response.append('\r');
|
}
|
rd.close();
|
String responseStr = response.toString();
|
is.close();
|
// System.out.println("WeedUpload.upload responseStr=" + responseStr);
|
assignMap = gson.fromJson(responseStr, Map.class);
|
} catch (Exception e) {
|
e.printStackTrace();
|
return null;
|
} finally {
|
if (connection != null) {
|
connection.disconnect();
|
}
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
if (assignMap != null) {
|
try {
|
HttpURLConnection connection = null;
|
String urlParameters = "";
|
try {
|
//WeedUpload.upload {"fid":"1,0206882d73","url":"127.0.0.1:8081","publicUrl":"127.0.0.1:8081","count":1}
|
//Create connection
|
// URL url = new URL("http://123.207.143.213:8081/"+assignMap.get("fid"));
|
String charset = "UTF-8";
|
// String requestURL = "http://123.207.143.213:8081/" + assignMap.get("fid");
|
String requestURL = "http://192.168.222.1:8081/" + assignMap.get("fid");
|
pictureUrl = requestURL;
|
// System.out.println("WeedUpload.upload " + requestURL + " " + FrameUtil.getFrames());
|
MultipartUtilityV2 multipart = new MultipartUtilityV2(requestURL);
|
multipart.addFilePart("file", camera_image_path_file);
|
String response = multipart.finish(); // response from server.
|
// System.out.println(response);
|
return requestURL;
|
} catch (Exception e) {
|
e.printStackTrace();
|
return pictureUrl;
|
} finally {
|
if (connection != null) {
|
connection.disconnect();
|
}
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
return pictureUrl;
|
}
|
|
public static void main(String[] args) {
|
upload(new File("d:\\a.jpg"));
|
}
|
}
|