包含流的方式调用servlet

package com.vtradex.tms.server.service.shipment.pojo;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;




public class PDAtest
{


private static String urlStr = "http://127.0.0.1:8089/gytms_edi/*.gyPDAServlet?MEMO=dsadasdasd&SHIPMENTNO=0703004&method=setGYPtion&ORDERNO=YYTACV&PLACESUPPDTLID=114005778G&PUCTID=002&GROUPNO=027000";


private static URL url;
private static HttpURLConnection httpURLConnection;
private static String response;


public static void main(String[] args) throws IOException {
upload();
}


public static void upload(){  
       List<String> list  = new ArrayList<String>();  //要上传的文件名,如:d:\haha.doc.你要实现自己的业务。我这里就是一个空list.  
       list.add("D:\\upPic.zip");
       try {  
           String BOUNDARY = "---------7d4a6d158c9"; // 定义数据分隔线  
           URL url = new URL(urlStr);  
           HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
           // 发送POST请求必须设置如下两行  
           conn.setDoOutput(true);  
           conn.setDoInput(true);  
           conn.setUseCaches(false);  
           conn.setRequestMethod("POST");  
           conn.setRequestProperty("connection", "Keep-Alive");  
           conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");  
           conn.setRequestProperty("Charsert", "UTF-8");   
           conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);  
             
           OutputStream out = new DataOutputStream(conn.getOutputStream());  
           byte[] end_data = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();// 定义最后数据分隔线  
           int leng = list.size();  
           for(int i=0;i<leng;i++){  
               String fname = list.get(i);  
               File file = new File(fname);  
               StringBuilder sb = new StringBuilder();    
               sb.append("--");    
               sb.append(BOUNDARY);    
               sb.append("\r\n");    
               sb.append("Content-Disposition: form-data;name=\"file"+i+"\";filename=\""+ file.getName() + "\"\r\n");    
               sb.append("Content-Type:application/octet-stream\r\n\r\n");    
                 
               byte[] data = sb.toString().getBytes();  
               out.write(data);  
               DataInputStream in = new DataInputStream(new FileInputStream(file));  
               int bytes = 0;  
               byte[] bufferOut = new byte[1024];  
               while ((bytes = in.read(bufferOut)) != -1) {  
                   out.write(bufferOut, 0, bytes);  
               }  
               out.write("\r\n".getBytes()); //多个文件时,二个文件之间加入这个  
               in.close();  
           }  
           out.write(end_data);  
           out.flush();    
           out.close();   
             
           // 定义BufferedReader输入流来读取URL的响应  
           BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));  
           String line = null;  
           while ((line = reader.readLine()) != null) {  
               System.out.println(line);  
           }  
 
       } catch (Exception e) {  
           System.out.println("发送POST请求出现异常!" + e);  
           e.printStackTrace();  
       }  
   }  
 
public static void SendURLPost(String articleId) throws IOException {


 
 url = new URL(urlStr);
 httpURLConnection = (HttpURLConnection) url.openConnection(); // 获取连接
 httpURLConnection.setRequestMethod("POST"); // 设置请求方法为POST, 也可以为GET
 httpURLConnection.setDoOutput(true);


 StringBuffer param = new StringBuffer("ArticleId="); // 请求URL的查询参数
 param.append(articleId);
 OutputStream os = httpURLConnection.getOutputStream();
 os.write(param.toString().getBytes());
 os.flush();
 os.close();


 InputStream is = httpURLConnection.getInputStream();
 BufferedReader br = new BufferedReader(new InputStreamReader(is));
 StringBuilder sb = new StringBuilder();
 while (br.read() != -1) {
  sb.append(br.readLine());
 }
 String content = new String(sb);
 content = new String(content.getBytes("GB2312"), "ISO-8859-1");
 System.out.println(content);
 br.close();
}


public String getUrlStr() {
 return urlStr;
}


public void setUrlStr(String urlStr) {
 this.urlStr = urlStr;
}


public URL getUrl() {
 return url;
}


public void setUrl(URL url) {
 this.url = url;
}


public HttpURLConnection getHttpURLConnection() {
 return httpURLConnection;
}


public void setHttpURLConnection(HttpURLConnection httpURLConnection) {
 this.httpURLConnection = httpURLConnection;
}


public String getResponse() {
 return response;
}


public void setResponse(String response) {
 this.response = response;
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值