java HttpURLConnection模拟上传图片功能

public static void Login() throws IOException{
String mimeBoundary="---------------------------7e2151351150";
//String queryText="";
String httpurl="http://postimages.org/json/rr";
URL urlx = new URL(httpurl);
HttpURLConnection connection = (HttpURLConnection) urlx.openConnection();

connection.setRequestMethod("POST");
connection.setDoOutput(true);
//若提交为post方式,需要修改为false
connection.setUseCaches(false);
connection.setConnectTimeout(5 * 60 * 1000);
connection.setReadTimeout(5 * 60 * 1000);
connection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36");


connection.addRequestProperty("Content-Type", "multipart/form-data; boundary=---------------------------7e2151351150");

//connection.addRequestProperty("Host", "www.zhihu.com");
// connection.addRequestProperty("authorization", "oauth c3cef7c66a1843f8b3a9e6a1e3160e20");
connection.addRequestProperty("referer","https://postimages.org/");
connection.addRequestProperty(":authority","postimages.org");

connection.addRequestProperty("accept", "application/json");
connection.addRequestProperty(":path", "/json/rr");


connection.addRequestProperty("x-requested-with", "XMLHttpRequest");


//向报务器连接

connection.connect();


OutputStream output = connection.getOutputStream();



long s = new Date().getTime();
//System.out.println(s);
StringBuffer sb = new StringBuffer();
//在boundary关需添加两个横线
sb = sb.append("--").append(mimeBoundary);
sb.append("\r\n");
sb.append("Content-Disposition: form-data; name=\"token\"");
//提交的数据前要有两个回车换行
sb.append("\r\n\r\n");
sb.append("61aa06d6116f7331ad7b2ba9c7fb707ec9b182e8");
sb.append("\r\n");
//第二个提交的参数
sb.append("--").append(mimeBoundary);
sb.append("\r\n");
sb.append("Content-Disposition: form-data; name=\"upload_session\"");
sb.append("\r\n\r\n");
sb.append("oclPpj4Sa46AJqbV5P7K0ZcMiqTwxqBk");
sb.append("\r\n");
//提交的第三个参数

sb.append("--").append(mimeBoundary);
sb.append("\r\n");
sb.append("Content-Disposition: form-data; name=\"numfiles\"");
sb.append("\r\n\r\n");
sb.append("1");
sb.append("\r\n");
//第四个参数
sb.append("--").append(mimeBoundary);
sb.append("\r\n");
sb.append("Content-Disposition: form-data; name=\"ui\"");
sb.append("\r\n\r\n");
sb.append("[24,1920,1080,\"true\",\"\",\"\",\"2018/4/9 下午4:48:23\"]");
sb.append("\r\n");


sb.append("--").append(mimeBoundary);
sb.append("\r\n");
sb.append("Content-Disposition: form-data; name=\"optsize\"");
sb.append("\r\n\r\n");
sb.append("0");
sb.append("\r\n");





//第五个参数
sb.append("--").append(mimeBoundary);
sb.append("\r\n");
sb.append("Content-Disposition: form-data; name=\"session_upload\"");
sb.append("\r\n\r\n");
sb.append("1523263703270");
sb.append("\r\n");

sb.append("--").append(mimeBoundary);
sb.append("\r\n");
sb.append("Content-Disposition: form-data; name=\"gallery\"");
sb.append("\r\n\r\n");
sb.append("");
sb.append("\r\n");


sb.append("--").append(mimeBoundary);
sb.append("\r\n");
sb.append("Content-Disposition: form-data; name=\"expire\"");
sb.append("\r\n\r\n");
sb.append("1");
sb.append("\r\n");


sb.append("--").append(mimeBoundary);
sb.append("\r\n");
sb.append("Content-Disposition: form-data; name=\"file\"; filename=\"timg.jpg\"");
sb.append("\r\n");
sb.append("Content-Type:image/jpeg");
sb.append("\r\n");
sb.append("\r\n");
System.out.println(sb.toString());
output.write(sb.toString().getBytes()); 



   DataInputStream in = new DataInputStream(
                   new FileInputStream("D:\\官微\\ss.jpg"));
   
   
   
 
   int bytes = 0;  
           byte[] bufferOut = new byte[1024];  
           while ((bytes = in.read(bufferOut)) != -1) {  
           output.write(bufferOut, 0, bytes);  
           }  
           in.close();  


  byte[] endData = ("\r\n--" + mimeBoundary + "--\r\n").getBytes(); 
  
  System.out.println(output.toString().length());
  
  output.write(endData);  
  output.flush();  
  output.close();  

System.out.println(connection.getResponseCode());
// System.out.println(connection.getHeaderField("X-Req-ID"));
String responseMessage = ""; 
StringBuffer response = new StringBuffer();
BufferedReader reader = null; 
reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8")); 
 
         while ((responseMessage = reader.readLine()) != null) { 
             response.append(responseMessage); 
             response.append("\n"); 
         } 


     
    





}

你可能感兴趣的:(解决问题)