1 /**
2 * 模拟form表单的形式 ,上传文件 以输出流的形式把文件写入到url中,然后用输入流来获取url的响应
3 *
4 * @param url
5 * 请求地址 form表单url地址
6 * @param filePath
7 * 文件在服务器保存路径
8 * @return JSONObject
9 * url的响应信息返回值
10 * @throws IOException
11 * 文件不存在
12 */
13 public static JSONObject send(String url, String filePath) throws IOException {
14
15 String result = null;
16
17 /**
18 * 第一部分
19 */
20 URL urlObj = new URL(url);
21 // 连接
22 HttpURLConnection con = (HttpURLConnection) urlObj.openConnection();
23
24 /**
25 * 设置关键值
26 */
27 con.setRequestMethod("POST"); // 以Post方式提交表单,默认get方式
28 con.setDoInput(true);
29 con.setDoOutput(true);
30 con.setUseCaches(false); // post方式不能使用缓存
31
32 // 设置请求头信息
33 con.setRequestProperty("Connection", "Keep-Alive");
34 con.setRequestProperty("Charset", "UTF-8");
35
36 // 设置边界
37 String BOUNDARY = "----------" + System.currentTimeMillis();
38 con.setRequestProperty("Content-Type", "multipart/form-data; boundary="
39 + BOUNDARY);
40
41 // 生成文件名
42 String fileName = System.currentTimeMillis()+"-"+(int) Math.random()+filePath.substring(filePath.lastIndexOf('.'));
43
44 // 第一部分:
45 StringBuilder sb = new StringBuilder();
46 sb.append("--"); // 必须多两道线
47 sb.append(BOUNDARY);
48 sb.append("\r\n");
49 sb.append("Content-Disposition: form-data;name=\"file\";filename=\""
50 + fileName + "\"\r\n");
51 sb.append("Content-Type:application/octet-stream\r\n\r\n");
52
53 byte[] head = sb.toString().getBytes("utf-8");
54
55 // 获得输出流
56 OutputStream out = new DataOutputStream(con.getOutputStream());
57 // 输出表头
58 out.write(head);
59
60 // 文件正文部分
61 // 把文件已流文件的方式 推入到url中
62 InputStream in = getWebFile(filePath);
63 int bytes = 0;
64 byte[] bufferOut = new byte[1024];
65 while ((bytes = in.read(bufferOut)) != -1) {
66 out.write(bufferOut, 0, bytes);
67 }
68 in.close();
69
70 // 结尾部分
71 byte[] foot = ("\r\n--" + BOUNDARY + "--\r\n").getBytes("utf-8");// 定义最后数据分隔线
72
73 out.write(foot);
74
75 out.flush();
76 out.close();
77
78 StringBuffer buffer = new StringBuffer();
79 BufferedReader reader = null;
80 try {
81 // 定义BufferedReader输入流来读取URL的响应
82 reader = new BufferedReader(new InputStreamReader(
83 con.getInputStream()));
84 String line = null;
85 while ((line = reader.readLine()) != null) {
86 // System.out.println(line);
87 buffer.append(line);
88 }
89 if (result == null) {
90 result = buffer.toString();
91 }
92 } catch (IOException e) {
93 System.out.println("发送POST请求出现异常!" + e);
94 e.printStackTrace();
95 throw new IOException("数据读取异常");
96 } finally {
97 if (reader != null) {
98 reader.close();
99 }
100
101 }
102
103 JSONObject jsonObj = JSONObject.fromObject(result);
104 return jsonObj;
105 }
106
107
108 /**
109 * 从网络中获取一个文件
110 * @param imgUrl
111 * 在网络中文件路径
112 * @return InputStream
113 * 返回获取到的文件流
114 */
115 public static InputStream getWebFile(String imgUrl) {
116 // new一个URL对象
117 URL url;
118 InputStream inStream = null;
119 try {
120 url = new URL(imgUrl);
121 // 打开链接
122 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
123 // 设置请求方式为"GET"
124 conn.setRequestMethod("GET");
125 // 超时响应时间为5秒
126 conn.setConnectTimeout(5 * 1000);
127 // 通过输入流获取图片数据
128 inStream = conn.getInputStream();
129 } catch (Exception e) {
130 System.out.println("文件不存在!");
131 }
132 return inStream;
133 }
134