帮我写一个java方法,调用POST请求,请求返回的是一个文件流
时间: 2023-03-15 20:29:00 浏览: 99
public static File getFileFromPOSTRequest(String url) throws IOException {
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
InputStream response = con.getInputStream();
File file = new File("filename.txt");
FileOutputStream fileOut = new FileOutputStream(file);
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = response.read(buffer)) != -1) {
fileOut.write(buffer, 0, bytesRead);
}
fileOut.close();
return file;
}
阅读全文