1 /**
2 * 通过HTTP协议向指定的网络地址发送文件。3 *@paramparams 传输过程中需要传送的参数4 *@paramfilename 需要传送的文件在本地的位置。5 *@throwsTransferException6 */
7 publicString doPost(HashMap params, InputStream stream)8 throwsTransferException9 {10 URLConnection conn = null; //URL连结对象。
11 BufferedReader in = null; //请求后的返回信息的读取对象。
12 String keyName = null;13 try
14 {15 conn =url.openConnection();16 conn.setUseCaches(false);17 conn.setDoOutput(true);18 conn.setRequestProperty("Content-Type", "multipart/form-data");19
20 //设置参数
21 if (params != null)22 {23 Set keys =params.keySet();24 //遍历参数集取得参数名称和值
25 if (!keys.isEmpty())26 {27 Iterator iterator =keys.iterator();28 while(iterator.hasNext())29 {30 keyName =(String) iterator.next();31 //将参数加入到连接对象中
32 conn.addRequestProperty(33 keyName,34 (String) params.get(keyName));35 }36 }37 }38 //构造传输文件39 //FileInputStream fis = new FileInputStream(filename);
40 BufferedInputStream bis = newBufferedInputStream( stream );41 ByteArrayOutputStream baos = newByteArrayOutputStream();42 intch;43 while ((ch = bis.read()) != -1)44 baos.write(ch);45 byte[] fileData =baos.toByteArray();46
47 //传输文件。
48 DataOutputStream dos =
49 newDataOutputStream(50 newBufferedOutputStream(conn.getOutputStream()));51 dos.write(fileData);52 dos.flush();53 dos.close();54
55
56 in =
57 newBufferedReader(58 newInputStreamReader(conn.getInputStream()));59 //in.close();
60 }61 catch(FileNotFoundException fe)62 {63 InputStream err =((HttpURLConnection) conn).getErrorStream();64 if (err == null)65 throw new TransferException("网络传输时发生的未知错误");66 in = new BufferedReader(newInputStreamReader(err));67 }68 catch(IOException ioe)69 {70 ioe.printStackTrace();71 throw new TransferException("网络传输错误!");72 }73
74 //返回提示信息
75 StringBuffer response = newStringBuffer();76 String line;77 try
78 {79 while ((line = in.readLine()) != null)80 response.append(line + "/n");81 in.close();82 }83 catch(IOException ioe)84 {85 ioe.getStackTrace();86 throw new TransferException("网络响应错误!");87 }88 returnresponse.toString();89 }90
91 /**
92 * 通过HTTP协议向指定的网络地址发送文件。93 *@paramparams 传输过程中需要传送的参数94 *@paramdata 需要传送的内容。95 *@throwsTransferException96 */
97 public InputStream doPost(HashMap params, byte[] data)98 throwsTransferException99 {100 URLConnection conn = null; //URL连结对象。
101 BufferedReader in = null; //请求后的返回信息的读取对象。
102 String keyName = null;103 try
104 {105 conn =url.openConnection();106 conn.setUseCaches(false);107 conn.setDoOutput(true);108 conn.setRequestProperty("Content-Type", "multipart/form-data");109
110 //设置参数
111 if (params != null)112 {113 Set keys =params.keySet();114 //遍历参数集取得参数名称和值
115 if (!keys.isEmpty())116 {117 Iterator iterator =keys.iterator();118 while(iterator.hasNext())119 {120 keyName =(String) iterator.next();121 //将参数加入到连接对象中
122 conn.addRequestProperty(123 keyName,124 (String) params.get(keyName));125 }126 }127 }128
129 //传输文件。
130 DataOutputStream dos =
131 newDataOutputStream(132 newBufferedOutputStream(conn.getOutputStream()));133 dos.write(data);134 dos.flush();135 dos.close();136 returnconn.getInputStream();137 }138 catch(FileNotFoundException fe)139 {140 InputStream err =((HttpURLConnection) conn).getErrorStream();141 if (err == null)142 throw new TransferException("网络传输时发生的未知错误");143 else
144 throw new TransferException("未知错误");145 }146 catch(IOException ioe)147 {148 ioe.printStackTrace();149 throw new TransferException("网络传输错误!");150 }151 }