日期:2014-05-20 浏览次数:20785 次
String urlPath = "http://www.xxxxx.com"; URL url = new URL(urlPath);// 得到网址 String BOUNDARY = "---------7d4a6d158c9";// 设置分隔线 byte[] end_data = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();// 定义最后数据分隔线 HttpURLConnection conn = (HttpURLConnection) url.openConnection();//获得连接 // 参数设置 conn.setRequestMethod("POST"); conn.setDoInput(true); conn.setDoOutput(true); conn.setUseCaches(false); conn.setRequestMethod("POST"); OutputStream outputStream = conn.getOutputStream();//获得输出流 StringBuilder sb = new StringBuilder(); sb.append("--"); sb.append(BOUNDARY); sb.append("\r\n"); sb.append("Content-Disposition: form-data;name=\"file1\";filename=\"" + file.getName() + "\"\r\n"); sb.append("Content-Type:application/octet-stream\r\n\r\n"); sb.append(end_data); byte[] data = sb.toString().getBytes(); outputStream.write(data); // 开始上传 int outSize; byte[] outBuff = new byte[1024]; while ((outSize = fileInputStream.read(outBuff, 0, 1024)) > 0) { outputStream.write(outBuff, 0, outSize);// 开始上传 } outputStream.flush(); outputStream.close(); fileInputStream.close();