java原生 HttpUrlConnection 实现post请求提交文件
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java原生 HttpUrlConnection 实现post请求提交文件,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2796字,纯文字阅读大概需要4分钟。
内容图文
![java原生 HttpUrlConnection 实现post请求提交文件](/upload/InfoBanner/zyjiaocheng/593/66711c2a1c1c43ea856fdf4742851e11.jpg)
CNM的HttpUrlConnection
真他妈难用,狗屎。
网上抄的:
public static String doPostWithFile(String url,String savefileName,String fileName, String param) {
String result = "";
try {
// 换行符
final String newLine = "\r\n";
final String boundaryPrefix = "--";
// 定义数据分隔线
String BOUNDARY = "========7d4a6d158c9";
// 服务器的域名
URL realurl = new URL(url);
// 发送POST请求必须设置如下两行
HttpURLConnection connection = (HttpURLConnection) realurl.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection","Keep-Alive");
connection.setRequestProperty("Charset","UTF-8");
connection.setRequestProperty("Content-Type","multipart/form-data; boundary=" + BOUNDARY);
// 头
String boundary = BOUNDARY;
// 传输内容
StringBuffer contentBody =new StringBuffer("--" + BOUNDARY);
// 尾
String endBoundary ="\r\n--" + boundary + "--\r\n";
//输出
OutputStream out = connection.getOutputStream();
// 1. 处理普通表单域(即形如key = value对)的POST请求(这里也可以循环处理多个字段,或直接给json)
//这里看过其他的资料,都没有尝试成功是因为下面多给了个Content-Type
//form-data 这个是form上传 可以模拟任何类型
contentBody.append("\r\n")
.append("Content-Disposition: form-data; name=\"")
.append("param" + "\"")
.append("\r\n")
.append("\r\n")
.append(param)
.append("\r\n")
.append("--")
.append(boundary);
String boundaryMessage1 =contentBody.toString();
System.out.println(boundaryMessage1);
out.write(boundaryMessage1.getBytes(StandardCharsets.UTF_8));
// 2. 处理file文件的POST请求(多个file可以循环处理)
contentBody = new StringBuffer();
contentBody.append("\r\n")
.append("Content-Disposition:form-data; name=\"")
.append("file" +"\"; ") // form中field的名称
.append("filename=\"")
.append(fileName +"\"") //上传文件的文件名,包括目录
.append("\r\n")
.append("Content-Type:multipart/form-data")
.append("\r\n\r\n");
String boundaryMessage2 = contentBody.toString();
System.out.println(boundaryMessage2);
out.write(boundaryMessage2.getBytes("utf-8"));
// 开始真正向服务器写文件
File file = new File(savefileName);
DataInputStream dis= new DataInputStream(new FileInputStream(file));
int bytes = 0;
byte[] bufferOut =new byte[(int) file.length()];
bytes =dis.read(bufferOut);
out.write(bufferOut,0, bytes);
dis.close();
byte[] endData = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();
out.write(endData);
out.flush();
out.close();
// 4. 从服务器获得回答的内容
String strLine="";
String strResponse ="";
InputStream in =connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
while((strLine =reader.readLine()) != null)
{
strResponse +=strLine +"\n";
}
System.out.print(strResponse);
return strResponse;
} catch (Exception e) {
System.out.println("发送POST请求出现异常!" + e);
e.printStackTrace();
}
return result;
}
内容总结
以上是互联网集市为您收集整理的java原生 HttpUrlConnection 实现post请求提交文件全部内容,希望文章能够帮你解决java原生 HttpUrlConnection 实现post请求提交文件所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。