java发送post请求并下载返回⽂件直接上代码
import java.io.*;
import java.HttpURLConnection;
import java.URL;
import java.URLEncoder;
import java.util.*;
public class ExportPost {
public static String url = "localhost:8088/file/xxx/download2";public static String cookie = "cookie";
public static void main(String[] args) throws Exception {
getInternetRes("D:\\demoefile",url,"a.pdf");
}
connect下载public static void getInternetRes(String newUrl, String oldUrl, String fileName) throws Exception {
URL url = null;
HttpURLConnection con = null;
InputStream in = null;
FileOutputStream out = null;
try {
url = new URL(oldUrl);
//建⽴http连接,得到连接对象
con = (HttpURLConnection) url.openConnection();
//设置通⽤的请求头属性
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestMethod("POST");    // 默认是 GET⽅式
con.setUseCaches(false);        // Post 请求不能使⽤缓存
con.setInstanceFollowRedirects(true);  //设置本次连接是否⾃动重定向
con.setRequestProperty("User-Agent", "Mozilla/5.0");
con.setRequestProperty("Content-Type", "application/form-data");
//            con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//            con.setRequestProperty("Content-Type","application/form-data;charset=utf-8");
// boundary就是request头和上传⽂件内容的分隔符
String BOUNDARY = "---------------------------123821742118716";
con.setRequestProperty("Content-Type","multipart/form-data; boundary=" + BOUNDARY);
con.setRequestProperty("Connection","Keep-Alive");//设置与服务器保持连接
con.setRequestProperty("Accept-Language", "zh-CN,zh;0.9");
con.setRequestProperty("cookie",cookie);
StringBuilder sb2 = new StringBuilder();
sb2.append("\r\n").append("--").append(BOUNDARY).append("\r\n");
sb2.append("Content-Disposition: form-data; name=\"no\"\r\n\r\n");
sb2.append("xxx");
sb2.append("\r\n").append("--").append(BOUNDARY).append("\r\n");
sb2.append("Content-Disposition: form-data; name=\"json\"\r\n\r\n");
sb2.append("{\"id\": \"123456\",\"mc\": \"测试名称\"}");
OutputStream outputStream = OutputStream();
outputStream.String().getBytes());
byte[] endData = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();
outputStream.write(endData);
outputStream.flush();
outputStream.close();
//输⼊流读取⽂件
in = InputStream();
//转化为byte数组
byte[] data = getByteData(in);
//建⽴存储的⽬录、保存的⽂件名
File file = new File(newUrl+ DateUtil.formatDate(new Date()));
if (!ists()) {
file.mkdirs();
}
//修改⽂件名⽤id重命名
File res = new File(file + File.separator + fileName);
//写⼊输出流
out = new FileOutputStream(res);
out.write(data);
//            logger.info("下载 successfully!");
System.out.println("下载 successfully!");
} catch (IOException e) {
//            ("下载出错!"+e.toString());
System.out.println("下载出错!"+e.toString());
} finally {
//关闭流
try {
if (null != out){
out.close();
}
if (null != in){
in.close();
}
} catch (IOException e) {
//                ("下载出错!"+e.toString());
System.out.println("下载出错!"+e.toString());
}
}
}
/**
*
* @Title: getByteData
* @Description: 从输⼊流中获取字节数组
* @author zml
* @date Sep 12, 2017 7:38:57 PM
*/
private static byte[] getByteData(InputStream in) throws IOException { byte[] b = new byte[1024];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int len = 0;
while ((len = in.read(b)) != -1) {
bos.write(b, 0, len);
}
if(null!=bos){
bos.close();
}
ByteArray();
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。