import java.io.*; import java.net.HttpURLConnection; import java.net.URL; import java.util.HashMap; import java.util.Map; public class fujian { public static void main(String args[]) throws Exception{ // 从接口返回信息中拿到resourceUrls String resourceUrl = "你获取到的resourceUrl"; // 从接口返回信息中拿到headers Map<String, String> headers=new HashMap<>(); headers.put("Authorization","你获取到的Authorization"); headers.put("x-oss-date","你获取到的x-oss-date"); URL url = new URL(resourceUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); if (headers != null) { for (Map.Entry<String, String> entry : headers.entrySet()) { connection.setRequestProperty(entry.getKey(), entry.getValue()); } } connection.setDoOutput(true); connection.setRequestMethod("PUT"); connection.setUseCaches(false); connection.setReadTimeout(10000); connection.setConnectTimeout(10000); connection.connect(); OutputStream out = connection.getOutputStream(); InputStream is = new FileInputStream(new File("/Users/Downloads/你的文件.xlsx")); byte[] b = new byte[1024]; int temp; while ((temp = is.read(b)) != -1) { out.write(b, 0, temp); } out.flush(); out.close(); int responseCode = connection.getResponseCode(); connection.disconnect(); if (responseCode == 200) { System.out.println("上传成功"); } else { System.out.println("上传失败"); } } }
标签:java,发起,接口,headers,connection,import,附件,new,out From: https://www.cnblogs.com/Andy-Blog/p/18205597