1、使用java的HttpURLconnnection上传
List<String> list = new ArrayList<String>(); // 上传多个文件使用
try {
// 定义数据分隔线
String BOUNDARY = "---------7d4a6d158c9";
byte[] end_data = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();// 定义最后数据分隔线
URL url = new URL("http://localhost:8080/service/latent.latentinfo!addLatentBranch.action");
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("Charsert", "UTF-8");
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
OutputStream out = new DataOutputStream(conn.getOutputStream());
// int leng = list.size(); 上传多个文件时使用
// for (int i = 0; i < leng; i++) {
// String fname = list.get(i);
// 上传文件
File file = new File("G:/素材文档/19.jpg");
StringBuilder sb = new StringBuilder();
sb.append("--");
sb.append(BOUNDARY);
sb.append("\r\n");
sb.append("Content-Disposition: form-data;name=\"photo\";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();
//}
// json参数
sb = new StringBuilder();
sb.append("--");
sb.append(BOUNDARY);
sb.append("\r\n");
sb.append("Content-Disposition: form-data;name=\"params\"\r\n\r\n");每个请求头后面是两个回车换行
out.write(sb.toString().getBytes());
out.write("{'user_name':'apple','channel_name':'测试渠道','channel_address':'(123.4,30.6)'}".getBytes());
out.write("\r\n".getBytes());
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();
}
}
2.使用apache-httpclient上传
// 图片地址
String photo = "G:/素材文档/19.jpg";
// 定义请求url
String uri = "http://localhost:8080/service/latent.latentinfo!addLatentBranch.action";
// 实例化http客户端
HttpClient httpClient = new DefaultHttpClient();
// 实例化post提交方式
HttpPost post = new HttpPost(uri);
// 实例化参数对象
MultipartEntity params = new MultipartEntity();
// 添加json参数
params.addPart("params", new StringBody("{'user_name':'apple','channel_name':'测试渠道','channel_address':'(123.4,30.6)'}",Charset.forName("UTF-8")));
// 设置上传文件
File file = new File("G:/素材文档/19.jpg");
FileBody fileBody = new FileBody(file);
params.addPart("photo", fileBody);
params.addPart("photoName", new StringBody(file.getName()));
// 将参数加入post请求体中
post.setEntity(params);
// 执行post请求并得到返回对象
HttpResponse resp = httpClient.execute(post);
// 解析返回对象
HttpEntity entity = resp.getEntity();
InputStream is = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuffer buffer = new StringBuffer();
String temp ;
while ((temp = reader.readLine()) != null) {
buffer.append(temp);
}
System.out.println(buffer);
}
catch (Exception ex) {
ex.printStackTrace();
}
List<String> list = new ArrayList<String>(); // 上传多个文件使用
try {
// 定义数据分隔线
String BOUNDARY = "---------7d4a6d158c9";
byte[] end_data = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();// 定义最后数据分隔线
URL url = new URL("http://localhost:8080/service/latent.latentinfo!addLatentBranch.action");
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("Charsert", "UTF-8");
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
OutputStream out = new DataOutputStream(conn.getOutputStream());
// int leng = list.size(); 上传多个文件时使用
// for (int i = 0; i < leng; i++) {
// String fname = list.get(i);
// 上传文件
File file = new File("G:/素材文档/19.jpg");
StringBuilder sb = new StringBuilder();
sb.append("--");
sb.append(BOUNDARY);
sb.append("\r\n");
sb.append("Content-Disposition: form-data;name=\"photo\";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();
//}
// json参数
sb = new StringBuilder();
sb.append("--");
sb.append(BOUNDARY);
sb.append("\r\n");
sb.append("Content-Disposition: form-data;name=\"params\"\r\n\r\n");每个请求头后面是两个回车换行
out.write(sb.toString().getBytes());
out.write("{'user_name':'apple','channel_name':'测试渠道','channel_address':'(123.4,30.6)'}".getBytes());
out.write("\r\n".getBytes());
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();
}
}
2.使用apache-httpclient上传
// 图片地址
String photo = "G:/素材文档/19.jpg";
// 定义请求url
String uri = "http://localhost:8080/service/latent.latentinfo!addLatentBranch.action";
// 实例化http客户端
HttpClient httpClient = new DefaultHttpClient();
// 实例化post提交方式
HttpPost post = new HttpPost(uri);
// 实例化参数对象
MultipartEntity params = new MultipartEntity();
// 添加json参数
params.addPart("params", new StringBody("{'user_name':'apple','channel_name':'测试渠道','channel_address':'(123.4,30.6)'}",Charset.forName("UTF-8")));
// 设置上传文件
File file = new File("G:/素材文档/19.jpg");
FileBody fileBody = new FileBody(file);
params.addPart("photo", fileBody);
params.addPart("photoName", new StringBody(file.getName()));
// 将参数加入post请求体中
post.setEntity(params);
// 执行post请求并得到返回对象
HttpResponse resp = httpClient.execute(post);
// 解析返回对象
HttpEntity entity = resp.getEntity();
InputStream is = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuffer buffer = new StringBuffer();
String temp ;
while ((temp = reader.readLine()) != null) {
buffer.append(temp);
}
System.out.println(buffer);
}
catch (Exception ex) {
ex.printStackTrace();
}